手把手教你PInvoke

本文手把手教你如何使用PInvoke在C#中调用非托管C++代码,详细解释了如何转换函数签名,包括处理窗口句柄、字符串和结构体等,并提供实例演示了获取HID设备接口GUID的过程。同时,推荐了pinvoke.net网站和Visual Studio插件作为辅助工具。
摘要由CSDN通过智能技术生成

当你写久了应用层代码,是不是需要来玩一下硬件呀?


这个时候你就会接触到一些美妙的dll,比如user32.dll,kernal32.dll

当然这些是非托管的代码,我们在.net中无法直接使用,所以我们会需要使用PInvoke进行调用

于是你会使用DllImport特性标记一个方法,引入非托管函数

比如 我们希望弹出一个消息框,就会使用下面这个函数,添加DllImport特性,表明从哪个dll引入方法

public class Win32
{
   
    [DllImport("user32.dll")]
    public static extern IntPtr MessageBox(int hWnd, String text, String caption, uint type);
}
class Program
{
   
    static void Main(string[] args)
    {
   
        Win32.MessageBox(0,
PInvoke插件 RedGate.PInvokeExtension VS平台调用插件 PInvoke 平台调用 调用C++ 调用系统 C#C++, Search Module: Directory Constants Delegates Enums Interfaces Structures Desktop Functions: advapi32 avifil32 cards cfgmgr32 comctl32 comdlg32 credui crypt32 dbghelp dbghlp dbghlp32 dhcpsapi difxapi dmcl40 dnsapi dtl dwmapi faultrep fbwflib fltlib fwpuclnt gdi32 gdiplus getuname glu32 glut32 gsapi hhctrl hid hlink httpapi icmp imm32 iphlpapi iprop irprops kernel32 mapi32 MinCore mpr mqrt mscorsn msdelta msdrm msi msports msvcrt ncrypt netapi32 ntdll ntdsapi odbc32 odbccp32 ole32 oleacc oleaut32 opengl32 pdh powrprof printui propsys psapi pstorec query quickusb rasapi32 rpcrt4 scarddlg secur32 setupapi shell32 shlwapi twain_32 unicows urlmon user32 userenv uxtheme version wer wevtapi winfax winhttp wininet winmm winscard winspool wintrust winusb wlanapi ws2_32 wtsapi32 xolehlp xpsprint Smart Device Functions: aygshell coredll ipaqutil rapi Glossary distributed computing LibHolocaust Marshaling Marshalling Marshalling PInvoke test pinvoke Show Recent Changes Subscribe (RSS) Misc. Pages Comments FAQ Helpful Tools Playground Suggested Reading Website TODO List Support Forum Download Visual Studio Add-In Terms of Use Privacy Policy Create page PInvoke (glossary) Summary The mechanism provided by the CLR that enables managed code to call static DLL exports. Access PInvoke.net directly from VS: Terms of Use Edit This Page Find References Show Printable Version Revisions Show changes
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值