将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")


本文转自斯克迪亚博客园博客,原文链接:http://www.cnblogs.com/sgsoft/archive/2009/12/24/1631651.html,如需转载请自行联系原作者


相关文章
mvc.net分页查询案例——DLL数据访问层(HouseDLL.cs)
mvc.net分页查询案例——DLL数据访问层(HouseDLL.cs)
|
C++
C/C++ DLL 简单实现
C/C++ DLL 简单实现
289 0
|
7月前
|
API C++ Windows
Visual C++运行库、.NET Framework和DirectX运行库的作用及常见问题解决方案,涵盖MSVCP140.dll丢失、0xc000007b错误等典型故障的修复方法
本文介绍Visual C++运行库、.NET Framework和DirectX运行库的作用及常见问题解决方案,涵盖MSVCP140.dll丢失、0xc000007b错误等典型故障的修复方法,提供官方下载链接与系统修复工具使用指南。
1598 2
|
10月前
|
C++ Windows
.NET Framework安装不成功,下载`NET Framework 3.5`文件,Microsoft Visual C++
.NET Framework常见问题及解决方案汇总,涵盖缺失组件、安装失败、错误代码等,提供多种修复方法,包括全能王DLL修复工具、微软官方运行库及命令行安装等,适用于Windows系统,解决应用程序无法运行问题。
1394 3
|
C++ 数据格式
LabVIEW传递接收C/C++DLL指针
LabVIEW传递接收C/C++DLL指针
687 1
|
IDE 开发工具 C++
[记录][问题]Win32调用C++/WinRT DLL
[记录][问题]Win32调用C++/WinRT DLL
248 0
|
C++ Windows
C++ --- Dll文件的生成与调用(二)之动态库注入技术
C++ --- Dll文件的生成与调用(二)之动态库注入技术
429 0
|
存储 C++
C++ dll 传 string 类 问题
C++ dll 传 string 类 问题
215 0
|
Java API Android开发
Java通过JNI调用C++的DLL库
Java通过JNI调用C++的DLL库
412 0
|
C++ 计算机视觉 Python
【Py调用C++】使用使用python调用C++生成dll处理图像(OPENCV)
【Py调用C++】使用使用python调用C++生成dll处理图像(OPENCV)
954 0
【Py调用C++】使用使用python调用C++生成dll处理图像(OPENCV)