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!

Calling a C++ DLL from VB 6

Status
Not open for further replies.

Denyson

Programmer
Mar 20, 2001
19
BR
I made a DLL (IMPCAR.DLL) using Microsoft Visual C++ 6.0. Now, I´m trying to call the DLL from the Visual Basic 6.0.
My DLL only has an exported function:

extern "C" _declspec(dllexport) icPar *IMPCAR(icPar *ParGlobal);

where:
icPar is a struct.

Please, anybody could me give an example how to declare and call this function from the IMPCAR.DLL?

Thanks
 
First of all, you'll need to write a corresponding structure definition (for your icPar) in VB using the Type keyword. Then, if you set the compiler switches correctly when you made the DLL, it should be as simple as this . . .


private Type icPar
...
...
end type

Private Declare Function IMPCAR Lib "IMPCAR.DLL" ( udtData As icPar) As icPar


The exported name (IMPCAR) may be different depending upon what compiler switches you use . . . you may need to use Quickview or Dumpbin to get the export table. Also, be sure the DLL is somewhere that your app can see it . . . either in the application path or the system directory or the windows directory . . . otherwise, you will need to hardcode the Lib path in the declare statement. - Jeff Marler B-)
 
here is my reply from your Mar 22 post


I dont know if you have yet, but the DLL written in VC must have a .def file

(side note... the ; behaves like a comment in the .def file)

***************************************************
; sample .def file
LIBRARY <dll name without the .dll extention and no &quot;<&quot; or &quot;>&quot;>

CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD MOVEABLE

EXPORTS
IMPCAR

***************************************

This will export the IMPCAR function.

NEXT, with a struct, there MAY be padding issues so make sure there is a 1 to 1 correspondence in the size of the struct defined in C++ and your TYPE defined in VB.

in VB you will want to add:

PRIVATE DECLARE FUNCTION IMPCAR LIB &quot;YOUR_DLL.dll&quot; Alias &quot;IMPCAR&quot;(byVal icPar as YOUR_TYPE) as YOUR_TYPE

*****************************

There may be a few bugs in it but this should get you well on your way into incorporating the dll into VB

Matt

 
Like Matt said -- the __declspec(dllexport) won't do it -- you have to use a .DEF file.

Note that to make it easy on yourself, strings inside a structure ought to be of fixed-length (use char MyString[xx], not char *MyString). Otherwise you can end up with memory leaks, string-terminator problems, and your VB program having to de-reference the string (yuck).

Chip H.
 
Actually, I have made C++ DLLs that exported APIs that were callable from VB without a .DEF file. The only catch is that you need to look at the export table to get the actual Function Name . . . For instance, a function call GetName might be called &quot;_GetName@4&quot;. If you use the correct compiler switches it will work. - Jeff Marler B-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top