When replacing from "wcslen" to "strnlen_s", is it the correct way to typecast with "const char*"?
In the below function, I am replacing
if (szPath[wcslen(szPath) - 1] != L'\')
with
if (szPath[strnlen_s((const char*)szPath, sizeof(szPath)) - 1] != L'\')
Below is the code snippet:
bool Activation::Execute()
{
    HRESULT hr = S_OK;
    typedef ULONG(APIENTRY *ActivateNowProc)(int);
    wchar_t szPath[MAX_PATH];
    std::fill_n(szPath, MAX_PATH, L'\0');
    //Gets the  CSIDL Program files path
    hr = SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL, NULL, szPath);
    if (SUCCEEDED(hr))
    {
        _tcscat_s(szPath, sizeof(szPath) / sizeof(TCHAR), MSC_PATH);
    }
    if (IsDirectoryExist(szPath))
    {
        std::wstringstream strActivationLibPath;
        strActivationLibPath << szPath;
        //if (szPath[wcslen(szPath) - 1] != L'\\') 
        if (szPath[strnlen_s((const char*)szPath, sizeof(szPath)) - 1] != L'\\')
            strActivationLibPath << L"\\";
        strActivationLibPath << OOBE_FOLDER_NAME << L"\\" << ACTIVATION_LIB_NAME;
        DWORD dwErr = McValidateModule(strActivationLibPath.str().c_str());
        if (dwErr != ERROR_SUCCESS) 
        {
            return false;
        }
        HMODULE hModule = LoadLibrary(strActivationLibPath.str().c_str());
        if (hModule == 0)
        {
            return false;
        }
        ActivateNowProc ActivateNow = (ActivateNowProc)GetProcAddress(hModule, ACTIVATION_PROC_NAME);
        if (ActivateNow)
        {
            long retVal = ActivateNow(1);
            if (retVal == E_FAIL)
            {
                FreeLibrary(hModule);
                return false;
            }
            else
            {
                ::Sleep(2000);
                CheckProcessRunningAndWait(SYNCPROCESSNAME);
            }
        }
        else
        {
            FreeLibrary(hModule);
            return false;
        }
        FreeLibrary(hModule);
        return true;
    }
    return false;
}
Aucun commentaire:
Enregistrer un commentaire