[Mono-list] PInvoke Methods
Daniel Morgan
danmorg@sc.rr.com
Sat, 13 Apr 2002 01:49:58 -0400
This is a multi-part message in MIME format.
------=_NextPart_000_0011_01C1E28D.84C6ED20
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit
Adam,
On IRC, you mentioned something about using reflection. Is IntPtr and
DllImport not the best way to go?
Here is what I am interested in doing. I would like to create a class
that will hold nothing but pinvoke functions to a native C library named
MyLibrary. I want the class MyLibrary private to the assembly. I think
this is done using the "internal" access modifier on the class. I am
using IntPtr for various structure pointers because the structures are
private to the native library. The only way to know what these
structures contain is to look at the source code to the library which we
may or may not have.
What is the best way to encapsulate the pinvoke calls to a native c
library in a class private to an assembly?
I will be using this for the native c library libpq.so/pq.dll
(PostgreSQL client library) in a class called PostgresLibrary in
assembly System.Data.dll
My example C# source code follows below:
// UseMe.cs - class that makes calls to the C library
using System;
namespace MyNamespace
{
public class UseMe
{
private callPinvokeMethod
{
IntPtr
ptrToPrivateStructToTheLibrary;
IntPtr ptrToAnotherPrivateStruct;
ptrToPrivateStructToTheLibrary =
MyLibrary.someLibraryFunction ("strvalue");
ptrToAnotherPrivateStruct =
MyLibrary.anotherLibraryFunction (ptrToPrivateStructToTheLibrary);
}
}
}
// MyLibrary.cs - class to contain pinvoke calls to native c library
libmy.so on linux/unix and my.dll on windows
using System;
using System.Runtime.InteropServices;
namespace MyNamespace
{
// using internal to make the class MyLibrary private to the
MyNamespace.dll assembly
internal class MyLibrary
{
[DllImport("my")]
public static extern IntPtr someLibraryFunction
(String value);
// PrivateLibraryStruct someLibraryFunction
(const char *value);
[DllImport("my")]
public static extern IntPtr
anotherLibraryFunction (IntPtr privateLibraryStructPtr);
// AnotherPrivateLibraryStruct
*anotherLibraryFunction (PrivateLibraryStruct *privateLibraryStructPtr);
}
}
Thank you,
Daniel
------=_NextPart_000_0011_01C1E28D.84C6ED20
Content-Type: text/html;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
<html xmlns:o=3D"urn:schemas-microsoft-com:office:office" =
xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns=3D"http://www.w3.org/TR/REC-html40">
<head>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Dus-ascii">
<meta name=3DProgId content=3DWord.Document>
<meta name=3DGenerator content=3D"Microsoft Word 10">
<meta name=3DOriginator content=3D"Microsoft Word 10">
<link rel=3DFile-List href=3D"cid:filelist.xml@01C1E28D.83F8A0A0">
<!--[if gte mso 9]><xml>
<o:OfficeDocumentSettings>
<o:DoNotRelyOnCSS/>
</o:OfficeDocumentSettings>
</xml><![endif]--><!--[if gte mso 9]><xml>
<w:WordDocument>
<w:SpellingState>Clean</w:SpellingState>
<w:GrammarState>Clean</w:GrammarState>
<w:DocumentKind>DocumentEmail</w:DocumentKind>
<w:EnvelopeVis/>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</w:WordDocument>
</xml><![endif]-->
<style>
<!--
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0in;
margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:12.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:"Times New Roman";}
a:link, span.MsoHyperlink
{color:blue;
text-decoration:underline;
text-underline:single;}
a:visited, span.MsoHyperlinkFollowed
{color:purple;
text-decoration:underline;
text-underline:single;}
span.EmailStyle17
{mso-style-type:personal-compose;
mso-style-noshow:yes;
mso-ansi-font-size:10.0pt;
mso-bidi-font-size:10.0pt;
font-family:Arial;
mso-ascii-font-family:Arial;
mso-hansi-font-family:Arial;
mso-bidi-font-family:Arial;
color:windowtext;}
span.SpellE
{mso-style-name:"";
mso-spl-e:yes;}
span.GramE
{mso-style-name:"";
mso-gram-e:yes;}
@page Section1
{size:8.5in 11.0in;
margin:1.0in 1.25in 1.0in 1.25in;
mso-header-margin:.5in;
mso-footer-margin:.5in;
mso-paper-source:0;}
div.Section1
{page:Section1;}
-->
</style>
<!--[if gte mso 10]>
<style>
/* Style Definitions */=20
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin:0in;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";}
</style>
<![endif]-->
</head>
<body lang=3DEN-US link=3Dblue vlink=3Dpurple =
style=3D'tab-interval:.5in'>
<div class=3DSection1>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'>Adam,<o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'>On IRC, you mentioned something about using =
reflection.<span
style=3D'mso-spacerun:yes'> </span>Is <span =
class=3DSpellE>IntPtr</span> and <span
class=3DSpellE>DllImport</span> not the best way to =
go?<o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'>Here is what I am interested in doing.<span
style=3D'mso-spacerun:yes'> </span>I would like to create a class =
that will
hold nothing but <span class=3DSpellE>pinvoke</span> functions to a =
native C
library named <span class=3DSpellE>MyLibrary</span>.<span
style=3D'mso-spacerun:yes'> </span>I want the class <span =
class=3DSpellE>MyLibrary</span>
private to the assembly.<span style=3D'mso-spacerun:yes'> </span>I =
think
this is done using the “internal” access modifier on the
class.<span style=3D'mso-spacerun:yes'> </span>I am using <span
class=3DSpellE>IntPtr</span> for various structure pointers because the
structures are private to the native library.<span
style=3D'mso-spacerun:yes'> </span>The only way to know what these
structures contain is to look at the source code to the library which we =
may or
may not have. <o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'>What is the best way to encapsulate the <span =
class=3DSpellE>pinvoke</span>
calls to a native c library in a class private to an =
assembly?<o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'>I will be using this for the native c library =
libpq.so/pq.dll
(PostgreSQL client library) in a class called <span =
class=3DSpellE>PostgresLibrary</span>
in assembly System.Data.dll <o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'>My example C# source code follows =
below:<o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'>// <span class=3DSpellE>UseMe.cs</span> – class =
that
makes calls to the C library<o:p></o:p></span></font></p>
<p class=3DMsoNormal><span class=3DGramE><font size=3D2 =
face=3DArial><span
style=3D'font-size:10.0pt;font-family:Arial'>using</span></font></span><f=
ont
size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;font-family:Arial'> =
System;<o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>
<p class=3DMsoNormal><span class=3DGramE><font size=3D2 =
face=3DArial><span
style=3D'font-size:10.0pt;font-family:Arial'>namespace</span></font></spa=
n><font
size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;font-family:Arial'> <span
class=3DSpellE>MyNamespace</span> <o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'>{<o:p></o:p></span></font></p>
<p class=3DMsoNormal style=3D'text-indent:.5in'><span =
class=3DGramE><font size=3D2
face=3DArial><span =
style=3D'font-size:10.0pt;font-family:Arial'>public</span></font></span><=
font
size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;font-family:Arial'> class <span
class=3DSpellE>UseMe</span><o:p></o:p></span></font></p>
<p class=3DMsoNormal style=3D'text-indent:.5in'><font size=3D2 =
face=3DArial><span
style=3D'font-size:10.0pt;font-family:Arial'>{<o:p></o:p></span></font></=
p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><span =
style=3D'mso-tab-count:1'>  =
; </span><span
style=3D'mso-tab-count:1'>  =
; </span><span
class=3DGramE>private</span> <span =
class=3DSpellE>callPinvokeMethod</span><o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><span =
style=3D'mso-tab-count:1'>  =
; </span><span
style=3D'mso-tab-count:1'>  =
; </span>{<o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><span =
style=3D'mso-tab-count:2'>  =
; =
</span><span
style=3D'mso-tab-count:1'>  =
; </span><span
class=3DSpellE>IntPtr</span> <span =
class=3DSpellE>ptrToPrivateStructToTheLibrary</span>;<o:p></o:p></span></=
font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><span =
style=3D'mso-tab-count:1'>  =
; </span><span
style=3D'mso-tab-count:2'>  =
; =
</span><span
class=3DSpellE>IntPtr</span> <span =
class=3DSpellE>ptrToAnotherPrivateStruct</span>;<o:p></o:p></span></font>=
</p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><span =
style=3D'mso-tab-count:1'>  =
; </span><o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><span =
style=3D'mso-tab-count:1'>  =
; </span><span
style=3D'mso-tab-count:1'>  =
; </span><span
style=3D'mso-tab-count:1'>  =
; </span><span
class=3DSpellE><span =
class=3DGramE>ptrToPrivateStructToTheLibrary</span></span><span
class=3DGramE> <span style=3D'mso-spacerun:yes'> </span>=3D</span> =
<span
class=3DSpellE>MyLibrary.someLibraryFunction</span> (“<span =
class=3DSpellE>strvalue</span>”);<o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><span =
style=3D'mso-tab-count:2'>  =
; =
</span><span
style=3D'mso-tab-count:1'>  =
; </span><span
class=3DSpellE><span =
class=3DGramE>ptrToAnotherPrivateStruct</span></span> =3D <span
class=3DSpellE>MyLibrary.anotherLibraryFunction</span> (<span =
class=3DSpellE>ptrToPrivateStructToTheLibrary</span>);<o:p></o:p></span><=
/font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><span =
style=3D'mso-tab-count:1'>  =
; </span><span
style=3D'mso-tab-count:1'>  =
; </span>}<o:p></o:p></span></font></p>
<p class=3DMsoNormal style=3D'text-indent:.5in'><font size=3D2 =
face=3DArial><span
style=3D'font-size:10.0pt;font-family:Arial'>}<o:p></o:p></span></font></=
p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'>}<o:p></o:p></span></font></p>
<p class=3DMsoNormal style=3D'text-indent:.5in'><font size=3D2 =
face=3DArial><span
style=3D'font-size:10.0pt;font-family:Arial'><o:p> </o:p></span></fo=
nt></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'>// <span class=3DSpellE>MyLibrary.cs</span> – =
class to
contain <span class=3DSpellE>pinvoke</span> calls to native c library =
<span
class=3DSpellE>libmy.so</span> on <span class=3DSpellE>linux/unix</span> =
and my.dll
on windows<o:p></o:p></span></font></p>
<p class=3DMsoNormal><span class=3DGramE><font size=3D2 =
face=3DArial><span
style=3D'font-size:10.0pt;font-family:Arial'>using</span></font></span><f=
ont
size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;font-family:Arial'> =
System;<o:p></o:p></span></font></p>
<p class=3DMsoNormal><span class=3DGramE><font size=3D2 =
face=3DArial><span
style=3D'font-size:10.0pt;font-family:Arial'>using</span></font></span><f=
ont
size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;font-family:Arial'> <span
class=3DSpellE>System.Runtime.InteropServices</span>;<o:p></o:p></span></=
font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>
<p class=3DMsoNormal><span class=3DGramE><font size=3D2 =
face=3DArial><span
style=3D'font-size:10.0pt;font-family:Arial'>namespace</span></font></spa=
n><font
size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;font-family:Arial'> <span
class=3DSpellE>MyNamespace</span><o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'>{<o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><span =
style=3D'mso-tab-count:1'>  =
; </span>//
using internal to make the class <span class=3DSpellE>MyLibrary</span> =
private to
the MyNamespace.dll assembly<o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><span =
style=3D'mso-tab-count:1'>  =
; </span><span
class=3DGramE>internal</span> class <span =
class=3DSpellE>MyLibrary</span><o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><span =
style=3D'mso-tab-count:1'>  =
; </span>{<o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><span =
style=3D'mso-tab-count:2'>  =
; =
</span>[<span
class=3DSpellE><span class=3DGramE>DllImport</span></span><span =
class=3DGramE>(</span>“my”)]<o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><span =
style=3D'mso-tab-count:2'>  =
; =
</span><span
class=3DGramE>public</span> static extern <span =
class=3DSpellE>IntPtr</span> <span
class=3DSpellE>someLibraryFunction</span> (String =
value);<o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><span =
style=3D'mso-tab-count:2'>  =
; =
</span>//
<span class=3DSpellE>PrivateLibraryStruct</span> <span =
class=3DSpellE>someLibraryFunction</span>
(const char *value);<o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><span =
style=3D'mso-tab-count:2'>  =
; =
</span>[<span
class=3DSpellE><span class=3DGramE>DllImport</span></span><span =
class=3DGramE>(</span>“my”)]<o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><span =
style=3D'mso-tab-count:2'>  =
; =
</span><span
class=3DGramE>public</span> static extern <span =
class=3DSpellE>IntPtr</span> <span
class=3DSpellE>anotherLibraryFunction</span> (<span =
class=3DSpellE>IntPtr</span> <span
class=3DSpellE>privateLibraryStructPtr</span>);<o:p></o:p></span></font><=
/p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><span =
style=3D'mso-tab-count:2'>  =
; =
</span>//
<span class=3DSpellE>AnotherPrivateLibraryStruct</span> *<span =
class=3DSpellE>anotherLibraryFunction</span>
(<span class=3DSpellE>PrivateLibraryStruct</span> *<span =
class=3DSpellE>privateLibraryStructPtr</span>);<o:p></o:p></span></font><=
/p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><span =
style=3D'mso-tab-count:1'>  =
; </span>}<o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'>}<o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'>Thank you,<o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'>Daniel<o:p></o:p></span></font></p>
<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>
</div>
</body>
</html>
------=_NextPart_000_0011_01C1E28D.84C6ED20--