Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Services / March 2005

Tip: Looking for answers? Try searching our database.

dll import

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jason - 25 Mar 2005 19:09 GMT
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=CallingConvention.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);
}
Dale Preston - 26 Mar 2005 01:57 GMT
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

> 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
[quoted text clipped - 35 lines]
>   memcpy(BufferOut, TheBuffer, sizeof(TheBuffer);
> }
Jason - 29 Mar 2005 01:17 GMT
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

> 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
[quoted text clipped - 47 lines]
> >   memcpy(BufferOut, TheBuffer, sizeof(TheBuffer);
> > }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.