0

嗨,我收到一个运行时错误,说周围的堆栈szDeviceType已损坏,但我不知道,我对 c++ 很陌生,所以我不会有最多的知识。任何帮助将不胜感激。

HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
int szDeviceType = 0;
char lpszString[MAX_PATH];
LPWSTR szComport = NULL;
LPWSTR szNumChannels = NULL;
char szChannels[MAX_PATH];
char szPort[MAX_PATH];

hr = WcaInitialize(hInstall, "GetDatascanInfo");
ExitOnFailure(hr, "Failed to initialize");

WcaLog(LOGMSG_STANDARD, "Initialized.");

hr = WcaGetIntProperty(L"DEVICETYPE",&szDeviceType);
ExitOnFailure(hr, "failed to get Device Type");

hr = WcaGetFormattedProperty(L"COMPORT",&szComport);
ExitOnFailure(hr, "failed to get Com Port");

wcstombs(szPort, szComport, 500);

hr = WcaGetProperty(L"NUMCHANNELS",&szNumChannels);
ExitOnFailure(hr, "failed to get Com Port");

wcstombs(szChannels, szNumChannels, 500);

    if(szDeviceType == 2)
    {
        strcat(lpszString, "datascan");
        strcat(lpszString, szPort);
        strcat(lpszString, "DATASCAN 7000,DS:");
        strcat(lpszString, szChannels);

    }
    if (szDeviceType == 3)
    {
        strcat(lpszString, "solo");
        strcat(lpszString, szPort);
        strcat(lpszString, "DATASCAN SOLO,SA:");
        strcat(lpszString, szChannels);

    }
    if (szDeviceType == 4)
    {
        strcat(lpszString, "dataweb");
        strcat(lpszString, szPort);
        strcat(lpszString, "DATAWEB,DW:");
        strcat(lpszString, szChannels);
    }

    hr = MsiSetProperty(hInstall, "DATASCANINFO",  lpszString);
    ExitOnFailure(hr, "failed to set DATASCANINFO");


LExit:
er = SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE;
return WcaFinalize(er);
}
4

1 回答 1

1

这看起来很可疑:

wcstombs(szPort, szComport, 500);

最后一个参数是要写入的最大字符数,但您的数组大小仅为MAX_PATH260。

您还使用strcat附加到lpszString而不检查是否溢出。您可能应该切换到使用strcat_s具有边界检查的功能。

于 2012-10-04T16:04:20.077 回答