简体   繁体   English

使用来自 Shlwapi 的 PathFileExists 和 PathQuoteSpaces 时在 node-gyp 中构建 C++ 代码时出错

[英]Error on building C++ code in node-gyp when using PathFileExists and PathQuoteSpaces from Shlwapi

I'm really a noob in C++, but I have C++ code and when I run node-gyp build it throws:我真的是 C++ 的菜鸟,但我有 C++ 代码,当我运行node-gyp build时它会抛出:
error LNK2001: unresolved external symbol __imp_PathQuoteSpacesA
and
error LNK2001: unresolved external symbol __imp_PathFileExistsA . error LNK2001: unresolved external symbol __imp_PathFileExistsA

My code:我的代码:

#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;
}

When I comment the code that throws an error, the code is built successfully.当我注释引发错误的代码时,代码构建成功。

It was necessary to add #pragma comment(lib, "Shlwapi.lib") at the top of the script to solve the problem需要在脚本顶部添加#pragma comment(lib, "Shlwapi.lib")来解决问题

Result:结果:

#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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM