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

📄 dcomclientdlg.cpp

📁 用ATL写的操作数据库实例
💻 CPP
字号:
// DcomClientDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DcomClient.h"
#include "DcomClientDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CDcomClientDlg dialog

CDcomClientDlg::CDcomClientDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDcomClientDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDcomClientDlg)
		// 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 CDcomClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDcomClientDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDcomClientDlg, CDialog)
	//{{AFX_MSG_MAP(CDcomClientDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDcomClientDlg message handlers

BOOL CDcomClientDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDcomClientDlg::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 CDcomClientDlg::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 CDcomClientDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
#include "../DcomServer/DcomServer.h"
#include <atlbase.h>
void CDcomClientDlg::OnButton1() 
{
	CoInitializeEx(NULL, COINIT_MULTITHREADED);

	HRESULT hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
		RPC_C_AUTHN_LEVEL_NONE, RPC_C_IMP_LEVEL_IDENTIFY, NULL, EOAC_NONE, NULL);
	
	CString strIP = "cc";
	CString strUser = "superstar";
	CString strPass = "0";
	COAUTHIDENTITY	us;
	
	us.User           = strUser.AllocSysString();
	us.UserLength     = wcslen(us.User);
	us.Password       = strPass.AllocSysString();
	us.PasswordLength = wcslen(us.Password);
	us.Domain         = NULL;
	us.DomainLength   = 0;
	us.Flags          = SEC_WINNT_AUTH_IDENTITY_UNICODE;
	
	COAUTHINFO	auth;
	
	auth.dwAuthnSvc	          = RPC_C_AUTHN_WINNT;
	auth.dwAuthzSvc	          = RPC_C_AUTHZ_NONE;
	auth.pwszServerPrincName  = NULL;
	auth.dwAuthnLevel         = RPC_C_AUTHN_LEVEL_CONNECT;
	auth.dwImpersonationLevel = RPC_C_IMP_LEVEL_IMPERSONATE;
	auth.dwCapabilities       = EOAC_NONE;
	auth.pAuthIdentityData    = &us;
	
	COSERVERINFO si;
	MULTI_QI     qi;
	
	CComBSTR bstr = strIP;
	LPWSTR name   = bstr.m_str;
	
	si.dwReserved1 = 0;
	si.pwszName    = name;
	si.pAuthInfo   = &auth;
	si.dwReserved2 = 0;

	IAdoProcess *pIExitWindow = NULL;
	
	IID iid = __uuidof(pIExitWindow);
	qi.pIID = &iid;
	qi.pItf = NULL;

	do
	{
		hr = CoCreateInstanceEx(__uuidof(AdoProcess), NULL, CLSCTX_REMOTE_SERVER/*CLSCTX_SERVER*/, &si, 1, &qi);
		if(FAILED(hr) || FAILED(qi.hr))
			break ;

		pIExitWindow = (IAdoProcess *)qi.pItf;

/*	VARIANT Data;
	hr = pIExitWindow->test3(&Data);
	SAFEARRAY *psa = Data.parray;
	SAFEARRAYBOUND   bd   =   {10 ,   0 };   
	BSTR  *m;   
	SafeArrayAccessData(psa,   (void   **)&m);   
	//***************   

	//     对pData[]赋值   
/*	for   (int   i   =0;i<10;i++)   
	{   
		Data[i]=i;   //全为1   
	}  */ 
	//int a1 = m[3];
	//***************   
/*	SafeArrayUnaccessData(psa); 	
	CString a0 = CString(m[0]);
	CString a1 = CString(m[1]);
	CString a2 = CString(m[2]);
	CString a3 = CString(m[3]);
	CString a4 = CString(m[4]);
	CString a5 = CString(m[5]);
	CString a6 = CString(m[6]);
	CString a7 = CString(m[7]);
	CString a8 = CString(m[8]);
	CString a9 = CString(m[9]);*/


		int num = 0;
		CString sServer = "172.16.2.135";
		CString sDb		= "DcomDb";
		CString sUser	= "sa";
		CString sPwd	= "123456";

		hr = pIExitWindow->InitData(sServer.AllocSysString(),(sDb.AllocSysString()),sUser.AllocSysString(),sPwd.AllocSysString());
		hr = pIExitWindow->Connect();
		CString sQl = "Select * from test";
		DWORD nCount = 0;
		hr = pIExitWindow->GetRecordCount(sQl.AllocSysString(),&nCount);

		CString sItem = "c";
		const int nNum = nCount;
		VARIANT *pppData = new VARIANT[nNum];
		hr = pIExitWindow->GetRecordDataDWORD(sQl.AllocSysString(),sItem.AllocSysString(),nCount,pppData);
		SAFEARRAY *psa = pppData->parray;
		SAFEARRAYBOUND   bd   =   {nCount ,   0 };   
		DWORD  *m;   
		SafeArrayAccessData(psa,   (void   **)&m);   
		//***************   

		//     对pData[]赋值   
	/*	for   (int   i   =0;i<10;i++)   
		{   
			Data[i]=i;   //全为1   
		}  */ 
		//int a1 = m[3];
		//***************   
		SafeArrayUnaccessData(psa); 		
		
		DWORD a01 = m[0];
		DWORD a11 = m[1];
		DWORD a21 = m[2];
		DWORD a31 = m[3];
		DWORD a41 = m[4];
		DWORD a51 = m[5];
		DWORD a61 = m[6];
		DWORD a71 = m[7];
		DWORD a81 = m[8];
		
		sItem = "a";
		//BSTR *ppData = new BSTR[nCount];
		VARIANT *pData = new VARIANT[nCount];
		hr = pIExitWindow->GetRecordDataChar(sQl.AllocSysString(),sItem.AllocSysString(),nCount,pData);
		SAFEARRAY *psa1 = pData->parray;
		SAFEARRAYBOUND   bd1   =   {nCount ,   0 };   
		BSTR  *m1;   
		SafeArrayAccessData(psa1,   (void   **)&m1);   
		//***************   

		//     对pData[]赋值   
	/*	for   (int   i   =0;i<10;i++)   
		{   
			Data[i]=i;   //全为1   
		}  */ 
		//int a1 = m[3];
		//***************   
		SafeArrayUnaccessData(psa); 	
		CString a0 = CString(m1[0]);
		CString a1 = CString(m1[1]);
		CString a2 = CString(m1[2]);
		CString a3 = CString(m1[3]);
		CString a4 = CString(m1[4]);
		CString a5 = CString(m1[5]);
		CString a6 = CString(m1[6]);
		CString a7 = CString(m1[7]);
		CString a8 = CString(m1[8]);

		sQl = "insert into test (a,b) values('df','fasf')";
		hr = pIExitWindow->ExecuteSQL(sQl.AllocSysString());
		pIExitWindow->Release();
		pIExitWindow = NULL;
	}while(0);		
}

⌨️ 快捷键说明

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