PowerWriteFriendlyName正在vista中创建一些垃圾字符 [英] PowerWriteFriendlyName is creating some junk characters in vista

查看:63
本文介绍了PowerWriteFriendlyName正在vista中创建一些垃圾字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在vista中更改/设置文本计数为> = 20的电源计划的名称时,使用 PowerWriteFriendlyName 正在编写,但后面跟着一些垃圾字符。我想设置没有junks的名字。请帮帮我。





这是我用的代码,

 CString szPlanName = _T(  abcdefghijabcdefghij);  //  计数正好是20  
PowerWriteFriendlyName(
NULL,
[PowerSchemeGuid], // 任何有效的guid
NULL,
NULL,
(UCHAR *)szPlanName.GetBuffer(),
szPlanName.GetLength()* 2
);







注意:

此代码在除vista之外的其他操作系统中运行正常。

解决方案

来自 PowerWriteFriendlyName [ ^ ]:

引用:

BufferSize [in]

Buffer参数指定的友好名称的大小,包括终止NULL字符。

所以你必须指定比字符串长度多两个字节。因为 Buffer 参数是一个Unicode字符串,所以你还应该使用 CStringW

 CStringW szPlanName = Labcdefghijabcdefghij; 
PowerWriteFriendlyName(
NULL,
[PowerSchemeGuid],//任何有效的guid
NULL,
NULL,
(UCHAR *)szPlanName.GetBuffer() ,
(1 + szPlanName.GetLength())* sizeof(wchar_t)
);


When I try to change/set the name for power plan whose text count is >= 20 in vista, using PowerWriteFriendlyName it is writing, but followed by some junk characters. I want to set the name without junks. Please help me.


Here's the code which I used,

CString szPlanName = _T("abcdefghijabcdefghij"); // Count is exactly 20
PowerWriteFriendlyName(
NULL,
[PowerSchemeGuid], // Any valid guid
NULL,
NULL,
(UCHAR *)szPlanName.GetBuffer(),
szPlanName.GetLength() * 2
);




Note:
This code is working fine in other OS except vista.

解决方案

From PowerWriteFriendlyName[^]:

Quote:

BufferSize [in]
The size of the friendly name specified by the Buffer parameter, including the terminating NULL character.


So you must specify two more bytes than the string length. Because the Buffer parameter is a Unicode string, you should also use CStringW:

CStringW szPlanName = L"abcdefghijabcdefghij";
PowerWriteFriendlyName(
    NULL,
    [PowerSchemeGuid], // Any valid guid
    NULL,
    NULL,
    (UCHAR *)szPlanName.GetBuffer(),
    (1 + szPlanName.GetLength()) * sizeof(wchar_t)
);


这篇关于PowerWriteFriendlyName正在vista中创建一些垃圾字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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