LPSTR -> String : Type Incompatibility Issue

  • Thread starter Thread starter Michael Chong
  • Start date Start date
M

Michael Chong

Another same issue on "NullReferenceException" :-
I have an (exe) executable program created in VB.NET 2003 that calls to a
MFC DLL written in VC++.NET 2003. I always get an error msg
"NullReferenceException: Object Reference Not Set to an Instance of an
Object" when my exe calls the following codes:

in VB.NET

Declare Function test Lib "C:\Cyob\IOComm\Debug\IOComm.dll" _
(ByVal a As Long, ByRef b As String) As Integer

Dim did As Integer
Dim ret As Integer

ret = test(PortHnd, did) //ERROR: Object Reference Not Set to an
Instance of an Object


in VC++.NET

extern "C" int APIENTRY test(HANDLE a, LPSTR b)
{
strcpy(b, "hello"); //ERROR: Object Reference Not Set to an
Instance of an Object
return 99;
}


Why is this happening? Any idea in solving such matter?
Thanks in Advance

Michael.
 
Sorry, the "Dim did as Integer" should be "Dim did as String"
I believe is DataType issue from VC++.NET (LPSTR) pass to VB.NET (String)

When I try to replace String with IntPtr, it doesn't give me error but the
return value for "did variable" is some numbers.
Plz help! TQ
 
Michael,

It looks like you haven't allocated memory to copy the
"hello" to in your strcpy call.

Try putting

b = new CHAR[strlen("hello") + 1];

before your call to strcpy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top