|
Calling a VB ActiveX DLL from a MFC Client
Author: Amit Dey In this article I'll present a way of calling a VB ActiveX DLL from a MFC client application. There are other ways to do so but I find this by far the easiest. I shall briefly point out the steps you need to follow-
First create a new ActiveX DLL project using VB 6.0. Name the project prjdll and the class clsdll. Add a new function fnCallDll to the class. My function just displays a messagebox and looks like
Public Function fnCallDll()
MsgBox "VB ActiveX DLL"
End Function
Save and compile this project to create prjdll.dll. This is our server component. Now we are going to develop the client. Create a new dialog based application in VC++ using MFC Appwizard and save the project. Next we are going to import the server component's type library using
the
# import "prjdll.dll"
using namespace prjdll;
You must add the above code after
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately
and before
#endif
in the stdafx.h file.Importing the prjdll.dll file helps the compiler to link to the dll's type library at runtime.The #import tells the compiler to generate the wrapper class, which will encapsulate the functionalities of the server component. If the server component is created using VB we need the import the associated .dll file and if the component is created using VC++, we need to import the .tlb file.The name of the wrapper class will be same as the server component name, by default. Compile the stdafx.cpp file. The compiler generates a .tlh and a .tli file in your projects Debug or Release directory(dependingon your configuration).These are the typelibrary header and implementation files. Open the .tlh file by double-clicking it and find out word immediately after the word namespace. This is usually the name of the project that we earlier created using VB.Look at the code we inserted earlier to the stdafx.h file.The using_namespace is required so that we can access the server's methods. Place a new button control (IDC_BUTTON1) on the dialog. Double-click the control to add a command handler OnButton1() for the button. Now add the following code in the handler:
HRESULT hresult;
CLSID clsid;
CoInitialize(NULL); //initialize COM library
hresult=
CLSIDFromProgID(OLESTR("prjdll.clsdll"),&clsid);
_clsdll *t;
hresult=CoCreateInstance(clsid,NULL,CLSCTX_INPROC_SERVER,__uuidof(_clsdll),(LPVOID *) &t);
if(FAILED(hresult))
{
AfxMessageBox("Creation Failed");
return;
}
t->fnCallDll (); //call method
CoUninitialize(); //Unintialize the COM library
The name of the CoClass is _clsdll. The CoCreateInstance function returns the address of the interface pointer requested. Now the pointer t can happily be used to access the functionality of the server component. That's it. On clicking the button a Messagebox should pop up. About the Author Amit is a final sem student at a software institute at Bangalore,India. He's been into VC++/MFC/ATL programming for a 3+ years . He's also a guitarist/keyboard player and jam up with hish buddies on weekends. He would love to hear about some job oppurtunities. Contribute to IDR: To contribute an article to IDR, a click here.
|
|