[Mono-list] PInvoke and Marshalling

James Lapalme james.lapalme@videotron.ca
Tue, 10 Feb 2004 21:19:49 -0500


This is a multi-part message in MIME format.

--Boundary_(ID_pfseDC+HOvZRnjMAoAXOhQ)
Content-type: text/plain; charset=iso-8859-1
Content-transfer-encoding: 7BIT

Dear All,

I created a dll with the following:

extern "C" _declspec(dllexport) int myFunction(A*){return a->myInt();}

class A{
    public:
        int i;
        int myInt(void){rerturn i;}
}

I creates a C# client with the folllowing:

public class MyClient{

    [DllImport("MyDll.dll")]
    public static extern in myFunction(A a);

    public static void Main(){

        A a = new A();
        a.i=5;
        Console.WriteLine(myFunction(a));
    }
}

[StructureLayout(LayputKind.Sequential)]
public class A{
        public int i;
        public int myInt(void){rerturn i;}
}


Why does this code work because most of the example I have seen is always using classes to mimic struct with no methods only fields?

How is the object marshalled?

I have readed .Net nad COM by Nathan/ COM and Net by Troelsen/MSND doc... there is no info on this matter. Where can I get more information on this subject?

What are the limites(inheritence,....) of redefining C+ classes in C# and call C++ code with the redefined classes?

James

P.s this code was tried with the microsoft's version of .Net

--Boundary_(ID_pfseDC+HOvZRnjMAoAXOhQ)
Content-type: text/html; charset=iso-8859-1
Content-transfer-encoding: 7BIT

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2800.1276" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2><FONT face="Times New Roman" size=3>Dear 
All,<BR><BR>I created a dll with the following:<BR><BR>extern "C" 
_declspec(dllexport) int myFunction(A*){return a-&gt;myInt();}<BR><BR>class 
A{<BR>&nbsp;&nbsp;&nbsp; public:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
int i;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int myInt(void){rerturn 
i;}<BR>}<BR><BR>I creates a C# client with the folllowing:<BR><BR>public class 
MyClient{<BR><BR>&nbsp;&nbsp;&nbsp; 
[DllImport("MyDll.dll")]<BR>&nbsp;&nbsp;&nbsp; public static extern in 
myFunction(A a);<BR><BR>&nbsp;&nbsp;&nbsp; public static void 
Main(){<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; A a = new 
A();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
a.i=5;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Console.WriteLine(myFunction(a));<BR>&nbsp;&nbsp;&nbsp; 
}<BR>}<BR><BR>[StructureLayout(LayputKind.Sequential)]<BR>public class 
A{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int 
i;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int myInt(void){rerturn 
i;}<BR>}<BR><BR><BR>Why does this code work because most of the example I have 
seen is always using classes to mimic struct with no&nbsp;methods only 
fields?</FONT></FONT></DIV>
<DIV><FONT face=Arial size=2><FONT face="Times New Roman" 
size=3></FONT></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><FONT face="Times New Roman" size=3>How is the 
object marshalled?</FONT></FONT></DIV>
<DIV><FONT face=Arial size=2><FONT face="Times New Roman" 
size=3></FONT></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><FONT face="Times New Roman" size=3>I have readed 
.Net nad COM by Nathan/ COM and Net by Troelsen/MSND doc... there is no info on 
this matter. Where can I get more information on this 
subject?</FONT></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV>What are the limites(inheritence,....) of redefining C+ classes in C# and 
call C++ code with the redefined classes?<FONT face=Arial size=2><FONT 
face="Times New Roman" size=3><BR><BR>James</FONT><BR><BR>P.s this code was 
tried with the microsoft's version of .Net</DIV></FONT></BODY></HTML>

--Boundary_(ID_pfseDC+HOvZRnjMAoAXOhQ)--