473,386 Members | 1,997 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,386 software developers and data experts.

Strange problem with c# and platform invoke

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!
Nov 15 '05 #1
0 1062

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

Similar topics

137
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very...
1
by: Rahul Iyer | last post by:
G'day I have encountered quite a strange problem with a new function i've added to an existing software. Pseudo code of actions. -- begins -- func_foo (){
1
by: Coder Coder | last post by:
Hi, I have written a very simple, dll in delphi which just returns a string. When i try use Add Reference in VS.net 2003, it gives me an error saying that this is not a valid dll. Is there...
1
by: Sam Kong | last post by:
Hello! Recently I had a strange problem with Visual C# 2005 beta 1. When I ran(F5 key) a program, <#if DEBUG> statement was not working. It ran as RELEASE mode. So I had to manually define...
5
by: cody | last post by:
I have a very funny/strange effect here. if I let the delegate do "return prop.GetGetMethod().Invoke(info.AudioHeader, null);" then I get wrong results, that is, a wrong method is called and I...
7
by: stephan querengaesser | last post by:
hi ng, i try to invoke a webservice-method with an filter-object, that contains value types. if i donīt want to filter the return value of the method, i have to pass a new instance of the...
1
by: Alexander Inochkin | last post by:
Hi! I found same strange behavior of ASP.NET. It is possible this is the bug. Follow the steps:
45
by: simnav | last post by:
In the following code something strange happens ! If I keep pressed any of ALT+Arrow, keys, they are extracted two times from buffer then getch seems to stop; if I release and press again ALT+arrow...
4
by: Gotch | last post by:
Hi, I'm getting a very strange behaviour while running a project I've done.... Let's expose it: I've two projects. Both of them use a Form to do some Gui stuff. Other threads pack up messages...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.