Discussion:
PowerWriteFriendlyName fails on Windows 7 (RTM)
(too old to reply)
Torben.Busk
2009-08-17 12:08:01 UTC
Permalink
Hi Forum.

I have written code to create a new power scheme on Windows Vista. After
duplicating an existing power scheme, I set the name and description of the
new scheme. This works fine on Vista, but not on Windows 7 (RTM).

The API call returns ERROR_SUCCESS, but the new scheme shows up in "Powercfg
-L" without a name.

Here's the code;

Dim ptrGuid As IntPtr
Dim ptrName As IntPtr
Dim BufferSize As UInt32 = 0
Dim res As UInt32 = 0

Try
ptrGuid = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(Guid)))
Marshal.StructureToPtr(inGuid, ptrGuid, True)
BufferSize = (Name.Length * 2) + 1
ptrName = Marshal.StringToBSTR(Name)
res = PowerWriteFriendlyName(IntPtr.Zero, ptrGuid, IntPtr.Zero,
IntPtr.Zero, ptrName, BufferSize)
If res = 0 Then
Return True
End If
Catch ex As Exception
Finally
Marshal.FreeHGlobal(ptrGuid)
Marshal.FreeBSTR(ptrName)
End Try


The code I use to set the description is almost identical (different API
call though) and works without problems on both Windows Vista and Windows 7.

Any help is appreciated.

/Torben Busk
Armin Zingler
2009-08-17 12:53:34 UTC
Permalink
Post by Torben.Busk
Hi Forum.
I have written code to create a new power scheme on Windows Vista. After
duplicating an existing power scheme, I set the name and description of the
new scheme. This works fine on Vista, but not on Windows 7 (RTM).
The API call returns ERROR_SUCCESS, but the new scheme shows up in "Powercfg
-L" without a name.
Here's the code;
Dim ptrGuid As IntPtr
Dim ptrName As IntPtr
Dim BufferSize As UInt32 = 0
Dim res As UInt32 = 0
Try
ptrGuid = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(Guid)))
Marshal.StructureToPtr(inGuid, ptrGuid, True)
BufferSize = (Name.Length * 2) + 1
ptrName = Marshal.StringToBSTR(Name)
res = PowerWriteFriendlyName(IntPtr.Zero, ptrGuid, IntPtr.Zero,
IntPtr.Zero, ptrName, BufferSize)
If res = 0 Then
Return True
End If
Catch ex As Exception
Finally
Marshal.FreeHGlobal(ptrGuid)
Marshal.FreeBSTR(ptrName)
End Try
The code I use to set the description is almost identical (different API
call though) and works without problems on both Windows Vista and Windows 7.
Any help is appreciated.
/Torben Busk
Is there a reason why you don't use the following signature?

byval intptr, byref Guid, byref Guid, byref Guid, byval String,
byval uint32

Marshalling is done implicitly. Also use

"Declare UNICODE Function PowerWriteFriendlyName...".

Though, I don't know if it solves the problem. Try it.


Armin
--
https://epetitionen.bundestag.de/index.php?action=petition;sa=details;petition=4958
Torben.Busk
2009-08-18 08:35:01 UTC
Permalink
Hi Armin.

Thank you for your suggestion. I tried it, but sadly without success. The
result is that I get a copy of the power scheme that I duplicate, but the
name remains untouched, even on Windows Vista.

Any other ideas? I'm all ears ;-)

/Torben Busk
Post by Armin Zingler
Post by Torben.Busk
Hi Forum.
I have written code to create a new power scheme on Windows Vista. After
duplicating an existing power scheme, I set the name and description of the
new scheme. This works fine on Vista, but not on Windows 7 (RTM).
The API call returns ERROR_SUCCESS, but the new scheme shows up in "Powercfg
-L" without a name.
Here's the code;
Dim ptrGuid As IntPtr
Dim ptrName As IntPtr
Dim BufferSize As UInt32 = 0
Dim res As UInt32 = 0
Try
ptrGuid = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(Guid)))
Marshal.StructureToPtr(inGuid, ptrGuid, True)
BufferSize = (Name.Length * 2) + 1
ptrName = Marshal.StringToBSTR(Name)
res = PowerWriteFriendlyName(IntPtr.Zero, ptrGuid, IntPtr.Zero,
IntPtr.Zero, ptrName, BufferSize)
If res = 0 Then
Return True
End If
Catch ex As Exception
Finally
Marshal.FreeHGlobal(ptrGuid)
Marshal.FreeBSTR(ptrName)
End Try
The code I use to set the description is almost identical (different API
call though) and works without problems on both Windows Vista and Windows 7.
Any help is appreciated.
/Torben Busk
Is there a reason why you don't use the following signature?
byval intptr, byref Guid, byref Guid, byref Guid, byval String,
byval uint32
Marshalling is done implicitly. Also use
"Declare UNICODE Function PowerWriteFriendlyName...".
Though, I don't know if it solves the problem. Try it.
Armin
--
https://epetitionen.bundestag.de/index.php?action=petition;sa=details;petition=4958
Omer Leshem
2010-08-24 20:15:19 UTC
Permalink
Here's my code:

[DllImport("powrprof.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern UInt32 PowerWriteFriendlyName(IntPtr RootPowerKey, ref Guid SchemeGuid, IntPtr SubGroupOfPowerSettingGuid, IntPtr PowerSettingGuid, String Buffer, UInt32 BufferSize);

[DllImport("powrprof.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern UInt32 PowerWriteDescription(IntPtr RootPowerKey, ref Guid SchemeGuid, IntPtr SubGroupOfPowerSettingGuid, IntPtr PowerSettingGuid, String Buffer, UInt32 BufferSize);


private static void ChangePowerSchemeName(Guid guidScheme, string powerSchemeName)
{
UInt32 BufferSize = (UInt32)(powerSchemeName.Length * sizeof (char));

if (PowerWriteFriendlyName(IntPtr.Zero, ref guidScheme, IntPtr.Zero, IntPtr.Zero, powerSchemeName, BufferSize) != 0)
{
throw new Exception("Failed to change the name of the power scheme: " + guidScheme);
}
}

private static void ChangePowerSchemeDescription(Guid guidScheme, string powerSchemeDescription)
{
UInt32 BufferSize = (UInt32)(powerSchemeDescription.Length * sizeof(char));

if (PowerWriteDescription(IntPtr.Zero, ref guidScheme, IntPtr.Zero, IntPtr.Zero, powerSchemeDescription, BufferSize) != 0)
{
throw new Exception("Failed to change the name of the power scheme: " + guidScheme);
}
}
Post by Torben.Busk
Hi Forum.
I have written code to create a new power scheme on Windows Vista. After
duplicating an existing power scheme, I set the name and description of the
new scheme. This works fine on Vista, but not on Windows 7 (RTM).
The API call returns ERROR_SUCCESS, but the new scheme shows up in "Powercfg
-L" without a name.
Here is the code;
Dim ptrGuid As IntPtr
Dim ptrName As IntPtr
Dim BufferSize As UInt32 = 0
Dim res As UInt32 = 0
Try
ptrGuid = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(Guid)))
Marshal.StructureToPtr(inGuid, ptrGuid, True)
BufferSize = (Name.Length * 2) + 1
ptrName = Marshal.StringToBSTR(Name)
res = PowerWriteFriendlyName(IntPtr.Zero, ptrGuid, IntPtr.Zero,
IntPtr.Zero, ptrName, BufferSize)
If res = 0 Then
Return True
End If
Catch ex As Exception
Finally
Marshal.FreeHGlobal(ptrGuid)
Marshal.FreeBSTR(ptrName)
End Try
The code I use to set the description is almost identical (different API
call though) and works without problems on both Windows Vista and Windows 7.
Any help is appreciated.
/Torben Busk
Is there a reason why you do not use the following signature?
byval intptr, byref Guid, byref Guid, byref Guid, byval String,
byval uint32
Marshalling is done implicitly. Also use
"Declare UNICODE Function PowerWriteFriendlyName...".
Though, I do not know if it solves the problem. Try it.
Armin
--
https://epetitionen.bundestag.de/index.php?action=petition;sa=details;petition=4958
Post by Torben.Busk
Hi Armin.
Thank you for your suggestion. I tried it, but sadly without success. The
result is that I get a copy of the power scheme that I duplicate, but the
name remains untouched, even on Windows Vista.
Any other ideas? I am all ears ;-)
/Torben Busk
Submitted via EggHeadCafe - Software Developer Portal of Choice
Changing WCF Service Implementation at Runtime
http://www.eggheadcafe.com/tutorials/aspnet/d9263dcc-f7ed-42f3-bc96-321461be3306/changing-wcf-service-implementation-at-runtime.aspx
Loading...