windows – vs 2010:错误LNK2028:未解析的令牌(0A000342)“extern”C“int __stdcall

前端之家收集整理的这篇文章主要介绍了windows – vs 2010:错误LNK2028:未解析的令牌(0A000342)“extern”C“int __stdcall前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
以下代码取自 here.我在Windows 7上工作时删除了所有Windows NT部分.

我复制了这段代码并在visual studio 2010中运行(新项目 – > VC – > CLR-> CLR控制台……).但它提供了许多未解决的外部’c’错误,如代码下方列出的那样.我犯了什么错?

#define STRICT  1 

#include <windows.h>
#include <iostream>
using namespace std;

BOOL CALLBACK EnumWindowsProc(HWND hWnd,LPARAM lParam) {
DWORD dwThreadId,dwProcessId;
HINSTANCE hInstance;
char String[255];
HANDLE hProcess;
if (!hWnd)
return TRUE;        // Not a window
if (!::IsWindowVisible(hWnd))
return TRUE;        // Not visible
if (!SendMessage(hWnd,WM_GETTEXT,sizeof(String),(LPARAM)String))
return TRUE;        // No window title
hInstance = (HINSTANCE)GetWindowLong(hWnd,GWL_HINSTANCE);
dwThreadId = GetWindowThreadProcessId(hWnd,&dwProcessId);
hProcess = OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwProcessId);
cout << hWnd << ' ' << dwProcessId << '\t' << String << '\t';
cout << "(None)\n";
CloseHandle(hProcess);
return TRUE;
}

int main(int argc,char *argv[],char *envp[]) {
EnumWindows(EnumWindowsProc,NULL);
return 0;
}

这给出了以下错误(和其他类似的未解决的extern C错误)

1>wndowfind.obj : error LNK2028: unresolved token (0A000342) "extern "C" int __stdcall       
 EnumWindows(int (__stdcall*)(struct HWND__ *,long),long)"     3
 (?EnumWindows@@$$J18YGHP6GHPAUHWND__@@J@ZJ@Z) referenced in function "int __cdecl 
 main(int,char * * const,char * * const)" (?main@@$$HYAHHQAPAD0@Z)

 1>wndowfind.obj : error LNK2028: unresolved token (0A000346) "extern "C" unsigned long  
 __stdcall GetWindowThreadProcessId(struct HWND__ *,unsigned long *)"    
 (?GetWindowThreadProcessId@@$$J18YGKPAUHWND__@@PAK@Z) referenced in function "int __stdcall 
 EnumWindowsProc(struct HWND__ *,long)" (?EnumWindowsProc@@$$FYGHPAUHWND__@@J@Z)

 1>wndowfind.obj : error LNK2028: unresolved token (0A000347) "extern "C" long __stdcall 
 GetWindowLongW(struct HWND__ *,int)" (?GetWindowLongW@@$$J18YGJPAUHWND__@@H@Z) referenced in 
 function "int __stdcall EnumWindowsProc(struct HWND__ *,long)" 
 (?EnumWindowsProc@@$$FYGHPAUHWND__@@J@Z)

 1>wndowfind.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall  
 EnumWindows(int (__stdcall*)(struct HWND__ *,long)" 
 (?EnumWindows@@$$J18YGHP6GHPAUHWND__@@J@ZJ@Z) referenced in function "int __cdecl 
 main(int,char * * const)" (?main@@$$HYAHHQAPAD0@Z)

 1>c:\users\afnan\documents\visual studio 2010\Projects\wndowfind\Debug\wndowfind.exe : fatal 
 error LNK1120: 10 unresolved externals
 1>
 1>Build Failed.

更新

通过包含库(如答案中所示),我能够成功运行该程序.但我无法理解为什么只有字符串的第一个字符打印而不是完整字符,如输出中所示:

00010060 2652   S       (None)
002502B2 5820   C       (None)
00090402 5160   w       (None)
00050392 5160   w       (None)
00060292 3520   F       (None)
000C02BA 3520   M       (None)
0001021A 3736   E       (None)
00040018 896    I       (None)
00010170 3580   A       (None)
0002003E 2684   D       (None)
00030316 4956   N       (None)
000202DE 3736   D       (None)
0001031E 2652   S       (None)
000100EA 2652   P       (None)

在上面的输出中,S实际上是“开始”,C是“控制台”等我通过间谍工具确认.
如何打印完整的字符串而不是第一个字符?

默认情况下,CLR项目不包括标准Windows库,例如user32.lib.

编辑项目属性,找到Linker Inputs选项,并添加kernel32.lib user32.lib advapi32.lib,它们是Win32代码所需的常用库.

原文链接:https://www.f2er.com/windows/364884.html

猜你在找的Windows相关文章