在vs2008里使用pinvoke.net插件

英文原文:http://www.red-gate.com/supportcenter/GeneralContent.aspx?c=knowledgebase\PInvoke\KB200711000198.htm

PInvoke.net add-in support for Visual Studio 2008

PInvoke.net目前安装版本只支持自动添加到vs.net2005,但你只需要在配置文件中做一些小修改,同样可以添加到2008去。
首先打开配置文件:PInvoke.net 安装目录下的

%programfiles%\red gate\PInvoke.NET\PInvokeDotNetAddIn.Addin

在以下配置后面
---------------------------------------------

< HostApplication >
< Name > Microsoft Visual Studio Macros </ Name >
< Version > 8.0 </ Version >
</ HostApplication >
< HostApplication >
< Name > Microsoft Visual Studio </ Name >
< Version > 8.0 </ Version >
</ HostApplication >

 

----------------------------------------------
添加以下配置
----------------------------------------------

< HostApplication >
< Name > Microsoft Visual Studio Macros </ Name >
< Version > 9.0 </ Version >
</ HostApplication >
< HostApplication >
< Name > Microsoft Visual Studio </ Name >
< Version > 9.0 </ Version >
</ HostApplication >

 

然后保存文件

打开vs2008,打开菜单:工具》选项》环境》外接程序/宏的安全性

然后选择添加,选择PInvoke.NET的安装路径。 (比如:c:\program files\red gate\PInvoke.NET)

按确定。重新打开vs2008之后就可以看到附加的菜单了。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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
在C++中通过PInvoke(Platform Invoke)技术调用C#编写的好处在于,它可以让你直接访问.NET框架下的类库和方法,而不需要将整个.NET环境部署到C++环境中。以下是基本步骤: 1. **包含头文件**:首先,你需要包含`DllImport`和`System.Runtime.InteropServices`命名空间的头文件,例如: ```cpp #include <windows.h> #include <tchar.h> #include <iostream> #include <string> using namespace std; using namespace System; using namespace System::Runtime::InteropServices; ``` 2. **声明PInvoke函数原型**:对C# DLL中的每个公开函数,都要使用`DllImport`声明,指定调用的库名、模块名以及函数签名。例如: ```cpp [DllImport("YourDllName.dll")] extern "stdcall" // 根据C#函数的约定选择适当的CallingConvention std::string MarshalStringReturnFunction(string input); ``` 3. **处理字符串 Marshalling**:由于C++和.NET处理字符串的方式不同,可能会涉及到`arshal_as`或`MarshalAs(UnmanagedType.LPStr)`来处理输入输出的字符串。 4. **调用函数并处理结果**: ```cpp string result = MarshalStringReturnFunction(L"Input String"); cout << "Result from C# DLL: " << result.c_str() << endl; ``` 5. **处理异常**:C#中的异常可能需要在PInvoke上下文中捕获并处理,你可以使用`try-catch`块来做到这一点。 注意,PInvoke会有一些性能开销,因为每次函数调用都会涉及到内存复制和类型转换。因此,只应在必要时使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值