dll import

J

Jason

I have created a dll from a C++ class using .Net and have imported it into my
C# applications (one webservice, one standalone app). It works fine for a
while, but will eventually fail to produce the error
"System.NullReferenceException: Object reference not set to an instance of an
object" when calling a method from the dll. In the case of the webservice,
after it fails once it continues to fail until I reboot. My searches have
been unsuccessful in finding a solution. I am using XP Pro with IIS 5.1 for
the webservice.

The only suggestion I have found through search is to check the permissions,
which I have done, but I could probably use some more detailed instructions
on how to do this.

Any suggestions on how I might fix this problem?



The code:

DLL call that produces the error:
IntPtr pMSG = MSG_Class.CreateMSGClass();

and also:
byte[] Message = new byte[MSG_Class.GetBufferSize(pMSG)];
MSG_Class.GetBuffer(pMSG , Message); // this fails occasionally


import:
DLL Import class:
[DllImport("MSG_1.dll")]
public static extern IntPtr CreateMSGClass ();

[DllImport("MSG_1.dll",CallingConvention=CallingConvention.ThisCall)]
public static extern void GetBuffer( IntPtr instance, byte [] bufferOut );


C++ (from which the dll was created):
CreateMSGClass:
return new MSGClass();

void MSG_Class::GetBuffer(char * BufferOut) {
memcpy(BufferOut, TheBuffer, sizeof(TheBuffer);
}
 
D

Dale Preston

You should be able to easily determine what is null when it should not be by
testing for null before using your objects. What has your debugging told
you, if anything?

Dale Preston
 
J

Jason

Debugging has not produced any information that seems helpful. My byte array
is created successfully and the IntPtr contains a seemingly valid value after
the CreateMSGClass method executes successfully.

I think I may be misunderstanding something basic about dlls. The error
that was previously occurring when instantiating the class from the dll
seemed to have stopped once I made the IntPtr to the class a member variable
of my C# class (instead of a variable local to the class method in which it
was being used) and removed the second instance of the class. I was
previously trying to create multiple instances of the class. Should I be able
to do this?
For example:
IntPtr pMSG1 = MSG_Class.CreateMSGClass();
IntPtr pMSG2 = MSG_Class.CreateMSGClass();

The class packaged in the DLL contains a pointer to a char array member
which is modified (and sometimes resized) through set accessors member
functions. Is this ok to do?

The failing GetBuffer() method is copying this array into the
pre-initialized byte array which I have verified is created successfully in
the C# code. The method is contained within the dll so, as far as I know, I
cannot step into that code to see what the data is set to, or verfiy that the
array is passed in successfully. Is there any way to do this or is there any
other way to figure out if something inside a method in the dll is set to
null?


Thanks,
Jason





Dale Preston said:
You should be able to easily determine what is null when it should not be by
testing for null before using your objects. What has your debugging told
you, if anything?

Dale Preston

Jason said:
I have created a dll from a C++ class using .Net and have imported it into my
C# applications (one webservice, one standalone app). It works fine for a
while, but will eventually fail to produce the error
"System.NullReferenceException: Object reference not set to an instance of an
object" when calling a method from the dll. In the case of the webservice,
after it fails once it continues to fail until I reboot. My searches have
been unsuccessful in finding a solution. I am using XP Pro with IIS 5.1 for
the webservice.

The only suggestion I have found through search is to check the permissions,
which I have done, but I could probably use some more detailed instructions
on how to do this.

Any suggestions on how I might fix this problem?



The code:

DLL call that produces the error:
IntPtr pMSG = MSG_Class.CreateMSGClass();

and also:
byte[] Message = new byte[MSG_Class.GetBufferSize(pMSG)];
MSG_Class.GetBuffer(pMSG , Message); // this fails occasionally


import:
DLL Import class:
[DllImport("MSG_1.dll")]
public static extern IntPtr CreateMSGClass ();

[DllImport("MSG_1.dll",CallingConvention=CallingConvention.ThisCall)]
public static extern void GetBuffer( IntPtr instance, byte [] bufferOut );


C++ (from which the dll was created):
CreateMSGClass:
return new MSGClass();

void MSG_Class::GetBuffer(char * BufferOut) {
memcpy(BufferOut, TheBuffer, sizeof(TheBuffer);
}
 

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

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top