473,480 Members | 1,498 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

dll import

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=CallingCon vention.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);
}


Nov 17 '05 #1
3 5945
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" <Ja***@discussions.microsoft.com> wrote in message
news:53**********************************@microsof t.com...
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=CallingCon vention.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);
}

Nov 17 '05 #2
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" <Ja***@discussions.microsoft.com> wrote in message
news:53**********************************@microsof t.com...
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=CallingCon vention.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);
}

Nov 17 '05 #3
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" wrote:
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" <Ja***@discussions.microsoft.com> wrote in message
news:53**********************************@microsof t.com...
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=CallingCon vention.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);
}


Nov 17 '05 #4

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

Similar topics

0
6883
by: Stian Sřiland | last post by:
all examples performed with: Python 2.3+ (#2, Aug 10 2003, 11:09:33) on linux2 (2, 3, 0, 'final', 1) This is a recursive import:
0
2550
by: Vio | last post by:
Hi, I've been trying to embed (statically) wxPy alongside an embedded py interpreter on a linux/gtk box. At one point, for some reason misc.o linking reported "multiple definitions of...
0
1979
by: John Roth | last post by:
I've found a case where it seems that Python is importing two copies of a module without any reason or indication. It took me a while to verify that this is what is occuring: I had to write a...
5
2448
by: Steve Holden | last post by:
This is even stranger: it makes it if I import the module a second time: import dbimp as dbimp import sys if __name__ == "__main__": dbimp.install() #k = sys.modules.keys() #k.sort() #for...
1
8155
by: mark | last post by:
In Access 2000 and 2002, I have created an import specification to import the fixed-width recordset below into an existing table. I am having strange problems with the import of the date and time...
4
6070
by: Bruce W. Roeser | last post by:
All, I'm reading a book by Charles Petzold (Programming VS.Net). Pretty good content but am confused about the difference. From the text: ...
2
2557
by: Jon | last post by:
It appears that (windows) python searches in the current working directory before looking in the local site-packages directory, or that '.' comes first in sys.path? The problem arises when I made...
7
20529
by: Ron Adam | last post by:
from __future__ import absolute_import Is there a way to check if this is working? I get the same results with or without it. Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win 32 ...
5
1720
by: W. Watson | last post by:
Is there a single source that explains these statements? ------------------------------ from Tkinter import * from Numeric import * import Image import ImageChops import ImageTk import time...
9
2093
by: rsoh.woodhouse | last post by:
Hi, I'm trying to work out some strange (to me) behaviour that I see when running a python script in two different ways (I've inherited some code that needs to be maintained and integrated with...
0
7044
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
6908
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
7045
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,...
1
6741
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
6944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
2985
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.