Click here to Skip to main content
15,884,648 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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,
C++
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.
Posted

1 solution

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)
);
 
Share this answer
 
Comments
WinAppy 9-Dec-13 23:36pm    
Oh! Thank you for explaining me this. Its working now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900