DLLImport System.NullReferenceException

  • Thread starter richard.brenner
  • Start date
R

richard.brenner

Hi There.

I try to implement methos from an unmanaged DLL in C# to get access to
an external device.
I have a problem with the following C method:

char* __stdcall RDR_SendCommandGetData(void* hReader, char* command,
char* data, char* buffer);

My C# counterpart:

[DllImport("Reader.dll",CharSet=CharSet.Ansi,SetLastError=true)]
internal static extern string RDR_SendCommandGetData(IntPtr hReader,
string command, string data,StringBuilder buffer);


I get an error at the following call:

output=ReaderBinding.RDR_SendCommandGetData(reader,command,commandvalue,buffer);

The error is:
An unhandled exception of type 'System.NullReferenceException'
occurred in testapp.exe
Additional information: Object reference not set to an instance of an
object

In order to get and receive data, I previously have to open the Reader
device and receive the handle.

The methods are:
C:
void* __stdcall OpenComm(char* commDevice, char autodetect, struct
presetSettings* settings);

C#:
[DllImport("Reader.dll",SetLastError=true,CharSet=CharSet.Ansi)]
internal static extern IntPtr RDR_OpenComm(System.String device,int
autodetect, presetSettings settings);


call:
IntPtr reader;
reader=ReaderBinding.RDR_OpenComm("COM5",1,null);

I tried setting some ref and out attributes but it doesn't change the
error.

Please help.
Thanks.
 
R

Richard

Hi There.

I try to implement methos from an unmanaged DLL in C# to get access to
an external device.
I have a problem with the following C method:

char* __stdcall RDR_SendCommandGetData(void* hReader, char* command,
char* data, char* buffer);

My C# counterpart:

[DllImport("Reader.dll",CharSet=CharSet.Ansi,SetLastError=true)]
internal static extern string RDR_SendCommandGetData(IntPtr hReader,
string command, string data,StringBuilder buffer);

I get an error at the following call:

output=ReaderBinding.RDR_SendCommandGetData(reader,command,commandvalue,buf­fer);

The error is:
An unhandled exception of type 'System.NullReferenceException'
occurred in testapp.exe
Additional information: Object reference not set to an instance of an
object

In order to get and receive data, I previously have to open the Reader
device and receive the handle.

The methods are:
C:
void* __stdcall OpenComm(char* commDevice, char autodetect, struct
presetSettings* settings);

C#:
[DllImport("Reader.dll",SetLastError=true,CharSet=CharSet.Ansi)]
internal static extern IntPtr RDR_OpenComm(System.String device,int
autodetect, presetSettings settings);

call:
IntPtr reader;
reader=ReaderBinding.RDR_OpenComm("COM5",1,null);

I tried setting some ref and out attributes but it doesn't change the
error.

Please help.
Thanks.

Hi. The problem didn't come from this methods. I've found the problem.
 

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

Top