Developing COM Components Using VC-ATL (1-2)

xiaoxiao2021-03-06  40

The following is a complete code for the customer test end of the four functions. // myexedlg.cpp: Implementation File

//

#include "stdafx.h"

#include "myexe.h"

#include "myexedlg.h"

#ifdef _Debug

#define new debug_new

#undef this_file

Static char this_file [] = __file__;

#ENDIF

USING NAMESPACE MyProjlib;

/

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

/

// CMYEXEDLG DIALOG

CMYEXEDLG :: CMYEXEDLG (CWND * PParent / * = null * /)

: Cdialog (cmyexedlg :: IDd, pparent)

{

// {{AFX_DATA_INIT (CMYEXEDLG)

// 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 CMYEXEDLG :: DODATAEXCHANGE (CDataExchange * PDX)

{

CDIALOG :: DODATAEXCHANGE (PDX);

// {{AFX_DATA_MAP (CMYEXEDLG)

// Note: The Classwizard Will Add Ddx and DDV Calls Here

//}} AFX_DATA_MAP

}

Begin_MESSAGE_MAP (CMYEXEDLG, CDIALOG)

// {{AFX_MSG_MAP (CMYEXEDLG)

ON_WM_SYSCOMMAND () ON_WM_PAINT ()

ON_WM_QUERYDRAGICON ()

ON_BN_CLICKED (IDC_Button1, ONMYF1)

ON_BN_CLICKED (IDC_BUTTON2, ONMYF2)

ON_BN_CLICKED (IDC_Button3, ONMYF3)

ON_BN_CLICKED (IDC_Button4, ONMYF4)

//}} AFX_MSG_MAP

END_MESSAGE_MAP ()

/

// CMYEXEDLG Message Handlers

Bool cmyexedlg :: 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 AutomaticL

// 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 UnsS you set the focus to a control

}

Void CMYEXEDLG :: 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 automatic or done for you by the framework.

void cmyexedlg :: onpaint ()

{

IF (Isiconic ())

{

CPAINTDC DC (this); // Device Context for Painting

SendMessage (WM_ICONERASEBKGND, (WPARAM) dc.getsafehdc (), 0); // Center icon in Client Rectangle

INT CXCION = 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 CMYEXEDLG :: ONQUERYDRAGICON ()

{

Return (hcursor) m_hicon;

}

Void cmyexedlg :: onmyf1 ()

{

// Todo: Add Your Control Notification Handler Code Here

HRESULT HR = Coinitialize (NULL);

CLSID CLSID;

HR = CLSIDFROGID (Olestr ("MyProj.mycom"), & clsid;

IF (Failed (HR))

{

AfxMessageBox ("com failed");

Return;

}

IMycom * ptr = null;

HR = CocreateInstance (CLSID, NULL, CLSCTX_INPROC_SERVER, __ uUidof (iMycom), (lpvoid *) & ptr);

Ptr-> myf1 ();

Ptr-> release (); // release example

Couninitialize ();

}

Void cmyexedlg :: onmyf2 ()

{

// Todo: Add Your Control Notification Handler Code Here

HRESULT HR = Coinitialize (NULL);

CLSID CLSID;

HR = CLSIDFROGID (Olestr ("MyProj.mycom"), & clsid;

IF (Failed (HR))

{

AfxMessageBox ("com failed");

Return;

}

IMycom * ptr = null;

HR = CocreateInstance (CLSID, NULL, CLSCTX_INPROC_SERVER, __ uUidof (iMycom), (lpvoid *) & ptr);

Char Str [32];

Sprintf (STR, "LEN =% D", PTR-> Myf2 ("AbcDefg"));

AfxMessageBox (STR);

Ptr-> release (); // release example

Couninitialize ();

}

Void cmyexedlg :: onmyf3 ()

{

// Todo: Add Your Control Notification Handler Code Here

HRESULT HR = Coinitialize (NULL);

CLSID CLSID;

HR = CLSIDFROMPROGID (OLESTR ("MyProj.MYCOM"), & clsid); if (failed (HR))

{

AfxMessageBox ("com failed");

Return;

}

IMycom * ptr = null;

HR = CocreateInstance (CLSID, NULL, CLSCTX_INPROC_SERVER, __ uUidof (iMycom), (lpvoid *) & ptr);

AfxMessageBox (Ptr-> Myf3 ("AbcDefg");

Ptr-> release (); // release example

Couninitialize ();

}

void cmyexedlg :: onmyf4 ()

{

// Todo: Add Your Control Notification Handler Code Here

HRESULT HR = Coinitialize (NULL);

CLSID CLSID;

HR = CLSIDFROGID (Olestr ("MyProj.mycom"), & clsid;

IF (Failed (HR))

{

AfxMessageBox ("com failed");

Return;

}

IMycom * ptr = null;

HR = CocreateInstance (CLSID, NULL, CLSCTX_INPROC_SERVER, __ uUidof (iMycom), (lpvoid *) & ptr);

INT x = 8; char STR [32];

Sprintf (STR, "X =% D, X 1 =% D", X, PTR-> myf4 (x));

AfxMessageBox (STR);

Ptr-> release (); // release example

Couninitialize ();

} Through this simple example, we have made the simplest COM component, and the client of the test component, just teaching everyone how to use VC ATL to do COM components, in next this article, Dei PeiPing COM Components Using VC-ATL (2) The basic principles of CoM will be introduced, and the source code of this example is analyzed. Developing COM Components Using VC-ATL (1)

Author Blog:

http://blog.9cbs.net/callzjy/

转载请注明原文地址:https://www.9cbs.com/read-77831.html

New Post(0)