By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,870 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,870 IT Pros & Developers. It's quick & easy.

Help:System.NullReferenceException with unmanaged memcpy

P: n/a
noe
I have done a DLL in unmanaged code C++ and in that dll I have defined
a function that use memcpy.This dll run ok.I use that dll in a
aplication in C# managed code in Visual Studio .NET. I import the
functions defined in the dll using DllImport and I have verified that
when in my dll I use memcpy then I have one error
:"System.NullReferenceException", and I don't known what happen?and I
must use memcpy in my dll.
Below is the definition of function in my DLL "DllWin32":
-------------------------------------------------------
DllExport bool Conectar()
{

HANDLE hAndle_ndisprotocol;
hAndle_ndisprotocol = CreateFile("\\\\.\\\\miprotocoldriverNDIS",
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,//share for reading and
writing
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) INVALID_HANDLE_VALUE);
dwErr = GetLastError();
//*******************
if (dwErr!=0)
{
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwErr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&buff,
0,
NULL);
//Display the string
MessageBox(NULL,(LPTSTR)buff,"Device.Error de
CreateFile",MB_ICONWARNING);
//Free the buffer
LocalFree(buff);
}
//*******************
//Bind the file handle to the driver
bl=DeviceIoControl(hAndle_ndisprotocol,
IOCTL_NDISPROT_BIND_WAIT,
NULL,
0,
NULL,
0,
&dwReturned,
NULL);
dwErr = GetLastError();
//*******************
if (dwErr!=0)
{
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwErr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&buff,
0,
NULL);
//Display the string
MessageBox(NULL,(LPTSTR)buff,"Device. Error llamada
IOCTL_NDISPROT_BIND_WAIT",MB_ICONWARNING);
//Free the buffer
LocalFree(buff);
}
//*******************
WCHAR *pwDeviceName;
CHAR Buf[1024];
PNDISPROT_QUERY_BINDING pQueryBinding;
pQueryBinding = (PNDISPROT_QUERY_BINDING)Buf;

bl = DeviceIoControl(hAndle_ndisprotocol,
IOCTL_NDISPROT_QUERY_BINDING,
pQueryBinding,sizeof(NDISPROT_QUERY_BINDING),
Buf, 1024,
&dwReturned,
NULL);
dwErr = GetLastError();
//*******************
if (dwErr!=0)
{
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwErr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&buff,
0,
NULL);
//Display the string
MessageBox(NULL,(LPTSTR)buff,"Device. Error llamada
IOCTL_NDISPROT_QUERY_BINDING",MB_ICONWARNING);
//Free the buffer
LocalFree(buff);
}
//*******************
//Convierto el nombre del dispositivo en un string Unicode(caracteres
anchos de 16 bits)
pwDeviceName=(WCHAR*)malloc(pQueryBinding->DeviceNameLength);

memcpy(pwDeviceName,((PBYTE)pQueryBinding+pQueryBi nding->DeviceNameOffset),pQueryBinding->DeviceNameLength);

return true;
}

---------------------------------------
My application in C# in Visual studio .NET:
------------------------------------
[STAThread]
static void Main()
{
bool ok;
Application.Run(new Form1());
ok=ApiWin32.Conectar();//Here is the error
System.NullReferenceException
if (ok)
{
ApiWin32.MessageBox(0,"Funciona","Ejemplo de invocación de
plataforma",0);
}
else
{
ApiWin32.MessageBox(0,"No funciona","Ejemplo de invocación de
plataforma",0);
}

}
-------------------------------------

ApiWin32 is class that I have defined:
------------------------------------
public class ApiWin32
{
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int MessageBox ( int hWnd,
String lpText,
String lpCaption,
uint uType) ;

[DllImport("DllWin32.dll", CharSet=CharSet.Auto)]
public static extern bool Conectar () ;
//Constructor de la clase
public ApiWin32()
{

}
}
-----------------------------
Please help me, I don't Known what happen.

Nov 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.