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 / .NET Framework / CLR / February 2004

Tip: Looking for answers? Try searching our database.

Strange problem with C# and platform invoke

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sqcliu - 17 Feb 2004 09:46 GMT
I encounter a strange problem using platform invoke using C#.

The senario is this:
I have a dll which uses a 3rd party static link library (lib). Inside the lib, there are some C structures defined by not exposed to user, in the dll, that data type can be treated as an opaque type.

I use C# and unamanaged C++ to write two programs to use the dll. The C++ version works perfectly, but the C# version always get some exception (Object reference not set to an instance of an object)

Here's some demo code:

(1)In 3rd party lib file, there's one type
//_tagContext's internal structre unknown, it's an opaque type
typedef _tagContext * tagContext;

(2)In my dll file there are 2 functions:
//Create an opaque type object to let ptagContext point to it
int Init(tagContext* ptagContext, int* status);

//Do some work using the context initialized by Init()
int DoWork(tagContext context, char* data);

(3)In my C++ code:
//Declare a opaque type variable
//Declare a variable of the opaque type
tagContext* ptagContext= 0;
int status = 0;

//Init, works fine
Init(ptagContext, &status);

//Do work, works fine
DoWork(*ptagContext, data);

(4)In my C# code:
public class MyClass
{
   private System.IntPtr context;
   private int status;

   public int Initialize()
   {
       MyApi.Init(out context, out status);
   }

   public int Work(String data)
   {
       MyApi.DoWork(context, data);
   }

   private class MyApi
  {
       [DllImport("MyApi.dll", CharSet = CharSet.Ansi)]
       public static extern int Init(out System.IntPtr context, out int status);

       [DllImport("MyApi.dll", CharSet = CharSet.Ansi)]
       public static extern int DoWork(System.IntPtr context, String data);
  }
}

The user of this class:
MyClass c = new MyClass();
String data = new String("data");
//Works fine, can see the context field inside MyClass is initialized
c.Init();
//Throw exception: Object reference not set to an instance of an object
c.Work(data);

I have no idea what's going on after CLR calls Init() to initialize IntPtr.
Is a unmanaged object created and pointed to by IntPtr?
Where is the unmanaged object created? Heap or stack?
After the c.Init() finish, is it possible for the unmanaged object (refeced by IntPtr) to be garbage collected,
so when I call c.Work() it shows me the exception?

Experts please help out! Thanks so much in advance!
Ben Rush - 21 Feb 2004 16:46 GMT
And you've followed the debugger into your Work() C# app and both
parameters are defined?

> I encounter a strange problem using platform invoke using C#.
>
[quoted text clipped - 69 lines]
>
> Experts please help out! Thanks so much in advance!

Rate this thread:







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.