dotnet C # Programmer ' s Guide to the Win32 API (Win

Source: Internet
Author: User
Tags dotnet win32
dotnet C # Programmer ' s Guide to the Win32 API (WIN32 API declaration C # version)



Stingy God, 2001.09.07



Oh, I'm sorry I used the title, in fact, if I can do 10%, I will be very happy. Because there is a Dan Appleman book on my desk, it has been with me for many years, maybe you also have one: 1215 pages, blue cover, machinery industry press. English name: Dan Appleman ' s Visual Basic 5.0 Programmer ' Guide to the Win32 API. In addition to borrowing this similar name, I just did a bit of tidying up, but it was a very small amount of work (haha)



I put KERNEL32.DLL, USER32. DLL, GDI32. DLL, ADVAPI32. DLL, SHELL32. DLL, Spoolss. DLL, winmm. DLL functions and declarations for a while, rewrite the syntax of C # instead, and sort out a C # version. This allows you to use these WIN32 API functions directly for dotnet Platform invocation Services (PInVoke). Currently MS does not disclose the whole win32api how many can be used in dotnet, how many do not. Some features do not have a corresponding implementation in dotnet, so you may also need to use WIN32 API functions at this time.



The structure and enumeration declarations are placed together, Kernel32.dll, User32.Dll, GDI32.dll are placed in 3 CS files; the remaining Advapi32.dll, Shell32.dll, Spoolss.dll, Winmm.dll Four DLL functions are placed in a CS file, the entire namespace called Win32API. The approximate structure should look like this:

Namespace Win32API {



Using System;

Using System.Runtime.InteropServices;



First, all the structures and enumeration declarations

Class Kernel32 {...}

Class User32 {...}

Class GDI32 {...}

Class Advapi32 {...}

Class Shell32 {...}

Class Spoolss {...}

Class Winmm {...}

}



The corresponding build is the following C # file:

Structs.NET.cs

Kernel32.NET.cs

User32.NET.cs

GDI32.NET.cs

OtherFnc.NET.cs



Then compile them so that you can use them, and I don't calculate all the exact numbers included in 5 files, but there should be hundreds of structs and enumerations, thousands of function declarations. After completing these, the most commonly used messagebox I can use now:



Using System;

Using Win32API;



public class Testwin32api

{

public static void Main ()

{



int iRet;

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

}

}



In the test of this example, an interesting thing happened, because at the beginning of my CS file does not have the ID of the net so it is natural to have Kernel32.cs, User32.cs and so on, MessageBox is User32.DLL, and I compiled the User32.cs default generated also called User32.DLL, when I executed this test program finally occurred, I always thought that my statement has a problem, I then used the example in FRAMEWORKSDK to make a mistake, until I found my own DLL name and the system's DLL name exactly the same when I was working in another directory. Then change to the above, but it is very strange csc/t:exe/r:user32.dll TestWin32API.cs compiler instructions do not have the wrong hint, really hateful. Remind you not to be like me.

Nightmares may still be in the back, I do not know the REAL CS statement and how many errors, anyway, when used to debug it. Because too much so cannot all test, also dare not one test, only hope that they can play a role in the finishing, if you have a new version, do not forget mail a copy to me.



The accompanying zip package contains all the CS source code, which I compiled under the framework SDK BETA2.

Windows ADV Server SP2 EN

Visual C # Compiler Version 7.00.9254

CLR version v1.0.2914

You can compile according to your situation (compile Structs.NET.cs first, because all else will be/r:structs.net.dll) or use binary DLLs. Dotnet's compilation technology is very good, 5 DLLs add up to more than 100 K.



Here is an excerpt from the source code:



--------------------------------------------------------------------------------


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 Kerne




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.