Click here to Skip to main content
6,634,665 members and growing! (16,216 online)
Email Password   helpLost your password?
General Programming » DLLs & Assemblies » General     Intermediate

Calling a VB ActiveX DLL from a MFC Client

By Amit Dey

A simple way to call a VB ActiveX DLL from a VC/MFC Client
VC6Win2K, MFC, Dev
Posted:5 Sep 2000
Updated:13 Sep 2000
Views:300,574
Bookmarked:37 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
50 votes for this article.
Popularity: 7.22 Rating: 4.25 out of 5
2 votes, 7.7%
1

2
2 votes, 7.7%
3
2 votes, 7.7%
4
20 votes, 76.9%
5
  • 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

    About the Author

    Amit Dey


    Member
    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


    Occupation: Web Developer
    Location: India India

    Other popular DLLs & Assemblies articles:

    Article Top
    You must Sign In to use this message board.
    FAQ FAQ 
     
    Noise Tolerance  Layout  Per page   
     Msgs 1 to 25 of 163 (Total in Forum: 163) (Refresh)FirstPrevNext
    GeneralDisplaying modeless window in VC++ from VB Pinmembermachnotrix4:15 20 Jan '09  
    GeneralRe: Displaying modeless window in VC++ from VB Pinmembermachnotrix5:28 20 Jan '09  
    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 Pinmemberrameshbabu_peddapalli20:15 15 Oct '08  
    QuestionHow to create Dll in VB dotnet. Pinmemberashokrana0078:30 18 Sep '08  
    GeneralWhat about the events ? PinmemberDevMan774:43 30 Jul '08  
    QuestionHow to pass CString Array in VC to a VB programmed DLL? Pinmemberurwangshd10:27 23 Nov '07  
    QuestionAnybody knows, How to dynamically include the dll PinmemberJothiMurugeswaran20:40 15 Nov '07  
    QuestionDoubt in loading a dll in VC++ Project Pinmemberganesa moorthy7:52 3 Jul '07  
    AnswerRe: Doubt in loading a dll in VC++ Project PinmemberJothiMurugeswaran1:37 20 Nov '07  
    GeneralString And ByRef Parameters PinmemberMazdisna meenavee mehr9:41 19 Jun '07  
    GeneralCalling a COM DLL from MFC Client PinmemberSabareeswaran0:28 29 Dec '06  
    Generaldiff bw ax & com dll PinmemberGANsJob3:15 26 Dec '06  
    QuestionCreation Failed Pinmemberxtiancorrea22:18 3 Sep '05  
    AnswerRe: Creation Failed PinmemberRivF9:49 22 Dec '05  
    AnswerRe: Creation Failed PinmemberElmue5:21 8 Aug '07  
    QuestionRe: Creation Failed Pinmember11166619:20 28 Aug '07  
    AnswerRe: Creation Failed Pinmemberpotesanj7:12 3 Oct '07  
    GeneralRe: Creation Failed Pinmember11166620:26 28 Aug '07  
    Generalproblem with dll create in C# PinmemberMihaiChioariu7:19 10 Aug '05  
    GeneralNumber of arguments error PinmemberShenthil2:43 26 Jul '05  
    Generalcalling vc++ dll from vb dll PinsussAnonymous19:26 26 Apr '05  
    GeneralCreate an ActiveX object in VC++ PinmemberAnonymous8:34 13 Apr '05  
    GeneralTrying to open workspace .dsw gives the error " "This makefile was not generated by developer studio" PinsussMohamed Jaffar Sagir23:14 16 Mar '05  
    GeneralRe: Trying to open workspace .dsw gives the error " "This makefile was not generated by developer studio" PinmemberMohamed Jaffar Sagir23:49 16 Mar '05  
    GeneralTo send a string from VC++ dll to VB app using SendMessage PinmemberMohamed Jaffar Sagir19:05 30 Jan '05  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 13 Sep 2000
    Editor: Chris Maunder
    Copyright 2000 by Amit Dey
    Everything else Copyright © CodeProject, 1999-2009
    Web22 | Advertise on the Code Project