Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Dll's written in VB in C/C++

Status
Not open for further replies.

Stovio

Programmer
Mar 20, 2002
30
0
0
SE
How do i load an Active X dll and call its methods in C/C++? I'm greatful for sample code, webpages, tutorials or help of other kinds.
 
Here is a quick example of how to access a VB ActiveX class from VC++. Com components are intended to be language independent so the code here can be used with any com component. There are a lot of issues regarding com but this should provide a good start.
Beowulf



vb Active X dll (vbcomtest.dll) with a class called Class1:

Class1 code:

Public Function afunc(ByVal val As Long) As Long
afunc = val * 2
End Function


Win32 console app:

// Example use of vb com components
// Beowulf

#include "stdafx.h"
#include <objbase.h>
//import the VB dll - This will provide wrappers which will be more intuative to a VB programer
#import &quot;vbcomtest.dll&quot; no_namespace, named_guids

int main(int argc, char* argv[])
{

//Initalise COM
HRESULT hr = CoInitialize(NULL);
if( FAILED( hr ) )
{
printf(&quot;Failed to CoInitialize\n&quot;);
}

//get a smart pointer
//The main interface is called _Class1 not
//IClass1 as is common practice - the &quot;_&quot; is to prevent the
//VB development enviroment from displaying default interface
_Class1Ptr ptrMyClass;

//Create instance of Class1 - note the CLSID_Class1
hr = ptrMyClass.CreateInstance(CLSID_Class1,NULL);
if( FAILED( hr ) )
{
printf(&quot;Failed to CreateInstance\n&quot;);
}

//use wapped afunc(...)
long lRes = ptrMyClass->afunc(23);
printf(&quot;result = %d\n&quot;, lRes);

//release object
ptrMyClass.Release();

//Uninitalise COM
CoUninitialize();

return 0;
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top