E
Enrico Pangan
I'm trying to call some functions in a C++ Dll, "Library.dll" from C#.
Some functions work but some return the NullReferenceException. I have
here the source code for the C++ version and for the C# version. The
C++ version works while the C# version returns the
NullReferenceException on a call to Connect().
The Open() and Close() functions works on both C++ and C# version. The
Connect() function only works using C++.
I hope you guys can help me. I'm stuck.
P.S. I don't have the source code for Library.dll
//
// C++ version (working properly)
//
// 1. open
typedef int (CALLBACK* LPOPEN)(
IN OUT LPHANDLE pHandle,
IN LPDWORD pStatus
);
LPOPEN Open;
// 2. connect
typedef int (CALLBACK* LPCONNECT)(
IN HANDLE handle,
IN OUT LPDWORD pStatus
);
LPCONNECT Connect;
// 3. close
typedef int (CALLBACK* LPCLOSE)(
IN HANDLE handle
);
LPCLOSE Close;
// main
void main()
{
HINSTANCE hInstance = LoadLibrary(TEXT("Library.dll"));
if(NULL != hInstance)
{
Open = (LPOPEN) ::GetProcAddress(hInstance, "Open");
Connect = (LPCONNECT) ::GetProcAddress(hInstance, "Connect");
Close = (LPCLOSE) ::GetProcAddress(hInstance, "Close");
HANDLE handle = 0xFFFFFFFF;
unsigned long status = 0;
Open(&handle, &status); // get handle (successful)
Connect(handle, &status); // get status (successful)
Close(handle);
FreeLibrary(hInstance);
}
}
//
// C# version (System.NullReferenceException on Connect())
//
// 1. open
[DllImport("Library.dll")]
unsafe public static extern int Open(
[In, Out] ref long pHandle,
[In] ref ulong pStatus);
// 2. connect
[DllImport("Library.dll")]
unsafe public static extern int Connect(
[In] long handle,
[In, Out] ref ulong pStatus);
// 3. close
[DllImport("Library.dll")]
unsafe public static extern int Close(
[In] long handle);
// main
namespace Test
{
public class Application
{
[System.STAThread]
static void Main()
{
unsafe
{
long handle = 0xFFFFFFFF;
ulong status = 0;
Open(ref handle, ref status); // get handle
(successful)
Connect(handle, ref status); // get status
(NullReferenceException)
Close(handle);
}
}
}
}
Some functions work but some return the NullReferenceException. I have
here the source code for the C++ version and for the C# version. The
C++ version works while the C# version returns the
NullReferenceException on a call to Connect().
The Open() and Close() functions works on both C++ and C# version. The
Connect() function only works using C++.
I hope you guys can help me. I'm stuck.
P.S. I don't have the source code for Library.dll
//
// C++ version (working properly)
//
// 1. open
typedef int (CALLBACK* LPOPEN)(
IN OUT LPHANDLE pHandle,
IN LPDWORD pStatus
);
LPOPEN Open;
// 2. connect
typedef int (CALLBACK* LPCONNECT)(
IN HANDLE handle,
IN OUT LPDWORD pStatus
);
LPCONNECT Connect;
// 3. close
typedef int (CALLBACK* LPCLOSE)(
IN HANDLE handle
);
LPCLOSE Close;
// main
void main()
{
HINSTANCE hInstance = LoadLibrary(TEXT("Library.dll"));
if(NULL != hInstance)
{
Open = (LPOPEN) ::GetProcAddress(hInstance, "Open");
Connect = (LPCONNECT) ::GetProcAddress(hInstance, "Connect");
Close = (LPCLOSE) ::GetProcAddress(hInstance, "Close");
HANDLE handle = 0xFFFFFFFF;
unsigned long status = 0;
Open(&handle, &status); // get handle (successful)
Connect(handle, &status); // get status (successful)
Close(handle);
FreeLibrary(hInstance);
}
}
//
// C# version (System.NullReferenceException on Connect())
//
// 1. open
[DllImport("Library.dll")]
unsafe public static extern int Open(
[In, Out] ref long pHandle,
[In] ref ulong pStatus);
// 2. connect
[DllImport("Library.dll")]
unsafe public static extern int Connect(
[In] long handle,
[In, Out] ref ulong pStatus);
// 3. close
[DllImport("Library.dll")]
unsafe public static extern int Close(
[In] long handle);
// main
namespace Test
{
public class Application
{
[System.STAThread]
static void Main()
{
unsafe
{
long handle = 0xFFFFFFFF;
ulong status = 0;
Open(ref handle, ref status); // get handle
(successful)
Connect(handle, ref status); // get status
(NullReferenceException)
Close(handle);
}
}
}
}