简体   繁体   中英

WinApi unicode problem

I have the code:

wchar_t* temp = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1000 * sizeof(wchar_t));
wchar_t* temp2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1000 * sizeof(wchar_t));

GetTempPathW(1000, temp);

GetTempFileNameW(temp, L"vdr", GetCurrentProcessId(), temp2);

HeapFree(GetProcessHeap(), 0, temp2);

MessageBoxW(0,temp2,0,0);

It should make a file "vdrXXXX.tmp" with XXXX to be the processs id. But it returns strange things. Like unicode symbols and then part of the path. What I'm doing wrong?

You are freeing the memory before calling MessageBox() .

In any case, there's no need to use HeapAlloc() , just use malloc() . In fact, since GetTempFileName() has a maximum buffer size of MAX_PATH , it's easiest to do this with stack allocated buffers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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