473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Help:System.NullReferenceException with unmanaged memcpy

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
0 2106

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tim21 | last post by:
OK, im miserable :)) so.. help'd b highly appreciated. Situation: Win XP aplication server RUNTIME (stored fmx files along with the ..hlp files) Fmx's generated and compiled on development...
5
by: dixie | last post by:
I was wondering if there is a way of doing a simple help system with either viewing a page in a browser or looking at a definite page in a .pdf file when a help button was pushed on an Access...
3
by: Jay | last post by:
Well just installed the new 7.1 compiler. Not bad I must say but I'm becoming increasingly frustrated with the help system. Problem: project didn't compile due to DX9 include directories not...
5
by: Steve Teeples | last post by:
Can someone point me to a document that clearly identifies the steps of creating a good help system for an application? I have a test tool that I'd like to add help to so that others will know how...
9
by: JJ | last post by:
Do you all use HTML help workshop to create your help system. I am finding it quite clumsy to use. Mayeb because I am not used to using it. Do any of you use any other techniques to create help...
3
by: firewood | last post by:
I am developing a CMS/web-building website, and I need to include a good help system to help my customers learn how to use the features of what my project offers. Does anyone have suggestions...
8
by: redefined.horizons | last post by:
I did some searching for this topic, but couldn't find anything. A search of this list only turned up an old post from 2002. I'd like to add a comprehesive help system to my Python Application....
2
by: Mark Thompson | last post by:
Hi, I have a VB.NET 2003 application which includes a help system developed externally into a .chm file which is linked to from the application. There are 3 ways the application links to the...
6
by: JonathanOrlev | last post by:
Hello everyone, I have a newbe question: In Access (2003) VBA, what is the difference between a Module and a Class Module in the VBA development environment? If I remember correctly, new...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.