无法从dll

时间:2017-06-12 09:05:41

标签: c++ dll

我用两个文件创建了非常简单的dll:

                                            dllmain.cpp
                                            dll.hpp

dllmain.cpp:

#include "dll.h"
#include <windows.h>

messageimport::messageimport()
{

}

messageimport::~messageimport()
{

}

void messageimport::message()
{
MessageBox(0, "Hello World from DLL!\n","Hi",MB_ICONINFORMATION);
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
{
  switch(fdwReason)
    {
    case DLL_PROCESS_ATTACH:
    {
        break;
    }
    case DLL_PROCESS_DETACH:
    {
        break;
    }
    case DLL_THREAD_ATTACH:
    {
        break;
    }
    case DLL_THREAD_DETACH:
    {
        break;
    }
}

/* Return TRUE on success, FALSE on failure */
return TRUE;

}

dll.hpp:

#ifndef _DLL_H_
#define _DLL_H_

#if BUILDING_DLL
#define DLLIMPORT __declspec(dllexport)
#else
#define DLLIMPORT __declspec(dllimport)
#endif

class DLLIMPORT messageimport
{
public:
    messageimport();
    virtual ~messageimport();

    void message();


};

#endif

我使用名为dlltest.cpp

的文件中的dll

dlltest.cpp:

#include <iostream>
#include <windows.h>

using namespace std;

typedef unsigned int(WINAPI* message)(void);

int main(int argc, char** argv) {

HMODULE dll = LoadLibrary("Call.dll");
if(dll != NULL){

    cout << "loaded dll \n";

    message func = (message)GetProcAddress(dll, "message");

    if(func != NULL)
    {
        cout << "found function";
    }else{

        cout << "cant find function";
    }
}else{

    cout << "cant load the library";
}




return 0;
}

但是当我运行dlltest.cpp时它的输出就像

加载dll 找不到功能

1 个答案:

答案 0 :(得分:0)

这是一篇很好的文章,为什么你can't simple export classes

可能您并不真正想要导出类,因此只需从类中汇出 消息 并导出为函数。对不起,我没有提供任何代码,没有c ++ atm。