Click here to Skip to main content
15,884,473 members
Articles / Desktop Programming / MFC
Article

Calling a VB ActiveX DLL from a MFC Client

Rate me:
Please Sign up or sign in to vote.
4.63/5 (27 votes)
13 Sep 2000 531K   2.6K   44   161
A simple way to call a VB ActiveX DLL from a VC/MFC Client
  • Download source files - 30 Kb
  • Introduction

    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. Any information presented here is only for learning purposes.

    I shall briefly point out the steps you need to follow-

    • Create an ActiveX server component with VB. This is the ActiveX DLL you need to create with VB.
    • Create an dialog based MFC application using the MFC Appwizard.
    • Import the server's (DLL) type library into the MFC client app.
    • Initialize the COM library
    • Retrieve the CLSID of the server component.
    • Create an instance of the COM server component.
    • Use the COM object
    • Uninitialize the COM library

    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.

    We are going to import the server component's type library using the #import statement. Copy the prjdll.dll file to the directory where you have saved your MFC Appwizard project. Click the FileView tab of the Project Workspace window, expand the Header Files folder, open the file stdafx.h and add the following code (appears greyed)

    # 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 (depending on your configuration). These are the type library 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 codebutton 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);    //retrieve CLSID of component
    		
    _clsdll *t; 
    hresult=CoCreateInstance(clsid,NULL,CLSCTX_INPROC_SERVER,__uuidof(_clsdll),(LPVOID *) &t);
    if(FAILED(hresult))
    {
    	AfxMessageBox("Creation Failed");
    	return;
    }
    
    t->fnCallDll ();  //call method
    t->Release();   //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.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    Web Developer
    India India
    Amit Dey is a freelance programmer from Bangalore,India. Chiefly programming VC++/MFC, ATL/COM and PocketPC and Palm platforms. Apart from programming and CP, he is a self-taught guitar and keyboard player.

    He can be contacted at visualcdev@hotmail.com


    Comments and Discussions

     
    GeneralPassing parameter ByRef Pin
    Sco++14-Feb-03 12:59
    Sco++14-Feb-03 12:59 
    GeneralRe: Passing parameter ByRef Pin
    naturailec24-Mar-04 19:50
    naturailec24-Mar-04 19:50 
    GeneralRe: Passing parameter ByRef Pin
    Srikar Babu Sriram9-Jun-04 0:06
    Srikar Babu Sriram9-Jun-04 0:06 
    Generalusing lib file in my dll Pin
    vishalnikisharma10-Feb-03 0:21
    vishalnikisharma10-Feb-03 0:21 
    GeneralCalling a VB ActiveX EXE Pin
    AlonOren23-Jan-03 1:33
    AlonOren23-Jan-03 1:33 
    GeneralRe: Calling a VB ActiveX EXE Pin
    Anonymous23-Jan-03 7:33
    Anonymous23-Jan-03 7:33 
    GeneralLoading a form instead of a msgbox Pin
    Gary Kirkham9-Jan-03 5:57
    Gary Kirkham9-Jan-03 5:57 
    GeneralRe: Loading a form instead of a msgbox Pin
    Amit Dey11-Jan-03 6:54
    Amit Dey11-Jan-03 6:54 
    Thanks for sharing that with us,Gary.

    In a discussion in a postings in this list quite sometime back, it's mentioned that the VB form's HWND property could passed to the VC app(or viceversa) thru a method and the SetParent() API could be used to set form's parent.


    Hush,hush...
    thought I heard you call my name now.

    Kula Shaker.

    Amit Dey

    Latest articles at CP -
    PocketPC New menu
    Office addin


    GeneralRe: Loading a form instead of a msgbox Pin
    hgaf9-Apr-03 10:27
    hgaf9-Apr-03 10:27 
    GeneralRe: Loading a form instead of a msgbox Pin
    Gary Kirkham9-Apr-03 10:47
    Gary Kirkham9-Apr-03 10:47 
    GeneralRe: Loading a form instead of a msgbox Pin
    hgaf21-Apr-03 20:15
    hgaf21-Apr-03 20:15 
    GeneralRe: Loading a form instead of a msgbox Pin
    UW_COOP23-Jun-03 8:38
    UW_COOP23-Jun-03 8:38 
    GeneralRe: Loading a form instead of a msgbox Pin
    visualcdev@hotmail.com24-Jun-03 22:23
    sussvisualcdev@hotmail.com24-Jun-03 22:23 
    GeneralRe: Loading a form instead of a msgbox Pin
    UW_COOP25-Jun-03 2:04
    UW_COOP25-Jun-03 2:04 
    GeneralRe: Loading a form instead of a msgbox Pin
    Amit Dey26-Jun-03 1:00
    Amit Dey26-Jun-03 1:00 
    GeneralRe: Loading a form instead of a msgbox Pin
    UW_COOP26-Jun-03 3:26
    UW_COOP26-Jun-03 3:26 
    GeneralRe: Loading a form instead of a msgbox Pin
    Amit Dey26-Jun-03 10:47
    Amit Dey26-Jun-03 10:47 
    GeneralRe: Loading a form instead of a msgbox Pin
    UW_COOP27-Jun-03 2:10
    UW_COOP27-Jun-03 2:10 
    GeneralLoading a form instead of a msgbox and using the controlls e.g. listbox etc. Pin
    Anonymous11-Sep-03 12:07
    Anonymous11-Sep-03 12:07 
    GeneralcoCreateInstance Works only if the vb project is running Pin
    Anonymous23-Dec-02 4:01
    Anonymous23-Dec-02 4:01 
    GeneralRe: coCreateInstance Works only if the vb project is running Pin
    Anonymous23-Dec-02 9:54
    Anonymous23-Dec-02 9:54 
    GeneralRe: coCreateInstance Works only if the vb project is running Pin
    Anonymous23-Dec-02 20:59
    Anonymous23-Dec-02 20:59 
    GeneralReturned Value Pin
    Doni17-Dec-02 18:12
    Doni17-Dec-02 18:12 
    GeneralRe: Returned Value Pin
    Amit Dey18-Dec-02 5:50
    Amit Dey18-Dec-02 5:50 
    GeneralActive X DLL Pin
    Anonymous3-Dec-02 0:49
    Anonymous3-Dec-02 0:49 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.