c++ - Windows 中的 Exe 在调用 GetWindowsDirectory 时 append wstring 时崩溃

标签 c++ windows crash append wstring

我正在 append wstring。我在 wstring 中有“C:\Windows”。我正在向它 append “\system32\config\”。通过以下代码。

#define MAX_PATH 260
 wstring wstrWindowsDirectory;
    if(0 != GetWindowsDirectory((LPWSTR)wstrWindowsDirectory.c_str(),MAX_PATH)) 
    {
        if(!wstrWindowsDirectory.empty()) {
            wstrWindowsDirectory += L"\System32\\config\\";
        }
}

But its crashing..
please help me.. 
Thanks in advance.

最佳答案

当您通过 c_str() 将内容写入字符串时,它不会自动增长。

因此,无法保证 wstrWindowsDirectory 足够大以包含您尝试写入其中的路径。

在调用 GetWindowsDirectory() 之前,您需要使用 wstring::reserve()

关于c++ - Windows 中的 Exe 在调用 GetWindowsDirectory 时 append wstring 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41095639/

相关文章:

c++ - C++11 是否允许 vector<const T>?

c++ - Visual Studio 无法打开源文件 <vulkan.h>

c++ - Sqlite3 和 pthread,双重释放或损坏(出)

Windows CMD : Echo command without executing it

xcode4 - 使用Xcode 4.2为iOS 3.1.3编译

c++ - 事务性、内存中、对象/键/值存储库?

c++ - 远程进程的 WM_PAINT

python - 如何只压缩文件夹内的文件而不压缩子文件夹?

iphone - Titanium Appcelerator 最终包构建崩溃(包括日志)

jquery - jQuery-查找确切的点击目标(代码在Chrome中有效,但在Firefox中无效)