⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bankserverdlg.cpp

📁 用interbus作为中间件的工具模拟银行自动取款机ATM.
💻 CPP
字号:
// BankServerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "BankServer.h"
#include "BankServerDlg.h"

//////////////////////////////////////////////////////////////////////////
//corba
#include <STAR/CORBA.h>

#include "..\\bank_impl.h"

#include <stdlib.h>
#include <errno.h>

#ifdef HAVE_FSTREAM
#   include <fstream>
#else
#   include <fstream.h>
#endif

#ifdef HAVE_STD_IOSTREAM
using namespace std;
#endif
//////////////////////////////////////////////////////////////////////////


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

///////////////////////////////////////////////////////////////////////////
//线程
typedef struct ThreadData
{
	CBankServerDlg* pDlg;
}THREADDATA;
UINT WorkThreadProc(LPVOID lParam);

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBankServerDlg dialog

CBankServerDlg::CBankServerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBankServerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBankServerDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

void CBankServerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBankServerDlg)
	DDX_Control(pDX, IDC_LIST_MSGS, m_ctlList);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CBankServerDlg, CDialog)
	//{{AFX_MSG_MAP(CBankServerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CLERAR, OnClerar)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_START, OnStart)
	//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBankServerDlg message handlers

BOOL CBankServerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here


	//数据库操作
	//初始化环境
	::CoInitialize(NULL);

	// 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
	// 因为它有时会经常出现一些意想不到的错误。
	try 
	{ 		
		//创建并打开数据库连接对象
		m_pCon.CreateInstance(__uuidof(Connection));
		// 打开本地Access库acount.mdb
		m_pCon->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=bank.mdb","","",adModeUnknown);
		//创建并打开记录集对象
		m_pRs.CreateInstance(__uuidof(Recordset));
		
	}
	catch(_com_error e)
	{
		AfxMessageBox("数据库连接失败,确认数据库bank.mdb是否在当前路径下!");
		return FALSE;
	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CBankServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CBankServerDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CBankServerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}




void CBankServerDlg::OnClerar() 
{
	// TODO: Add your control notification handler code here
	m_ctlList.ResetContent();
	UpdateData(FALSE);
	
}

void CBankServerDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	//关闭数据库连接
	if(m_pCon->State)
		m_pCon->Close();
	m_pCon= NULL; 
	//释放环境
	::CoUninitialize();	
}

CString CBankServerDlg::getFieldValue(CString strID, CString strField)
{
	_variant_t vFieldValue;
	CString strFieldValue;
	CString strSQL;
	strSQL = "select * from account where ID=\'";
	strSQL += strID;
	strSQL += "\'";
	//AfxMessageBox((LPCSTR)strSQL);
	try
	{
		m_pRs->Open((LPCSTR)strSQL, m_pCon.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
		AfxMessageBox("数据库操作错误!");
		m_pRs->Close();
		return "";
	}
	
	if(VARIANT_FALSE == m_pRs->EndOfFile)
    {
		//获得记录
		vFieldValue = m_pRs->GetCollect((LPCSTR)strField);
		strFieldValue = (char*)_bstr_t(vFieldValue);
		vFieldValue.Clear();
		m_pRs->Close();
		return strFieldValue;
    }
	else 
	{
		m_pRs->Close();
		return "";
	}

}

void CBankServerDlg::OnOK() 
{
	// TODO: Add extra validation here
	OnDestroy();
    ExitProcess(1);
	//停止服务	
	CDialog::OnOK();
}

BOOL CBankServerDlg::updateSUM(CString strID, double dMoney)
{
	_variant_t vFieldValue;
	CString strFieldValue;
	CString strSQL;
	strSQL = "select * from account where ID=\'";
	strSQL += strID;
	strSQL += "\'";
	try
	{
		m_pRs->Open((LPCSTR)strSQL, m_pCon.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
		AfxMessageBox("数据库操作错误!");
		m_pRs->Close();
		return FALSE;
	}
	
	try
	{
		m_pRs->MoveFirst();
		m_pRs->PutCollect("SUM", dMoney);
		m_pRs->Update();

	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
		AfxMessageBox("数据库更新错误!");
		m_pRs->Close();
		return FALSE;
	}
	m_pRs->Close();
	return TRUE;

}

BOOL CBankServerDlg::updatePAW(CString strID, CString strPAW)
{
	_variant_t vFieldValue;
	CString strFieldValue;
	CString strSQL;
	strSQL = "select * from account where ID=\'";
	strSQL += strID;
	strSQL += "\'";
	try
	{
		m_pRs->Open((LPCSTR)strSQL, m_pCon.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
		AfxMessageBox("数据库操作错误!");
		m_pRs->Close();
		return FALSE;
	}
	
	try
	{
		m_pRs->MoveFirst();
		m_pRs->PutCollect("PAW", _variant_t(strPAW));
		m_pRs->Update();
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
		AfxMessageBox("数据库更新错误!");
		m_pRs->Close();
		return FALSE;
	}
	m_pRs->Close();
	return TRUE;
}

void CBankServerDlg::addListMsg(CString msg)
{
	int index = m_ctlList.AddString((LPCSTR)msg);
	m_ctlList.SetCaretIndex(index);
}

void CBankServerDlg::OnStart() 
{
	// TODO: Add your control notification handler code here
	THREADDATA* pData = new THREADDATA;
	pData->pDlg = this;
	AfxBeginThread(WorkThreadProc,pData);
	
}
//工作线程
UINT WorkThreadProc(LPVOID lParam)
{
	//参数错误,返回
	if(lParam == NULL)
		return -1;

	//转换参数
	THREADDATA* pData = (THREADDATA*)lParam;

	CORBA::ORB_var orb;
	int argc = 0;
	char** argv = NULL;
	int status;
	try
    {
		orb = CORBA::ORB_init(argc, argv);
		//
		// Resolve Root POA
		//
		CORBA::Object_var poaObj = orb -> resolve_initial_references("RootPOA");
		PortableServer::POA_var rootPOA = PortableServer::POA::_narrow(poaObj);
		
		//
		// Get a reference to the POA manager
		//
		PortableServer::POAManager_var manager = rootPOA -> the_POAManager();
		
		//
		// Create implementation object
		//
		BankAccount_impl* bankImpl = new BankAccount_impl();
		bankImpl->setDlg(pData->pDlg);
		PortableServer::ServantBase_var servant = bankImpl;
		BankAccount_var bank = bankImpl -> _this();
		
		//
		// Save reference
		//
		CORBA::String_var s = orb -> object_to_string(bank);
		
		const char* refFile = "..\\BankAccountobj.ref";
		ofstream out(refFile);
		if(out.fail())
		{
			AfxMessageBox(": can't open");
			status = EXIT_FAILURE;
		}
		
		out << s << endl;
		out.close();
		
		//
		// Run implementation
		//
		pData->pDlg->addListMsg("开始服务!");
		manager -> activate();
		
		orb -> run();
		
		status = EXIT_SUCCESS;
	}
	   catch(const CORBA::Exception& ex)
	   {
		   cerr << ex << endl;
		   status = EXIT_FAILURE;
	   }
	   
	   if(!CORBA::is_nil(orb))
	   {
		   try
		   {
			   orb -> destroy();
		   }
		   catch(const CORBA::Exception& ex)
		   {
			   cerr << ex << endl;
			   status = EXIT_FAILURE;
		   }
	   }
    


	//清除内存
	delete pData;
	return status;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -