用Qt開發時,調用系統API函數時的問題,在win7及以上系統沒什么大問題。在xp下出現了標題描述的現象,導致無法啟動程序。看了下網上的解決方案如下:
這里我要討論的是在 WinSDK v7.0中的一些不友好的錯誤。如果你是一名開發者,並且當前使用的是VS2010編譯器自帶的 WinSDK v7.0,那么個別時候當你執行程序時,可能遇到這樣的錯誤提示:The procedure entry point K32*** could not be located in the dynamic link library KERNEL32.dll
中文版本的就是:無法定位程序輸入點K32EnumProcessModules於動態鏈接庫KERNEL32.dll上。這樣的錯誤提示一般會出現在非 Windows7 或者 Windows Server 2008 R2 的系統上面。
下面我解釋下為什么會出現這樣的錯誤。因為一些性能的問題,在Windows7 和 Windows Server 2008 R2 系統上,微軟把一些API函數從Psapi.dll 移到了 Kernel32.dll 動態庫中,並在VS2010編譯器自帶的 WinSDK v7.0版本上面做了處理。這樣的設計在Windows7 和 Windows Server 2008 R2系統上面沒有問題,但是如果你用vs2010編譯的程序運行在Win7之前的系統上,那么肯定會遇到剛才說的錯誤。因為老系統的KERNEL32.dll中根本沒有那些被移植過去的函數,所以肯定會執行失敗。
受影響的函數如下:
- //Snapshot from Psapi.lib – WinSDK V7.0*
- #if (PSAPI_VERSION > 1)
- #define EnumProcesses K32EnumProcesses
- #define EnumProcessModules K32EnumProcessModules
- #define EnumProcessModulesEx K32EnumProcessModulesEx
- #define GetModuleBaseNameA K32GetModuleBaseNameA
- #define GetModuleBaseNameW K32GetModuleBaseNameW
- #define GetModuleFileNameExA K32GetModuleFileNameExA
- #define GetModuleFileNameExW K32GetModuleFileNameExW
- #define GetModuleInformation K32GetModuleInformation
- #define EmptyWorkingSet K32EmptyWorkingSet
- #define QueryWorkingSet K32QueryWorkingSet
- #define QueryWorkingSetEx K32QueryWorkingSetEx
- #define InitializeProcessForWsWatch K32InitializeProcessForWsWatch
- #define GetWsChanges K32GetWsChanges
- #define GetWsChangesEx K32GetWsChangesEx
- #define GetMappedFileNameW K32GetMappedFileNameW
- #define GetMappedFileNameA K32GetMappedFileNameA
- #define EnumDeviceDrivers K32EnumDeviceDrivers
- #define GetDeviceDriverBaseNameA K32GetDeviceDriverBaseNameA
- #define GetDeviceDriverBaseNameW K32GetDeviceDriverBaseNameW
- #define GetDeviceDriverFileNameA K32GetDeviceDriverFileNameA
- #define GetDeviceDriverFileNameW K32GetDeviceDriverFileNameW
- #define GetProcessMemoryInfo K32GetProcessMemoryInfo
- #define GetPerformanceInfo K32GetPerformanceInfo
- #define EnumPageFilesW K32EnumPageFilesW
- #define EnumPageFilesA K32EnumPageFilesA
- #define GetProcessImageFileNameA K32GetProcessImageFileNameA
- #define GetProcessImageFileNameW K32GetProcessImageFileNameW
- #endif
復制代碼
通過上面的解釋,你應該明白為什么出現那樣的錯誤了吧?也大體上知道怎么樣改正這個錯誤了。不知道大家注意到沒有,有個條件判斷#if (PSAPI_VERSION > 1),也就是說只有當PSAPI_VERSION被定義為大於1的數值時才有這樣的問題,所以解決方案就是將 PSAPI_VERSION 定義為小於等於1的數值就可以啦,如下:
要加在#include <Psapi.h>上面
#ifndef PSAPI_VERSION
#define PSAPI_VERSION 1
#endif
#include <Tlhelp32.h>
#include <Psapi.h>
#pragma comment(lib, "Psapi.lib")
至此可以解決問題,注意紅色字體部分,開始只加了上半部分,直接編譯異常,把后面一行紅色部分加入后才可以。剛開始涉足Qt,碰到的問題網上大都能解決,該問題列出供自己參考。