0

マネージ C++ コードから .dll を動的にロードする必要があります。以下のコードはこれを行います

[DllImport("kernel32.dll", SetLastError = true,
        CharSet = CharSet::Unicode, ExactSpelling = true,
        CallingConvention = CallingConvention::StdCall)]
    static HMODULE LoadLibrary(LPCTSTR lpFileName);

    [DllImport("kernel32.dll", SetLastError = true,
        CharSet = CharSet::Unicode, ExactSpelling = true,
        CallingConvention = CallingConvention::StdCall)]
    static BOOL FreeLibrary(HMODULE hModule);

    [DllImport("kernel32.dll", SetLastError = true,
        CharSet = CharSet::Unicode, ExactSpelling = true,
        CallingConvention = CallingConvention::StdCall)]
    static FARPROC GetProcAddress(HMODULE hModule, LPCSTR lpProcName);

    typedef int (*CFunction)();

    int _tmain()
    {
        HMODULE pLib = LoadLibrary(TEXT("FunctionDLL.dll"));
        if(pLib != 0)
        {
            FARPROC function = GetProcAddress(pLib, "QFunction");
            if (function != 0)
                MessageBox::Show(Convert::ToString(function()));

            FreeLibrary(pLib);
        }
        else
        {
            MessageBox::Show(L"Error", L"Couldn't load ell");
            Close();
        }

        return 0;
    }

ただし、関数は常に null です。dll が間違っていると思います。dll のコードの下:

.h ファイル

#ifdef __FUNCTIONDLL__
#define __FUNCTIONDLL__

#ifdef FUNCTIONDLL_EXPORTS
    #define FUNCTIONDLL_API __declspec(dllexport)
#else
    #define FUNCTIONDLL_API __declspec(dllimport)
#endif

#ifdef __cplusplus
extern "C"
{
#endif

FUNCTIONDLL_API int QFunction();

#ifdef __cplusplus
}
#endif // __cplusplus

#endif // __FUNCTIONDLL__

.cpp ファイル

#include "FunctionDLL.h"

int QFunction()
{
    return 42;
}
4

1 に答える 1

0

C ++プロジェクトに.defがありますか?.defを追加するだけではない場合

LIBRARY   FunctionDLL
EXPORTS
  QFunction   @1
于 2012-11-24T20:01:59.477 に答える