关于c ++字符串转换 [英] regarding c++ string conversion

查看:76
本文介绍了关于c ++字符串转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误1错误C2664:``PostMessageW'':无法将参数4从``CHAR [32] [32]''转换为``LPARAM''d:\ him \ 1dvrtestfront end \ gdsapi.cpp 431
如何将char [] []转换为LPARAM

Error 1 error C2664: ''PostMessageW'' : cannot convert parameter 4 from ''CHAR [32][32]'' to ''LPARAM'' d:\him\1dvrtestfront end\gdsapi.cpp 431
how to convert char[][] to LPARAM

推荐答案

只需进行强制转换.但是要注意PostMessage函数.由于它是异步的,因此必须确保从调用函数退出后,发送的指针将保持有效.

Just do a cast. But be careful about PostMessage function. Since it is asynchronous, you must make sure that the pointer you send will remain valid after you exit from the calling function.

void function()
{
    char aFewStrings[32][32];
    ...
    //don't do that!!
    //because aFewStrings will get out of scope and will be destroyed
    //so your message handler will probably get a bad pointer...
    PostMessage(..., (LPARAM)(void*)aFewString);
}



如果要同步呼叫,请使用SendMessage.或者只是确保您发送的指针保持有效(使用new进行分配,或将其保留为类的成员变量).



Use SendMessage if you want a synchronous call. Or just make sure the pointer you send remains valid (allocate it with new, or keep it as a member variable of your class).


此外,使用Visual Studio,它会自动在末尾填写该"W",从而得到"PostMessageW".你不要这个只需摆脱W,您将剩下原始的"PostMessage".那就是你想要的.

-Bob
Also, using Visual Studio, it auto-fills in that "W" at the end, resulting in "PostMessageW". You don''t want this. Just get rid of the W and you are left with raw "PostMessage". That''s what you want.

- Bob


如果您使用unicode函数PostMessageW,则应使用unicode字符(WCHAR,wchar_t或unsigned short).
但是"Olivier Levrey"是正确的,您应该从不发布一条带有指向应用程序消息循环指针的消息.如果使用new发布指针,则可能会发生内存泄漏.对于指针,应始终使用SendMessage之类的同步调用.这就是为什么内存的所有者(堆栈,new或malloc等)始终是调用方的原因. 问候.
If you use unicode functions PostMessageW you should use unicode characters (WCHAR, wchar_t, or unsigned short).
But ''Olivier Levrey'' is right, you should NEVER post a message with pointers to the applications message loop. If you post pointers with new you possibly will get memory leaks. For pointers you should always use synchronous calls like SendMessage. Thats why the owner of the memory (stack or new or malloc etc.) is always the caller.
Regards.


这篇关于关于c ++字符串转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆