DLL question -- integrating VB with C++ DLL
This looks like a good place to ask this type of question...
I'm having some weird problems trying to get a VB program to use a C DLL.
My VB program which calls the DLL is abending strangely, will errors like
the following:
"The instruction at "0x77f34cab" referenced memory at "0x001d7000". The
memory could not be read."
"Out of memory (Error 7)".
It is difficult to reproduce the problems every time. Sometimes they occur
immediately (after the first button click), sometimes they happen after
three clicks (each click invokes the DLL).
The DLL is basically a sockets interface written in C which talks to the
rest of my application. The VB part just calls the DLL and returns the
results. The program always blows up when calling erremotecmd (see below).
Trace code put into the DLL also indicates that everything processed
successfully and that a return code of 0 was passed back to the caller.
The DLL does no dynamic memory allocation on its own. I'm wondering if VB
does. The only other thing I could think of was that my DLL was overwriting
the memory of VB (the DLL does copy data into a string buffer provided by
VB). However, I don't think that is the case either, since I've statically
allocated string space in the VB program far in excess of what is needed
(and I've varied that amount to see if I had done too much).
One other thing I tried was writing the same code in C, and calling the DLL
from C. This works as expected, in other words, no problems at all.
C prototypes for DLL calls:
void __stdcall erremotecmdsetoption (int option_id, int option_val);
int __stdcall erremotecmd (char *host, char *mgrport, char *request,
char *reply, int *replycnt, char *errmsg);
VB Code:
Private Declare Function erremotecmd Lib
"\eric\projects\dllcall\debug\ggscidll.dll" (ByVal host As String, ByVal
port As String, ByVal request As String, ByVal reply As String, ByRef
replycnt As Long, errmsg As String) As Long
Private Declare Sub erremotecmdsetoption Lib
"\eric\projects\dllcall\debug\ggscidll.dll" (ByVal option_id As Long, ByVal
option_val As Long)
Private Sub Command2_Click()
Dim res As Long
Dim request As String * 500
Dim reply As String * 5000
Dim errmsg As String * 500
Dim replycnt As Long
'turn tracing on
'Call erremotecmdsetoption(2, 1)
Call erremotecmdsetoption(1, 1)
'request = String(500, 0)
'reply = String(5000, 0)
'errmsg = String(500, 0)
'request = command.Text
res = erremotecmd(host.Text, port.Text, command.Text, reply, replycnt,
errmsg)
results.Text = reply
errortext.Text = errmsg
results2.SelStart = 0
results2.SelLength = replycnt
results2.SelFontName = "Courier"
results2.SelFontSize = 10
results2.TextRTF = Left(reply, replycnt)
End Sub