dotNET C# Programmer’s Guide to the Win32 API (Win32 API 声明C#版)

王朝c#·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

dotNET C# Programmer’s Guide to the Win32 API (Win32 API 声明C#版)

小气的神 2001.09.07

噢,对不起我用了这个标题,事实上如果我能做到10%,我就很高兴了。因为我的桌上正放着一本Dan Appleman的书,它曾伴随我许多年,也许你也有一本:1215页,蓝色书皮,机械工业出版社。英文名叫:《 Dan Appleman’s Visual Basic 5.0 Programmer’s Guide to the Win32 API 》。而我除了借用这个类似的名字之外,只是做了一下整理工作,不过这是一个不小的工作量(haha)

我把KERNEL32.DLL、USER32.DLL、GDI32.DLL、 ADVAPI32.DLL、SHELL32.DLL、SPOOLSS.DLL、WINMM.DLL的函数和声明整理了一下,改用C#的语法重新改写了一遍,整理出一个C#版本。这样针对dotNET的Platform Invocation Services(PInVoke),你就可以直接使用这些Win32的API函数了。目前MS没有公布整个的Win32API有多少可以在dotNET中使用,有多少不行。有些功能在dotNET中没有对应的实现,那么目前你可能还需要使用Win32的API函数。

结构和枚举声明放在一起,Kernel32.dll、User32.Dll、GDI32.dll分别放在3个cs文件中;剩下的Advapi32.dll、Shell32.dll、Spoolss.dll、Winmm.dll四个DLL的函数放在一个cs文件中,整个的namespace 起名叫Win32API.大致的构架应该象下面这样:

namespace Win32API {

using System;

using System.Runtime.InteropServices;

先是所有的结构和枚举声明

class Kernel32 {….}

class User32 {….}

class GDI32 {….}

class Advapi32 {….}

class Shell32 {….}

class Spoolss {….}

class Winmm {….}

}

对应的生成下面的C#文件:

Structs.NET.cs

Kernel32.NET.cs

User32.NET.cs

GDI32.NET.cs

OtherFnc.NET.cs

然后编译它们,这样就可以使用它们了,我没有计算5个文件包括的所有的准确个数,不过应该有百个结构和枚举,上千个函数声明吧。完成这些之后,现在最常用的MessageBox我可以这样使用了:

using System ;

using Win32API;

public class TestWin32API

{

public static void Main()

{

int iRet ;

iRet = User32.MessageBox( 0, "Hello C# Win32 API" , "My Pinvoke", 0 ) ;

}

}

就在测试这个例子时发生了一件有趣的事,因为刚开始我的cs文件都没有加NET的标识这样很自然的就有了Kernel32.cs、 User32.cs等等,MessageBox是User32.DLL中的,而我编译的User32.cs默认生成的也叫User32.DLL,当我执行这个测试程序时终于发生错误,我一直以为自己的声明有问题,然后我用FrameworkSDK中的例子也还是出错,直到我在其它的目录中运行正常时,我才发现原来自己的DLL名和系统的DLL名完全一样。然后就改成上面的那样,不过很奇怪csc /t:exe /r:User32.dll TestWin32API.cs 的编译指令居然没有错误提示,真是可恶。提醒你不要也象我一样。

恶梦也许还在后面,我还不知道真正的cs声明中又有多少个错误,反正用到的时候再调试吧。因为太多了所以不可能全都测试,也不敢一个个的测试,只希望自己这些整理能起到抛砖引玉的作用,如果以后你有新的版本,不要忘了Mail一份给我。

附带的Zip包中有所有cs的源码,我在Framework SDK Beta2 下都编译通过。

Windows 2000 ADV Server SP2 EN

Visual C# Compiler Version 7.00.9254

CLR version v1.0.2914

你可以根据你的情况进行编译(要先编译Structs.NET.cs,因为其它的都要/r:Structs.NET.DLL)或者使用二进制的DLL。dotnet的编译技术很好,5个DLL加起来才100多K。

下面是一下源码的节选:

// Write by ccBoy

// Date: 2001.09.07

// csc /t:library /r:System.dll Structs.NET.cs

// csc /t:library /r:Structs.NET.dll Kernel32.NET.cs

// Emeditor 3.14

namespace Win32API {

using System;

using System.Runtime.InteropServices;

[StructLayout (LayoutKind.Sequential)]

public class ABC {

public int abcA;

public int abcB;

public int abcC;

}

[StructLayout (LayoutKind.Sequential)]

public class ABCFLOAT {

public float abcfA;

public float abcfB;

public float abcfC;

}

public Class Kernel32 {

[DllImport("KERNEL32.DLL") ]

public static extern int GlobalHandle (int pMem);

[DllImport("KERNEL32.DLL") ]

public static extern int GlobalLock (int hMem);

[DllImport("KERNEL32.DLL") ]

public static extern void GlobalMemoryStatus (MEMORYSTATUS lpBuffer);

[DllImport("KERNEL32.DLL") ]

public static extern int GlobalReAlloc (int hMem, int dwBytes, int uFlags);

[DllImport("KERNEL32.DLL") ]

public static extern int GlobalSize (int hMem);

[DllImport("KERNEL32.DLL") ]

public static extern bool GlobalUnWire (int hMem);

}

public class User32 {

[DllImport("USER32.DLL") ]

public static extern bool MessageBeep (int uType);

[DllImport("USER32.DLL") ]

public static extern int MessageBoxEx (int hWnd, String lpText, String lpCaption, int uType, short wLanguageId);

[DllImport("USER32.DLL") ]

public static extern int MessageBoxIndirect (MSGBOXPARAMS anonymous0);

[DllImport("USER32") ]

public static extern int MessageBox (int hWnd, String lpText, String lpCaption, int uType);

}

pubic class Winmm {

[DllImport("WINMM.DLL") ]

public static extern int SendDriverMessage (int hDriver, int message, int lParam1, int lParam2);

[DllImport("WINMM.DLL") ]

public static extern int auxGetDevCaps (int uDeviceID, AUXCAPS pac, int cbac);

[DllImport("WINMM.DLL") ]

public static extern int auxGetNumDevs ();

[DllImport("WINMM.DLL") ]

public static extern int auxGetVolume (int uDeviceID, int[] pdwVolume);

[DllImport("WINMM.DLL") ]

public static extern int auxOutMessage (int uDeviceID, int uMsg, int dw1, int dw2);

[DllImport("WINMM.DLL") ]

public static extern int auxSetVolume (int uDeviceID, int dwVolume);

}

.,. ..,. ..,.. ..,..,.. ,. ..,. ..,.. ..,..,.. 太多太多hahaha..

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航