这是三篇网上收集的技术文章的合集,分别讲解了如何创建win32 dll,然后如何在c#里面调用这个dll的教程。
首先是创建win32 dll的文章。讲解这个的文章到处都有,这里给出一篇我看过的:http://www.flipcode.com/articles/article_creatingdlls.shtml。win32 dll的创建其实在visual studio里面已经给出了比较好的模板,只是需要注意的,有些xxx_api宏并没有把extern "c"加进去,这样会造成在c#里面找不到这个函数的错误。所以,请记住,一定要把函数定义extern "c"。
第二篇文章来之于http://edu.100down.com/it/program/csharp/105256797.html,下面是转贴内容:
平台调用服务 (pinvoke) 允许托管代码调用在 dll 中实现的非托管函数。
本教程说明使用什么方法才能从 c# 调用非托管 dll 函数。该教程所讨论的属性允许您调用这些函数并使数据类型得到正确封送。
c# 代码有以下两种可以直接调用非托管代码的方法:
对于这两种技术,都必须向 c# 编译器提供非托管函数的声明,并且还可能需要向 c# 编译器提供如何封送与非托管代码之间传递的参数和返回值的说明。
该教程由下列主题组成:
该教程包括下列示例:
若要声明一个方法使其具有来自 dll 导出的实现,请执行下列操作:
本示例显示如何使用 dllimport 属性通过调用 msvcrt.dll 中的 puts 输出消息。
// pinvoketest.csusing system;using system.runtime.interopservices;class platforminvoketest{    [dllimport("msvcrt.dll")]    public static extern int puts(string c);    [dllimport("msvcrt.dll")]    internal static extern int _flushall();    public static void main()     {        puts("test");        _flushall();    }}test
前面的示例显示了声明在非托管 dll 中实现的 c# 方法的最低要求。platforminvoketest.puts 方法用 static 和 extern 修饰符声明并且具有 dllimport 属性,该属性使用默认名称 puts 通知编译器此实现来自 msvcrt.dll。若要对 c# 方法使用不同的名称(如 putstring),则必须在 dllimport 属性中使用 entrypoint 选项,如下所示:
[dllimport("msvcrt.dll", entrypoint="puts")]有关 dllimport 属性的语法的更多信息,请参见 dllimportattribute 类。
当从 c# 代码中调用非托管函数时,公共语言运行库必须封送参数和返回值。
对于每个 .net framework 类型均有一个默认非托管类型,公共语言运行库将使用此非托管类型在托管到非托管的函数调用中封送数据。例如,c# 字符串值的默认封送处理是封送为 lptstr(指向 tchar 字符缓冲区的指针)类型。可以在非托管函数的 c# 声明中使用 marshalas 属性重写默认封送处理。
本示例使用 dllimport 属性输出一个字符串。它还显示如何通过使用 marshalas 属性重写函数参数的默认封送处理。
// marshal.csusing system;using system.runtime.interopservices;class platforminvoketest{    [dllimport("msvcrt.dll")]    public static extern int puts(        [marshalas(unmanagedtype.lpstr)]        string m);    [dllimport("msvcrt.dll")]    internal static extern int _flushall();    public static void main()     {        puts("hello world!");        _flushall();    }}运行此示例时,字符串
hello world!
将显示在控制台上。
在前面的示例中,puts 函数的参数的默认封送处理已从默认值 lptstr 重写为 lpstr。
marshalas 属性可以放置在方法参数、方法返回值以及结构和类的字段上。若要设置方法返回值的封送处理,请将 marshalas 属性与返回属性位置重写一起放置在方法上的属性块中。例如,若要显式设置 puts 方法返回值的封送处理:
...[dllimport("msvcrt.dll")] [return : marshalas(unmanagedtype.i4)]public static extern int puts( ...有关 marshalas 属性的语法的更多信息,请参见 marshalasattribute 类。
注意 in 和 out 属性可用于批注非托管方法的参数。它们与 midl 源文件中的 in 和 out 修饰符的工作方式类似。请注意,out 属性与 c# 参数修饰符 out 不同。有关 in 和 out 属性的更多信息,请参见 inattribute 类和 outattribute 类。
可以为传递到非托管函数或从非托管函数返回的结构和类的字段指定自定义封送处理属性。通过向结构或类的字段中添加 marshalas 属性可以做到这一点。还必须使用 structlayout 属性设置结构的布局,还可以控制字符串成员的默认封送处理,并设置默认封装大小。
本示例说明如何为结构指定自定义封送处理属性。
请考虑下面的 c 结构:
typedef struct taglogfont {    long lfheight;    long lfwidth;    long lfescapement;    long lforientation;    long lfweight;    byte lfitalic;    byte lfunderline;    byte lfstrikeout;    byte lfcharset;    byte lfoutprecision;    byte lfclipprecision;    byte lfquality;    byte lfpitchandfamily;    tchar lffacename[lf_facesize]; } logfont; 在 c# 中,可以使用 structlayout 和 marshalas 属性描述前面的结构,如下所示:
// logfont.cs// compile with: /target:moduleusing system;using system.runtime.interopservices;[structlayout(layoutkind.sequential)]public class logfont {     public const int lf_facesize = 32;    public int lfheight;     public int lfwidth;     public int lfescapement;     public int lforientation;     public int lfweight;     public byte lfitalic;     public byte lfunderline;     public byte lfstrikeout;     public byte lfcharset;     public byte lfoutprecision;     public byte lfclipprecision;     public byte lfquality;     public byte lfpitchandfamily;    [marshalas(unmanagedtype.byvaltstr, sizeconst=lf_facesize)]    public string lffacename; }有关 structlayout 属性的语法的更多信息,请参见 structlayoutattribute 类。
然后即可将该结构用在 c# 代码中,如下所示:
// pinvoke.cs// compile with: /addmodule:logfont.netmoduleusing system;using system.runtime.interopservices; class platforminvoketest{         [dllimport("gdi32.dll", charset=charset.auto)]      public static extern intptr createfontindirect(            [in, marshalas(unmanagedtype.lpstruct)]            logfont lplf   // characteristics            );       [dllimport("gdi32.dll")]      public static extern bool deleteobject(            intptr handle            );       public static void main()       {            logfont lf = new logfont();            lf.lfheight = 9;            lf.lffacename = "arial";            intptr handle = createfontindirect(lf);             if (intptr.zero == handle)            {                  console.writeline("can't creates a logical font.");            }            else            {                                    if (intptr.size == 4)                        console.writeline("{0:x}", handle.toint32());                  else                        console.writeline("{0:x}", handle.toint64());                           // delete the logical font created.                  if (!deleteobject(handle))                       console.writeline("can't delete the logical font");            }      }}c30a0ae5
在前面的示例中,createfontindirect 方法使用了一个 logfont 类型的参数。marshalas 和 in 属性用于限定此参数。程序将由此方法返回的数值显示为十六进制大写字符串。
若要注册调用非托管函数的托管回调,请用相同的参数列表声明一个委托并通过 pinvoke 传递它的一个实例。在非托管端,它将显示为一个函数指针。有关 pinvoke 和回调的更多信息,请参见平台调用详解。
例如,考虑以下非托管函数 myfunction,此函数要求 callback 作为其参数之一:
typedef void (__stdcall *pfn_mycallback)();int __stdcall myfunction(pfn_ mycallback callback);
若要从托管代码调用 myfunction,请声明该委托,将 dllimport 附加到函数声明,并根据需要封送任何参数或返回值:
public delegate void mycallback();[dllimport("mydll.dll")]public static extern void myfunction(mycallback callback);同时,请确保委托实例的生存期覆盖非托管代码的生存期;否则,委托在经过垃圾回收后将不再可用。
第三篇文章来之于http://www.njpro.cn/8918/showpost.aspx,一篇关于c/c++和c#中的数据类型的对应表。
| wtypes.h 中的非托管类型 | 非托管 c 语言类型 | 托管类名 | 说明 | 
|---|---|---|---|
| handle | void* | 32 位 | |
| byte | unsigned char | 8 位 | |
| short | short | 16 位 | |
| word | unsigned short | 16 位 | |
| int | int | 32 位 | |
| uint | unsigned int | 32 位 | |
| long | long | 32 位 | |
| bool | long | 32 位 | |
| dword | unsigned long | 32 位 | |
| ulong | unsigned long | 32 位 | |
| char | char | 用 ansi 修饰。 | |
| lpstr | char* | 用 ansi 修饰。 | |
| lpcstr | const char* | 用 ansi 修饰。 | |
| lpwstr | wchar_t* | 用 unicode 修饰。 | |
| lpcwstr | const wchar_t* | 用 unicode 修饰。 | |
| float | float | 32 位 | |
| double | double | 
新闻热点
疑难解答