[英]Error on building C++ code in node-gyp when using PathFileExists and PathQuoteSpaces from Shlwapi
我真的是 C++ 的菜鳥,但我有 C++ 代碼,當我運行node-gyp build
時它會拋出:
error LNK2001: unresolved external symbol __imp_PathQuoteSpacesA
和
error LNK2001: unresolved external symbol __imp_PathFileExistsA
。
我的代碼:
#include <node.h>
#include <Shlwapi.h>
// Full path and name of the API
static char g_szAPI[MAX_PATH] = "";
// Handle on the QuickVision window used to comunicate
static HWND g_hWndQV = NULL;
BOOL StartQV(LPSTR lpszCmdLine)
{
STARTUPINFO siStartInfo;
PROCESS_INFORMATION processInfo;
char szCmd[1024];
DWORD retval = 0;
// First we get the installation path and name from the registry
if (!GetApiFullNameFromRegistry(g_szAPI, sizeof(g_szAPI))) {
return FALSE;
}
// If the path name has spaces, the full path name must be enclosed with quotation marks before calling CreateProcess
PathQuoteSpaces(g_szAPI); //<----throw error LNK2001: unresolved external symbol __imp_PathQuoteSpacesA
return (BOOL)retval;
}
BOOL GetApiFullNameFromRegistry(char* szAPI, DWORD dwBufLen)
{
HKEY hKey=0;
LONG retval= -1;
BOOL Flag = FALSE;
if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGISTRY_KEY, 0, KEY_QUERY_VALUE|KEY_WOW64_32KEY, &hKey))
{
retval = RegQueryValueEx(hKey, "MjExec", NULL, NULL, (LPBYTE)szAPI, &dwBufLen);
RegCloseKey(hKey);
}
Flag = ERROR_SUCCESS == retval && dwBufLen != 0;
if (Flag) {
Flag = PathFileExists(szAPI); //<----throw error LNK2001: unresolved external symbol __imp_PathFileExistsA
}
return Flag;
}
當我注釋引發錯誤的代碼時,代碼構建成功。
需要在腳本頂部添加#pragma comment(lib, "Shlwapi.lib")
來解決問題
結果:
#pragma comment(lib, "Shlwapi.lib") // <-- adding this
#include <node.h>
#include <Shlwapi.h>
// Full path and name of the API
static char g_szAPI[MAX_PATH] = "";
// Handle on the QuickVision window used to comunicate
static HWND g_hWndQV = NULL;
BOOL StartQV(LPSTR lpszCmdLine)
{
STARTUPINFO siStartInfo;
PROCESS_INFORMATION processInfo;
char szCmd[1024];
DWORD retval = 0;
// First we get the installation path and name from the registry
if (!GetApiFullNameFromRegistry(g_szAPI, sizeof(g_szAPI))) {
return FALSE;
}
// If the path name has spaces, the full path name must be enclosed with quotation marks before calling CreateProcess
PathQuoteSpaces(g_szAPI); // <-- now it works fine
return (BOOL)retval;
}
BOOL GetApiFullNameFromRegistry(char* szAPI, DWORD dwBufLen)
{
HKEY hKey=0;
LONG retval= -1;
BOOL Flag = FALSE;
if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGISTRY_KEY, 0, KEY_QUERY_VALUE|KEY_WOW64_32KEY, &hKey))
{
retval = RegQueryValueEx(hKey, "MjExec", NULL, NULL, (LPBYTE)szAPI, &dwBufLen);
RegCloseKey(hKey);
}
Flag = ERROR_SUCCESS == retval && dwBufLen != 0;
if (Flag) {
Flag = PathFileExists(szAPI); // <-- now it works fine
}
return Flag;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.