Developing COM Components Using VC-ATL (3-6)

xiaoxiao2021-03-06  39

N Visual Basic calls Visual C components

N write steps:

1. Create a new standard engineering, the engineering name is set to vbtestvccomproj; introduce components, select menu Project-> References ..., click Browse, select C: / ValidatecreditCardServer / debug / validatecreditcardServer.dll; knock into the following form code (don't forget Put two text boxes on the form and a button).

Private submmand1_click ()

Dim Comobj As New ValidatecreditcardServerlib.validateCard

If Comobj.validatecreditcard (Text1.Text, Text2.Text) = 1 THEN

Msgbox "Valid Card"

Else

MSGBOX "Invalid Card"

END IF

End Sub

2. Cut the run test procedure.

n code test: the same (omitted).

N Visual C calls Visual C components

Here I no longer introduce the steps, I believe that through the previous two examples, you should master the specific operational points. Here I only briefly mention the program.

1. Create a dialog-based application using Visual C , the engineering name is VCTestVCCCBPROJ, just like the above test engineering, placed two text boxes and a certaintable button on the dialog.

2. Import the type library of the server in the client

2-1 Add code in stdafx.h file

#import "../Validatecreditcardserver/validatecreditcardserver.tlb"

or

#import "../Validatecreditcardserver/debug/validatecreditcardserver.dll"

Both methods can be!

2-2 Compile STDAFX.CPP, generate packaging classes, the two files created in the Customer Engineering Space (C: / VCTestVCCuggProj / Debug): Type LiberreditCardServer.tlh and Type Library implementation files ( ValidatecreditCardServer.tli).

2-3 Dialog The function of accessing the server components requires the server packaging class name space. To this end, add the following statement in the head of the VCTestVCComProjdlg.cpp file:

Using Namespace ValidatecreditcardServerLib;

3. Initialize the COM library

4. Get the CLSID of the server

5. Creating an instance of COM server components

6. Use a COM object

7. Terminate the COM library

VCTestVCCComprojdlg.cpp list

// vctestvccomprojdlg.cpp: Implementation File

//

#include "stdafx.h"

#include "vctestvccomproj.h"

#include "vctestvccomprojdlg.h"

#ifdef _Debug

#define new debug_new

#undef this_file

Static char this_file [] = __file__;

#ndifusioning namespace validatecreditcardSerLib;

...

Begin_Message_Map (CVCTestVCCComprojdlg, CDIALOG)

// {{AFX_MSG_MAP (CVCTestVCComprojdlg)

ON_WM_SYSCOMMAND ()

ON_WM_PAINT ()

ON_WM_QUERYDRAGICON ()

ON_BN_CLICKED (IDC_Button1, Onbutton1)

//}} AFX_MSG_MAP

END_MESSAGE_MAP ()

...

Void cvctestvccomprojdlg :: onbutton1 ()

{

// Todo: Add Your Control Notification Handler Code Here

HRESULT HR = Coinitialize (NULL); // Initialization COM library

CLSID CLSID;

HR = CLSIDFROMPROGID (OLESTR ("ValidatecreditCardServer.validateCard"), & clsid); // Get the server's CLSID

Ivalidatecard * cardptr;

HR = CocreateInstance (CLSID, NULL, CLSCTX_INPROC_SERVER, __UIDOF (IVALIDATECARD), (LPVOID *) & Cardptr);

IF (Failed (HR))

{

AfxMessageBox ("Server Creation Failed");

Return;

} // Create an instance of COM server components

Char ccardnum [20], ccardtype [10];

Getdlgitemtext (IDC_EDit1, ccardnum, 20);

Getdlgitemtext (IDC_EDit2, ccardtype, 10);

_BSTR_T BSTCARDNUM (CCardNum), BstcardType (ccardtype);

Long Lstatus;

LSTATUS = Cardptr-> Validatecreditcard (bstcardnum, bstcardtype);

IF (LSTATUS == 1)

AfxMessageBox ("Valid Card");

Else

AfxMessageBox ("invalid card"); // uses COM objects

Couninitialize (); // Terminate COM library

}

8. Cut the running test procedure.

n code test: the same (omitted).

Author Blog:

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

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

New Post(0)