Call a DLL with C-ish "int argc, char *argv[]"?

Call a DLL with C-ish "int argc, char *argv[]"?

Post by l.willm » Mon, 28 Mar 2005 01:22:00

There is a DLL written in C++ which I would like to call from VB6,
and which is declared this way:

extern "C"
BLATDLL_API int _stdcall Blat(int argc, char *argv[])

The integer argument count should be no problem, I'm not so sure
about argv: "argv is the pointer to a list of pointers to
arguments" says a comment in the C source. Could a VB array be passed
as parameter? Or what else should I do?

Lo Willms
/--------- XXXX@XXXXX.COM -- Alle Rechte vorbehalten --

Es gibt manche Leute die nicht eher hen bis man ihnen die Ohren abschneidet. -G.C.Lichtenberg

Call a DLL with C-ish "int argc, char *argv[]"?

Post by Jim Mac » Mon, 28 Mar 2005 08:53:30

You could simulate this by passing a UDT containing variable-length

Type ArgV
arg1 As String
arg2 As String
arg3 As String
argN As String
End Type

Pass an instance of this (ByRef) to the function. VB will take care of
converting the elements of the UDT to Ansi, so the result will be a
pointer to a list of pointers to null-terminated character arrays, or so
it will seem to the called function.

Give it a try :-)

And remember that "int argc" in C is 'ByVal argc As Long' in VB.


Jim Mack
MicroDexterity Inc


Call a DLL with C-ish "int argc, char *argv[]"?

Post by Matthew Ha » Fri, 01 Apr 2005 11:48:11

Couldn't you also just pass a really long string
to the function?

For example:
Declare Function Blat Lib "theLib" (ByRef argc _
As Long, ByVal argv As String) As Long

Dim sArgs() As String
Dim sArgV As String
Dim lArgc As Long, lRet As Long

str = String(1024&, Chr(255))
lRet = Blat(lArgc, sArgV)

sArgV = Left$(sArgV, InStr(sArgV, Chr(255)) - 1)
sArgs = Split(sArgV, Chr(0))

I might be wrong though. Just a thought. :-)

Matthew Hanna

Call a DLL with C-ish "int argc, char *argv[]"?

Post by Jim Mac » Fri, 01 Apr 2005 22:50:02

The function is expecting an array of pointers to strings, not a buffer
containing multiple strings. So what you're suggesting wouldn't work in
this case.

Of course, if he were writing the function he could choose to do what
you show, but as I read this he's dealing with an already-defined
function that expects the data a certain way.