Click here to Skip to main content
12,300,191 members (60,710 online)
Click here to Skip to main content
Add your own
alternative version

Stats

423.6K views
2.4K downloads
44 bookmarked
Posted

Calling a VB ActiveX DLL from a MFC Client

, 13 Sep 2000
Rate this:
Please Sign up or sign in to vote.
A simple way to call a VB ActiveX DLL from a VC/MFC Client
<!-- Download Links -->
  • Download source files - 30 Kb
  • <!-- Main HTML starts here -->

    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

    Share

    About the Author

    Amit Dey
    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


    You may also be interested in...

    Comments and Discussions

     
    QuestionCan we run the above vc++ application by replacing the existing dll with Updated dll (add a new public function) without builing the vc++ application Pin
    rameshbabu_peddapalli15-Oct-08 19:15
    memberrameshbabu_peddapalli15-Oct-08 19:15 
    QuestionHow to create Dll in VB dotnet. Pin
    ashokrana00718-Sep-08 7:30
    memberashokrana00718-Sep-08 7:30 
    QuestionWhat about the events ? Pin
    DevMan7730-Jul-08 3:43
    memberDevMan7730-Jul-08 3:43 
    QuestionHow to pass CString Array in VC to a VB programmed DLL? Pin
    urwangshd23-Nov-07 9:27
    memberurwangshd23-Nov-07 9:27 
    QuestionAnybody knows, How to dynamically include the dll Pin
    JothiMurugeswaran15-Nov-07 19:40
    memberJothiMurugeswaran15-Nov-07 19:40 
    QuestionDoubt in loading a dll in VC++ Project Pin
    ganesa moorthy3-Jul-07 6:52
    memberganesa moorthy3-Jul-07 6:52 
    AnswerRe: Doubt in loading a dll in VC++ Project Pin
    JothiMurugeswaran20-Nov-07 0:37
    memberJothiMurugeswaran20-Nov-07 0:37 
    GeneralString And ByRef Parameters Pin
    Mazdisna meenavee mehr19-Jun-07 8:41
    memberMazdisna meenavee mehr19-Jun-07 8:41 
    GeneralCalling a COM DLL from MFC Client Pin
    Sabareeswaran28-Dec-06 23:28
    memberSabareeswaran28-Dec-06 23:28 
    Generaldiff bw ax & com dll Pin
    GANsJob26-Dec-06 2:15
    memberGANsJob26-Dec-06 2:15 
    QuestionCreation Failed Pin
    xtiancorrea3-Sep-05 21:18
    memberxtiancorrea3-Sep-05 21:18 
    AnswerRe: Creation Failed Pin
    RivF22-Dec-05 8:49
    memberRivF22-Dec-05 8:49 
    AnswerRe: Creation Failed Pin
    Elmue8-Aug-07 4:21
    memberElmue8-Aug-07 4:21 
    QuestionRe: Creation Failed Pin
    11166628-Aug-07 18:20
    member11166628-Aug-07 18:20 
    AnswerRe: Creation Failed Pin
    potesanj3-Oct-07 6:12
    memberpotesanj3-Oct-07 6:12 
    GeneralRe: Creation Failed Pin
    11166628-Aug-07 19:26
    member11166628-Aug-07 19:26 
    Generalproblem with dll create in C# Pin
    MihaiChioariu10-Aug-05 6:19
    memberMihaiChioariu10-Aug-05 6:19 
    GeneralNumber of arguments error Pin
    Shenthil26-Jul-05 1:43
    memberShenthil26-Jul-05 1:43 
    Generalcalling vc++ dll from vb dll Pin
    Anonymous26-Apr-05 18:26
    sussAnonymous26-Apr-05 18:26 
    GeneralCreate an ActiveX object in VC++ Pin
    Anonymous13-Apr-05 7:34
    memberAnonymous13-Apr-05 7:34 
    GeneralTrying to open workspace .dsw gives the error &quot; &quot;This makefile was not generated by developer studio&quot; Pin
    Mohamed Jaffar Sagir16-Mar-05 22:14
    sussMohamed Jaffar Sagir16-Mar-05 22:14 
    GeneralRe: Trying to open workspace .dsw gives the error &quot; &quot;This makefile was not generated by developer studio&quot; Pin
    Mohamed Jaffar Sagir16-Mar-05 22:49
    memberMohamed Jaffar Sagir16-Mar-05 22:49 
    GeneralTo send a string from VC++ dll to VB app using SendMessage Pin
    Mohamed Jaffar Sagir30-Jan-05 18:05
    memberMohamed Jaffar Sagir30-Jan-05 18:05 
    GeneralRe: To send a string from VC++ dll to VB app using SendMessage Pin
    prosantalangtha15-Nov-05 14:47
    memberprosantalangtha15-Nov-05 14:47 
    Generalplease help me Pin
    groom hellscream26-Sep-04 3:06
    membergroom hellscream26-Sep-04 3:06 
    Generalafter recompiling the vbproject it doesnot work Pin
    purusingh11-Aug-04 23:14
    memberpurusingh11-Aug-04 23:14 
    GeneralRe: after recompiling the vbproject it doesnot work Pin
    Elmue8-Aug-07 6:12
    memberElmue8-Aug-07 6:12 
    QuestionHow do I correctly retrieve function return values Pin
    Pavel Brun5-Aug-04 5:38
    memberPavel Brun5-Aug-04 5:38 
    GeneralHere's the correct way Pin
    greggdavis7-Jun-04 21:26
    membergreggdavis7-Jun-04 21:26 
    GeneralThe same thing can also be done by adding one wrapper class by Class Wizard (Ctrl+W) Pin
    Jigar Mehta29-Apr-04 23:00
    memberJigar Mehta29-Apr-04 23:00 
    GeneralRemote Activation Pin
    Anonymous10-Feb-04 4:54
    sussAnonymous10-Feb-04 4:54 
    GeneralAccessing a dll variable Pin
    siddharths24-Jan-04 0:35
    membersiddharths24-Jan-04 0:35 
    GeneralTrying to call an VB ActiveX DLL from a VC++ DLL Pin
    Dan Amarandei5-Nov-03 12:22
    memberDan Amarandei5-Nov-03 12:22 
    GeneralRe: Trying to call an VB ActiveX DLL from a VC++ DLL Pin
    Anonymous7-Nov-03 6:14
    sussAnonymous7-Nov-03 6:14 
    GeneralCollectionPtr - Pin
    pas_ascom18-Sep-03 5:14
    memberpas_ascom18-Sep-03 5:14 
    GeneralRe: CollectionPtr - Pin
    Anonymous23-Oct-03 13:53
    sussAnonymous23-Oct-03 13:53 
    GeneralRe: CollectionPtr - Pin
    pas_ascom24-Oct-03 5:44
    memberpas_ascom24-Oct-03 5:44 
    GeneralRe: CollectionPtr - Pin
    pas_ascom24-Oct-03 5:47
    memberpas_ascom24-Oct-03 5:47 
    GeneralLoading a form instead of a msgbox and using the controlls e.g. listbox etc. Pin
    arvindIT200311-Sep-03 12:19
    memberarvindIT200311-Sep-03 12:19 
    GeneralRe: Loading a form instead of a msgbox and using the controlls e.g. listbox etc. Pin
    Amit Dey12-Sep-03 21:59
    memberAmit Dey12-Sep-03 21:59 
    GeneralPassing a value to VB DLL function Pin
    asierra2-Sep-03 14:25
    memberasierra2-Sep-03 14:25 
    GeneralRe: Passing a value to VB DLL function Pin
    asierra3-Sep-03 5:48
    memberasierra3-Sep-03 5:48 
    GeneralCalling a VB ActiveX DLL from ASP page Pin
    Azghar Ahmed Baig22-Aug-03 20:37
    memberAzghar Ahmed Baig22-Aug-03 20:37 
    GeneralProblems with the source code Pin
    Faisal_tuhh24-Jul-03 0:56
    memberFaisal_tuhh24-Jul-03 0:56 
    GeneralRe: Problems with the source code Pin
    mbridge23-Sep-03 12:34
    membermbridge23-Sep-03 12:34 
    GeneralRe: Problems with the source code Pin
    Ben02-Dec-03 8:21
    memberBen02-Dec-03 8:21 
    GeneralRe: Problems with the source code Pin
    liweiye8-Dec-03 14:41
    memberliweiye8-Dec-03 14:41 
    GeneralDoing this in .NET Pin
    Anonymous15-May-03 12:52
    sussAnonymous15-May-03 12:52 
    GeneralRe: Doing this in .NET Pin
    Gernot Frisch25-Aug-03 4:35
    memberGernot Frisch25-Aug-03 4:35 
    GeneralCommunicating with VB-created ActiveX-DLL Pin
    P.O. Arnäs2-Apr-03 4:45
    sussP.O. Arnäs2-Apr-03 4:45 

    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.

    | Advertise | Privacy | Terms of Use | Mobile
    Web02 | 2.8.160525.2 | Last Updated 14 Sep 2000
    Article Copyright 2000 by Amit Dey
    Everything else Copyright © CodeProject, 1999-2016
    Layout: fixed | fluid