如何从 GUID_POWERSCHEME_PERSONALITY 设置中获取 TGUID?

How to get TGUID from the GUID_POWERSCHEME_PERSONALITY setting?

我一直在努力包装完整的 WM_POWERBROADCAST Windows Message, and encapsulating it inside of an event driven component. A majority of it is working fine, and I have also further captured all of the PBT_POWERSETTINGCHANGE setting GUID's - 除了一个 GUID_POWERSCHEME_PERSONALITY.

根据文档,我应该将 Data 成员翻译成 TGUID...

The active power scheme personality has changed. All power schemes map to one of these personalities. The Data member is a GUID that indicates the new active power scheme personality.

问题在于提供的数据类型如下:

Data: packed array[0..0] of UCHAR;

如何从这个 Data 会员中获得一个 TGUID

以下是我目前如何在捕获 PBT_POWERSETTINGCHANGE 事件后捕获这些设置的其余部分...

procedure TPowerMonitor.HandlePowerSetting(const Val: PPowerBroadcastSetting);
var
  VDWord: DWORD;
  function IsVal(G: String): Boolean;
  begin
    Result:= Assigned(Val);
    if Result then
      Result:= IsEqualGUID(StringToGUID(G), Val.PowerSetting);
  end;
begin
  if IsVal('{5d3e9a59-e9D5-4b00-a6bd-ff34ff516548}') then begin
    //GUID_ACDC_POWER_SOURCE
    VDWord:= DWORD(Val.Data[0]);
    if Assigned(Self.FOnSourceChange) then
      FOnSourceChange(Self, TPowerSource(VDWord));
  end else
  if IsVal('{a7ad8041-b45a-4cae-87a3-eecbb468a9e1}') then begin
    //GUID_BATTERY_PERCENTAGE_REMAINING
    VDWord:= DWORD(Val.Data[0]);
    if Assigned(Self.FOnBatteryPercent) then
      FOnBatteryPercent(Self, VDWord);
  end else
  if IsVal('{6fe69556-704a-47a0-8f24-c28d936fda47}') then begin
    //GUID_CONSOLE_DISPLAY_STATE
    VDWord:= DWORD(Val.Data[0]);
    if Assigned(FOnConsoleDisplayState) then
      FOnConsoleDisplayState(Self, TPowerDisplayState(VDWord));
  end else
  if IsVal('{786E8A1D-B427-4344-9207-09E70BDCBEA9}') then begin
    //GUID_GLOBAL_USER_PRESENCE
    VDWord:= DWORD(Val.Data[0]);
    if Assigned(FOnGlobalUserPresence) then
      FOnGlobalUserPresence(Self, TPowerUserPresence(VDWord));
  end else
  if IsVal('{515c31d8-f734-163d-a0fd-11a08c91e8f1}') then begin
    //GUID_IDLE_BACKGROUND_TASK
    if Assigned(FOnIdleBackgroundTask) then
      FOnIdleBackgroundTask(Self);
  end else
  if IsVal('{02731015-4510-4526-99e6-e5a17ebd1aea}') then begin
    //GUID_MONITOR_POWER_ON
    VDWord:= DWORD(Val.Data[0]);
    if Assigned(FOnMonitorPower) then
      FOnMonitorPower(Self, TPowerDisplayState(VDWord));
  end else
  if IsVal('{E00958C0-C213-4ACE-AC77-FECCED2EEEA5}') then begin
    //GUID_POWER_SAVING_STATUS
    VDWord:= DWORD(Val.Data[0]);
    if Assigned(FOnPowerSavingStatus) then
      FOnPowerSavingStatus(Self, TPowerSavingStatus(VDWord));
  end else
  if IsVal('{245d8541-3943-4422-b025-13A784F679B7}') then begin
    //GUID_POWERSCHEME_PERSONALITY
    //TODO: How to read "Data" as a TGUID?
    //GUID_MIN_POWER_SAVINGS (8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c)
    //GUID_MAX_POWER_SAVINGS (a1841308-3541-4fab-bc81-f71556f20b4a)
    //GUID_TYPICAL_POWER_SAVINGS (381b4222-f694-41f0-9685-ff5bb260df2e)
  end else
  if IsVal('{2B84C20E-AD23-4ddf-93DB-05FFBD7EFCA5}') then begin
    //GUID_SESSION_DISPLAY_STATUS
    VDWord:= DWORD(Val.Data[0]);
    if Assigned(FOnSessionDisplayState) then
      FOnSessionDisplayState(Self, TPowerDisplayState(VDWord));
  end else
  if IsVal('{3C0F4548-C03F-4c4d-B9F2-237EDE686376}') then begin
    //GUID_SESSION_USER_PRESENCE
    VDWord:= DWORD(Val.Data[0]);
    if Assigned(FOnSessionUserPresence) then
      FOnSessionUserPresence(Self, TPowerUserPresence(VDWord));
  end else
  if IsVal('{98a7f580-01f7-48aa-9c0f-44352c29e5C0}') then begin
    //GUID_SYSTEM_AWAYMODE
    VDWord:= DWORD(Val.Data[0]);
    if Assigned(FOnAwayMode) then
      FOnAwayMode(Self, TPowerAwayMode(VDWord));
  end else begin
    //Unrecognized GUID
    //TODO: Handle error
  end;
end;

这在此处记录:

POWERBROADCAST_SETTING structure

结构就是所谓的 "variable length structure"。最后一个成员可能比实际声明的要长。 DataLength 成员告诉您 Data 成员中实际存在多少数据。例如,您可以通过调用 System.Move().

来读取它

如果负载确实是 GUID,则检查 DataLength 是否是 GUID 的大小并将数据复制到您的 GUID 变量中。

var
  GUID: TGUID;
.... 
// check that DataLength = SizeOf(TGUID) 
Move(Data, GUID, DataLength);

其他电源方案 GUID 的数据通常是 DWORD,以类似方式处理。