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!

Memory could not be [read/written]

Status
Not open for further replies.

Malachi

Programmer
Aug 13, 2001
38
US
I have a piece of code giving me fits. I am attempting to call a DLL function, which should return a String to the calling environment.

Here is the DLL source code:
Code:
// MYDLL.DLL

#include <windows.h>

BOOL WINAPI DllMain( HINSTANCE hinstDll, DWORD fdwReason, LPVOID lpvReserved ) 
{

}

void MyDllFunction( char myString[20] )
{
    wsprintf( myString, &quot;%s&quot;, &quot;Hello, World&quot; );
    //MessageBox( NULL, myString, &quot;MyDllFunction Caption&quot;, MB_OK );
}

Here is the Module Declaration Statement:
Code:
Declare Sub MyDllFunction Lib &quot;D:\VisualBasicAPI\MYDLL.DLL&quot; (ByRef lpString As String)

The Form contains a Text Control and a Button Control. Here is the code associated with the Button control:
Code:
Private Sub Command1_Click()
    Dim TextMessage As String
    MyDllFunction (TextMessage)
    Text1.Text = TextMessage
End Sub


When I compile the DLL and copy it to the proper location, I then Start the Visual Basic program. When I press the Button I get the error:


The instruction at &quot;0x779d926&quot; referenced memory at &quot;0x57202c6b&quot;. The memory could not be &quot;read&quot;


If I pass the Visual Basic String variable ByRef then I get the error:

The instruction at &quot;0x779d926&quot; referenced memory at &quot;0x57202c6b&quot;. The memory could not be &quot;written&quot;


Can anyone tell me what the problem really is and the solution?

Thanks in advance

Brad

 
There are a couple of problems:

The main one is that the C function is expecting to be passed a 20-byte block of memory. However, you are attempting to pass a pointer to an unintialised variable-length string.

You'd probably be better off using LPCSTR MyString, or even const char *MyString, instead of char MyString[20]

Also, I don't know how your compiler is configured, or if you are using a MAP file. You may want to consider changing the C declaration to:

__declspec(dllexport) void MyDllFunction(...

In any case, as I said, you then try and pass a pointer to an uninitialised variable-length string from VB. This will cause pain, because the mangling that VB does to pass strings means that this is NOT the same as a pointer to a null string.

Try something like

Dim textMessage as String * 20

Also, either take the brackets out from the function call, or use the Call keyword:

eg

MyDllFunction TextMessage

or

Call MyDllFunction(TextMessage)
 
Thanks for the response. It gave me some insight to the VB string passing quirks. Passing a LPCSTR to wsprintf causes a warning so I didn't use it. I'm now assigning a LPCSTR variable a string value:

LPCSTR myString = &quot;Hello, World!&quot;;

and returning the myString to the calling environment.

In the VB routine I initialize:

Dim TextMessage As String

and pass it to my DLL Function:

TextMessage = MyDllFunction

I've declared my DLL Function in a Module as:

Declare Function MyDllFunction Lib &quot;D:\VisualBasicAPI\MYDLL\Debug\MYDLL.DLL&quot; ()

I also placed some MessageBox debug statements in the DLL code, which pop up messages indicating which Process/Thread stage I'm in. The pop up message appear as expected when I press the Button Control on the VB application. However, when the VB program attempts to call the DLL Function &quot;MyDllFunction&quot; using both Call MyDllFunction(TextMessage) and MyDllFunction TextMessage I get an error:

Bad DLL Calling Convention

Any Ideas to stop the error?

Thanks again!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top