#C #Windows 我来总结一下用了哪些 API 怎么用,方便背
首先,你需要
#CSharp 版:需要使用 C# 的 P/Invoke (System.Runtime.InteropServices)特性
1. 对于外部的 C 结构体,只需要使用
2. 对于外部的 C 子程序,只需要将在一个类里定义的静态方法用
Trumeet 的示例还用了一个 [STAThread] 方法特性,只是这个单元只能单线程执行,不过其实不必加在
首先,你需要
aero.h
[@ysc3839's gist] 这个 undocumented API header 其中包括必须的数据类型和子程序定义#CSharp 版:需要使用 C# 的 P/Invoke (System.Runtime.InteropServices)特性
1. 对于外部的 C 结构体,只需要使用
struct
关键字定义,然后加上 [StructLayout(LayoutKind.Sequential)]
特性避免压缩移动优化结构体就可以了2. 对于外部的 C 子程序,只需要将在一个类里定义的静态方法用
extern
修饰,这个方法加上 [DllImport("user32.dll")]
之类的特性就可以了,如果要指定载入使用的字符编码,需要使用 CharSet(=CharSet.Ansi) 特性指定Trumeet 的示例还用了一个 [STAThread] 方法特性,只是这个单元只能单线程执行,不过其实不必加在
Main
静态方法上,和 COM 交互的时候加就好了,这个知道就好Docs
DllImportAttribute Class (System.Runtime.InteropServices)
Indicates that the attributed method is exposed by an unmanaged dynamic-link library (DLL) as a static entry point.