简体   繁体   English

WinApi unicode问题

[英]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. 它应将带有XXXX的文件“ vdrXXXX.tmp”作为进程ID。 But it returns strange things. 但是它返回奇怪的东西。 Like unicode symbols and then part of the path. 像unicode符号,然后是路径的一部分。 What I'm doing wrong? 我做错了什么?

You are freeing the memory before calling MessageBox() . 您将在调用MessageBox()之前释放内存。

In any case, there's no need to use HeapAlloc() , just use malloc() . 无论如何,都不需要使用HeapAlloc() ,只需使用malloc() In fact, since GetTempFileName() has a maximum buffer size of MAX_PATH , it's easiest to do this with stack allocated buffers. 实际上,由于GetTempFileName()的最大缓冲区大小为MAX_PATH ,所以最简单的方法是使用堆栈分配的缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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