MFC:如何将 DWORD 和 BYTE 转换为 LPCTSTR 以在 MessageBox 中显示

发布于 2024-08-07 18:32:58 字数 345 浏览 5 评论 0原文

我正在使用带有“使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

勿忘心安 2024-08-14 18:32:58

看起来您可能需要一些有关 C 语言本身的帮助,我建议您找到一本与 Windows 编程无关的 C 初学者书籍。

MessageBox() 只显示 C 风格的字符串,这些字符串是 char 类型的数组,其中包含 ASCII 值为 0 的字符。这个零字符是 NUL 字符,此类字符串是称为“NUL 终止”或“零终止”。打印字符串时仅显示 NUL 之前的字符,连接字符串时仅复制 NUL 之前的字符。但是,如果数组中没有 NUL 字符,则该字符串未正确终止,并且尝试显示它可能会导致崩溃,或显示“垃圾”,如:“Can I have a beer?# BT&I10)aaX?。

MessageBox() 的 szTitleszText 参数需要 char *,它们是指向此类字符串的指针。

如果您尝试传递 BYTE 而不是 char *,则 BYTE 的值将被错误地视为地址。 MessageBox() 将尝试访问 BYTE“指定”值处的内存,并且将发生访问冲突。

解决此问题的一种方法是分配一个 char 类型的缓冲区,并使用 snprintf_s 将数据值转录为字符串表示形式。

例如:

char output_buffer[1024];

snprintf_s(output_buffer,dimensionof(output_buffer),“Geraet = 0x%02X”,m_bGeraet[i]);
MessageBox(hwnd_parent, output_buffer, "来自我的消息:", MB_OK);

将显示一个消息框,其中包含一条类似“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 the NUL 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 a char *, the value of the BYTE will be mistakenly treated as an address. MessageBox() will attempt to access memory at the value "specified" by the BYTE and an Access Violation will occur.

One solution to this problem is to allocate a buffer of type char and use snprintf_s to transcribe your data values to string representations.

For example:

char output_buffer[1024];

snprintf_s(output_buffer, dimensionof(output_buffer), "Geraet = 0x%02X", m_bGeraet[i]);
MessageBox(hwnd_parent, output_buffer, "Message from me:", MB_OK);

Would display a MessageBox with a message reading something like "Geraet = 0x35".

猫性小仙女 2024-08-14 18:32:58

如果 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.

嗼ふ静 2024-08-14 18:32:58
//easy way for bytes is to do this

CString sTemp;

sTemp.Format("my byte = %d", bySomeVal);

MessageBox(sTemp);

//for a DWORD try

sTemp.Format("Dword is %lu", dwSomeVal);

MessageBox(sTemp);

如果您使用 MessageBox,我建议像 AfxMessageBox 这样的 soetming...

//easy way for bytes is to do this

CString sTemp;

sTemp.Format("my byte = %d", bySomeVal);

MessageBox(sTemp);

//for a DWORD try

sTemp.Format("Dword is %lu", dwSomeVal);

MessageBox(sTemp);

if you using MessageBox, i would suggest soetming like AfxMessageBox...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文