MFC:如何将 DWORD 和 BYTE 转换为 LPCTSTR 以在 MessageBox 中显示
我正在使用带有“使用 Unicode 字符集”选项的 VS2005
typedef unsigned char BYTE;
typedef unsigned long DWORD;
BYTE m_bGeraet[0xFF];
DWORD m_dwAdresse[0xFF];
如何使代码工作?
MessageBox (m_bGeraet[0], _T("Display Content"));
MessageBox (m_dwAdresse[0], _T("Display Content"));
I'm using VS2005 with "using Unicode Character Set" option
typedef unsigned char BYTE;
typedef unsigned long DWORD;
BYTE m_bGeraet[0xFF];
DWORD m_dwAdresse[0xFF];
How do i make the code work?
MessageBox (m_bGeraet[0], _T("Display Content"));
MessageBox (m_dwAdresse[0], _T("Display Content"));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您可能需要一些有关 C 语言本身的帮助,我建议您找到一本与 Windows 编程无关的 C 初学者书籍。
MessageBox() 只显示 C 风格的字符串,这些字符串是
char
类型的数组,其中包含 ASCII 值为 0 的字符。这个零字符是NUL
字符,此类字符串是称为“NUL 终止”或“零终止”。打印字符串时仅显示 NUL 之前的字符,连接字符串时仅复制 NUL 之前的字符。但是,如果数组中没有 NUL 字符,则该字符串未正确终止,并且尝试显示它可能会导致崩溃,或显示“垃圾”,如:“Can I have a beer?# BT&I10)aaX?。MessageBox() 的 szTitle 和 szText 参数需要
char *
,它们是指向此类字符串的指针。如果您尝试传递
BYTE
而不是char *
,则 BYTE 的值将被错误地视为地址。 MessageBox() 将尝试访问 BYTE“指定”值处的内存,并且将发生访问冲突。解决此问题的一种方法是分配一个
char
类型的缓冲区,并使用snprintf_s
将数据值转录为字符串表示形式。例如:
将显示一个消息框,其中包含一条类似“Geraet = 0x35”的消息。
It looks like you might need some help with the C language itself, and I recommend you find a beginner's book on C that is not about Windows programming.
MessageBox() only displays C-style strings which are arrays of type
char
which contain a character with ASCII value 0. This zero character is theNUL
character, and such strings are said to be "NUL-terminated" or "Zero-terminated." Only the characters prior to the NUL are displayed when the string is printed, or copied when the string is concatenated. However, if there is no NUL character in the array, then the string is not properly terminated and an attempt to display it could lead to a crash, or to "garbage" being displayed, as in: "Can I have a beer?#BT&I10)aaX?.The szTitle and szText arguments to MessageBox() expect
char *
which are pointers to this type of string.If you attempt to pass a
BYTE
instead of achar *
, the value of the BYTE will be mistakenly treated as an address. MessageBox() will attempt to access memory at the value "specified" by theBYTE
and an Access Violation will occur.One solution to this problem is to allocate a buffer of type
char
and usesnprintf_s
to transcribe your data values to string representations.For example:
Would display a MessageBox with a message reading something like "Geraet = 0x35".
如果 BYTE 必须是 1 字节,那么您必须(可选)使用 mbstowcs 将字节字符串转换为宽字符串。
If it's essential that BYTE is 1-byte then you have to (optionally) convert your byte strings to wide strings using mbstowcs.
如果您使用 MessageBox,我建议像 AfxMessageBox 这样的 soetming...
if you using MessageBox, i would suggest soetming like AfxMessageBox...