为什么DLL内的HelloWorld函数指针为NULL?

时间:2012-06-17 08:58:29

标签: winapi dll

我试图使用运行时动态链接在SampleDLL.dll中调用我的HelloWorld函数。这是代码:

//SampleApp.cpp
#include "stdafx.h"

typedef VOID (*DLLPROC) (LPTSTR);

int APIENTRY WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow) {
        HINSTANCE hinstDLL;
        DLLPROC HelloWorld;
        BOOL fFreeDLL;
        hinstDLL = LoadLibraryA("SampleDLL.dll");
        if (hinstDLL != NULL)
        {
            HelloWorld = (DLLPROC) GetProcAddress(hinstDLL,
                "HelloWorld");
            if (HelloWorld != NULL)
                (HelloWorld);

            fFreeDLL = FreeLibrary(hinstDLL);
        }
}

//SampleDLL.cpp

#include "stdafx.h"
#define EXPORTING_DLL
#include "SampleDLL.h"

void HelloWorld() {
    MessageBox(NULL, TEXT("Hello World"),
        TEXT("In a DLL"), MB_OK);
}

//SampleDLL.h

#ifndef INDLL_H
#define INDLL_H

#ifdef EXPORTING_DLL
extern _declspec(dllexport) void HelloWorld();
#else
extern _declspec(dllimport) void HelloWorld();
#endif

#endif

一切都有效,直到第HelloWorld =...行。缺少HelloWorld函数指针。为什么会这样呢?我尝试加载时间动态链接,它的工作原理。我尝试在SampleApp.exe中使用depends.exe,虽然我可以获得DLL的句柄,但它并没有说我的代码依赖于SampleDLL.dll。有线索吗?

0 个答案:

没有答案