dmDeviceName 只是“c”;
我正在尝试使用 DEVMODE.dmDeviceName
:
dmDeviceName
一个以零结尾的字符数组,指定打印机或显示器的“友好”名称;例如,如果是 PCL/HP LaserJet,则为“PCL/HP LaserJet”。该字符串在设备驱动程序中是唯一的。请注意,此名称可能会被截断以适合 dmDeviceName 数组。
我使用以下代码:
log.printf("Device Name: %s",currDevMode.dmDeviceName);
但是对于每个监视器,名称都打印为 c
。 DEVMODE 中的所有其他信息似乎打印正常。出了什么问题?
I'm trying to get the names of each of my monitors using DEVMODE.dmDeviceName
:
dmDeviceName
A zero-terminated character array that specifies the "friendly" name of the printer or display; for example, "PCL/HP LaserJet" in the case of PCL/HP LaserJet. This string is unique among device drivers. Note that this name may be truncated to fit in the dmDeviceName array.
I'm using the following code:
log.printf("Device Name: %s",currDevMode.dmDeviceName);
But for every monitor, the name is printed as just c
. All other information from DEVMODE seems to print ok. What's going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您很可能正在使用该结构的 Unicode 版本,因此将宽字符传递给
printf
。由于您使用的格式字符串暗示char
数据,因此存在不匹配。UTF-16 编码导致 ASCII 范围内的字符每隔一个字节都为 0,因此 printf 认为前两个字节字符的第二个字节实际上是空终止符。
这是使用
printf
时遇到的问题,它当然没有类型安全。由于您使用的是 C++,因此可能值得切换到基于 iostream 的 I/O。但是,如果您想使用 ANSI 文本(如注释中所示),那么最简单的解决方案是使用结构体的 ANSI
DEVMODEA
版本和相应的A
版本API 函数的列表,例如EnumDisplaySettingsA
、DeviceCapabilityA
。Most likely you are using the Unicode version of the structure and thus are passing wide characters to
printf
. Since you use a format string that implieschar
data there is a mis-match.The UTF-16 encoding results in every other byte being 0 for characters in the ASCII range and so
printf
thinks that the second byte of the first two byte character is actually a null-terminator.This is the sort of problem that you get with
printf
which of course has no type-safety. Since you are using C++ it's probably worth switching toiostream
based I/O.However, if you want to use ANSI text, as you indicate in a comment, then the simplest solution is to use the ANSI
DEVMODEA
version of the struct and the correspondingA
versions of the API functions, e.g.EnumDisplaySettingsA
,DeviceCapabilitiesA
.dmDeviceName
是TCHAR[]
,因此如果您针对 unicode 进行编译,第一个宽字符将被解释为“c”,后跟一个零终止符。您需要将其转换为 ascii 或使用支持 unicode 的打印例程。
dmDeviceName
isTCHAR[]
so if you're compiling for unicode, the first wide character will be interpreted as a 'c' followed by a zero terminator.You will need to convert it to ascii or use unicode capable printing routines.