将C++ DLL Wrap后供.NET 调用

What you would do is providing call stubs from your DLL that then are accessible via PInvoke, e.g.

//wrapper.cpp
#include "manufacturer.h"
#pragma comment(lib,"manufacturer.lib")

extern "C" __declspec(dllexport) int WrapperCallManufacturerFunc1(int a, int b)
{
    return ManufacturerFunc1(a,b);
}

extern "C" __declspec(dllexport) char* WrapperCallManufacturerFunc2(char* pString)
{
    return ManufacturerFunc2(pString);
}

extern "C" __declspec(dllexport) double WrapperCallManufacturerFunc3(double d)
{
    return ManufacturerFunc3(d);
}

extern "C" __declspec(dllexport) void WrapperCallManufacturerFunc4()
{
    ManufacturerFunc4();
}

That's basically it. The 'extern "C"' statement is used to tell the compiler to not apply C++ name mangling, i.e. the functions names are not decorated and exported 'as is'. The return types resemble the return types of the functions in the .lib you want to call, except for 'void' functions where your function is 'void' also, yet the 'return' statement is not used. Then you can access the wrapper functions like

Declare Auto Function ManufacturerFunc1  Lib "wrapper.dll" Alias "WrapperCallManufacturerFunc1 " ( _
    ByVal a As Integer, _
    ByVal b As Integer) _
    As Integer

or

Imports System.Runtime.InteropServices
Public Class Win32
    Declare Auto Function WrapperCallManufacturerFunc1 Lib "wrapper.dll" _
       (ByVal a As Integer, _
        ByVal b As Integer) As Integer
End Class

See also http://msdn2.microsoft.com/en-us/library/172wfck9(vs.80).aspx ("Walkthrough: Calling Windows APIs") and http://msdn2.microsoft.com/en-us/library/w4byd5y4(VS.80).aspx ("Creating Prototypes in Managed Code")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值