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 C++ pointers functions

Status
Not open for further replies.

Denyson

Programmer
Mar 20, 2001
19
BR
Hi,

Is there any problem to call in VB 6.0 a pointer function from a DLL created in VC++ 6.0?

C++ DLL Function exported:

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

Visual Basic Declaration:
Private Declare Sub IMPCAR Lib "C:\Meus documentos\AME\Visual C\Call_DLL\IMPCAR.DLL" (udtData As icPar)

Visual basic Call:

Call IMPCAR(ParMain)

Is there any error?

Thanks
 
Data passed ByRef (the default) is passed with a pointer. Bata passed ByVal in actually passed as the data itself. You should be able to pass in a structure (you need to define the structure in VB first though) ByRef and C++ should accept its pointer. (if this is wrong, then please correct me . . . my C++ is a bit rusty, but this is how I remember it working). - Jeff Marler B-)
 
when you are working with a Function Pointer, you do not want to link into the dll, passing byVal is advisible when passing from VB to the DLL. that way a reference of something in VB is not altered while in a VC++ dll Karl Blessing aka kb244{fastHACK}
kblogo.jpg
 
Karl,
Although you raise a valid point when you stated that passing data ByVal keeps you data safe from functions (i.e. the function can not alter your data) that it is passed into, you can't alway pass data ByVal to a C++ dll. If it is expecting a memory address (i.e. a pointer) and you pass it the raw data, it is going to get cranky. Typically, I have always seen UDT passed in ByRef (a pointer to the actual structure in memory holding the data). For example, consider the CreateProcess API (declaration pulled from API Viewer).


Public Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long


The SECURITY_ATTRIBUTES structure is passed in ByRef. If you change the declaration to ByVal, you will most likly get a GPF or some similar error.
- Jeff Marler B-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top