2012-07-10 120 views
0

这里是我的代码:VC++编译错误的DLL项目

#include "stdafx.h" 
#include <Windows.h> 

extern "C" int __stdcall myfunction(); 

BOOL WINAPI DllMain (HINSTANCE hin, DWORD reason, LPVOID lpvReserved); 

int __stdcall myfunction() 
{ 
     MessageBoxW(NULL,L"Question",L"Title",MB_OK); 
     return 0; 
} 

BOOL WINAPI DllMain (HINSTANCE hin, DWORD reason, LPVOID lpvReserved) 
{ 
    return TRUE; 
} 

当我编译显示这些错误:

错误LNK2028:参考辛博尔 (令牌)未解决(0A000027)“外部“C” INT STDCALL MessageBoxW(结构HWND *,wchar_t的常量*,wchar_t的常量*,无符号 INT) “(?MessageBoxW @@ $$ J216YGHPAUHWND __ @@ @ PB_W1I Z)中的函数 ” 外部的 “C” int __stdcall myf结(无效) “(myfunction的@@ $$ J10YGHXZ?)

错误LNK2019:外部符号” 外部的 “C” INT STDCALL MessageBoxW(结构HWND *,wchar_t的常量*,wchar_t的常量*,无符号 INT )”(?MessageBoxW @@ $$ J216YGHPAUHWND __ @@ @ PB_W1I Z)未解决在 函数中使用的 “外部的 ”C“ INT __stdcall MyFunction的(无效)” (?myfunction的@@ $$ J10YGHXZ)

我不明白错误及其原因在哪里。 如果有人能帮助我解决它,我会非常感谢:)

回答

0

extern "C"需求是在功能上,而不是定义:

int __stdcall myfunction(); 

extern "C" int __stdcall myfunction() 
{ 
     MessageBoxW(NULL, L"Question", L"Title", MB_OK); 
     return 0; 
} 

,而不是附加extern "C"到这一切不过,你可以将其包装在预解析条件中:

int __stdcall myfunction(); 

#ifdef __cplusplus 
    extern "C" { 
#endif 

int __stdcall myfunction() 
{ 
     MessageBoxW(NULL, L"Question", L"Title", MB_OK); 
     return 0; 
} 

#ifdef __cplusplus 
    } 
#endif 
+0

此代码返回错误C2732:链接规范与'myfunction''的早期规范相矛盾。 – 2012-07-10 13:44:26

1

谢谢大家,但问题在于user32.lib。

#include "stdafx.h" 
#include <Windows.h> 

#pragma comment(lib,"user32.lib"); //Missing lib (No compile errors) 

BOOL __stdcall DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID lpReserved) { 
    return TRUE; 
} 

extern "C" __declspec(dllexport) void __stdcall SomeFunction() { 
    MessageBoxA(NULL, "Hello", "HELLO", 0x000000L); //0x000000L = MB_OK 
} 

我希望这会对像我这样的noobs有帮助。