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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MSVC++ DLL for VB

Status
Not open for further replies.

ryan

Programmer
Nov 13, 2000
73
US
Could anyone post some sample code on how to make a MSVC++ DLL function workable as an API call from VB?

Thanks!
 
Start with declaring your exported function as extern "C" so that the C++ name mangling doesn't occur. Next, all strings passed/accepted must be ANSI, not Unicode (IOW, VB can't call Unicode versions of functions)

Then inside VB, define the function prototype as:
[tt]
Private Declare Function MyFunction lib "MyDLL.DLL" (ByRef FirstParameter as Long, ByVal SecondParameter as Long) as boolean
[/tt]
where:
"Private" refers to the visibility of the prototype.
"MyFunction" is your exported function name.
"ByRef" and "ByVal" control whether the contents of a parameter can be modified

Note that strings in VB must always be passed ByVal, otherwise you get the address of the BSTR, and not a pointer-to-char.

You can also create subroutines (C functions that don't return a variable) besides functions. Because of the BSTR/PCHAR problem, VB has a tough time calling functions that return a string, so try and make string return values part of the parameter list.

Datatypes in VB are pretty close to C, except that they're always unsigned, and a "int" in C is a "Long" in VB. A "short" in C is a "Integer" in VB.

Chip H.
 
hehe thanks but I meant from C++. I know how to get the values from VB. This helps though.

Thanks!
 
Ryank -

So you want to call a VB DLL from C++? I must have mis-read your earlier post then, as I thought you wanted to go the other way.

In this case, take a look at MS knowledgebase article Q200150, available at:
This demonstrates how to call a VB Active-X DLL from a C++ program.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top