DLL wrapper sample

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

原文链接: https://www.cnblogs.com/sgsoft/archive/2010/08/31/1813463.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/14542

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月7日 下午2:12
下一篇 2023年2月7日 下午2:12

相关推荐