-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
287 changed files
with
66,377 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
unit Api.Message.Sender; | ||
|
||
interface | ||
|
||
uses | ||
WinApi.Windows, | ||
Api.Message; | ||
|
||
function FindMainWindow: HWND; | ||
function SendNextSchemeMessage(NextSchemeType: TNextSchemeType): Boolean; | ||
function SendNextSchemeMessageAsync(NextSchemeType: TNextSchemeType): Boolean; | ||
|
||
implementation | ||
|
||
function FindMainWindow: HWND; | ||
begin | ||
Result := FindWindow(MainWindowClass, MainWindowName); | ||
end; | ||
|
||
function SendNextSchemeMessage(NextSchemeType: TNextSchemeType): Boolean; | ||
var | ||
wnd: THandle; | ||
begin | ||
wnd := FindMainWindow; | ||
if wnd = 0 then Exit(False); | ||
|
||
Result := SendMessage(wnd, WM_NEXT_SCHEME, WPARAM(NextSchemeType), 0) = NextSchemeConfirm; | ||
end; | ||
|
||
function SendNextSchemeMessageAsync(NextSchemeType: TNextSchemeType): Boolean; | ||
var | ||
wnd: THandle; | ||
begin | ||
wnd := FindMainWindow; | ||
if wnd = 0 then Exit(False); | ||
|
||
Result := PostMessage(wnd, WM_NEXT_SCHEME, WPARAM(NextSchemeType), 0); | ||
end; | ||
|
||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
unit Api.Message; | ||
|
||
interface | ||
|
||
uses | ||
WinApi.Messages; | ||
|
||
const | ||
MainWindowName = 'BatteryModeForm'; | ||
MainWindowClass = 'TBatteryModeForm'; | ||
|
||
/// <summary> | ||
/// Сообщение переключения схемы электропитания. | ||
/// </summary> | ||
/// <param name="wParam"> | ||
/// Тип переключения схемы электропитания. | ||
/// Задаётся перечислением TNextSchemeType | ||
/// </param> | ||
/// <param name="lParam"> | ||
/// This parameter is not used. | ||
/// </param> | ||
/// <returns> | ||
/// Если выполнено успешно, то возвращается | ||
/// NextSchemeConfirm = 1 | ||
/// </returns> | ||
WM_NEXT_SCHEME = WM_USER + 1; | ||
NextSchemeConfirm = 1; | ||
|
||
CmdNextScheme = '-Next'; | ||
CmdMaxPowerSavings = '-Economy'; | ||
CmdTypicalPowerSavings = '-Typical'; | ||
CmdMinPowerSavings = '-Performance'; | ||
|
||
type | ||
TNextSchemeType = (nstNext, | ||
nstMaxPowerSavings, | ||
nstTypicalPowerSavings, | ||
nstMinPowerSavings); | ||
|
||
implementation | ||
|
||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
unit Api.Pipe.Client; | ||
|
||
interface | ||
|
||
uses | ||
Winapi.Windows, | ||
System.SysUtils, | ||
Api.Pipe.Command; | ||
|
||
type | ||
TApiClient = class | ||
private | ||
FFullPipeName: string; | ||
public | ||
constructor Create(PipeName: string); | ||
|
||
function Send(Command: IApiCommand): Boolean; | ||
function SendAndWaitResponse(Command: IApiCommand; out Response: string): Boolean; | ||
end; | ||
|
||
implementation | ||
|
||
{ TApiClient } | ||
|
||
constructor TApiClient.Create(PipeName: string); | ||
const | ||
PipeBaseNameFmt = '\\.\pipe\%0:s'; | ||
begin | ||
inherited Create; | ||
FFullPipeName := Format(PipeBaseNameFmt, [PipeName]); | ||
end; | ||
|
||
function TApiClient.Send(Command: IApiCommand): Boolean; | ||
var | ||
Pipe: THandle; | ||
lpBytesWrite: DWORD; | ||
LastError: DWORD; | ||
|
||
CommandStr: string; | ||
begin | ||
Pipe := CreateFile(LPCTSTR(FFullPipeName), GENERIC_WRITE or GENERIC_READ, 0, | ||
nil, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, 0); | ||
|
||
if Pipe = INVALID_HANDLE_VALUE then | ||
Exit(False); | ||
|
||
try | ||
LastError := GetLastError; | ||
if (LastError <> ERROR_SUCCESS) and (LastError <> ERROR_PIPE_BUSY) then | ||
Exit(False); | ||
|
||
if LastError = ERROR_PIPE_BUSY then | ||
if not WaitNamedPipe(LPCTSTR(FFullPipeName), 8000) then | ||
Exit(False); | ||
|
||
CommandStr := Command.GetCommand; | ||
Result := WriteFile(Pipe, | ||
LPCTSTR(CommandStr)^, CommandStr.Length * Sizeof(char), | ||
lpBytesWrite, nil); | ||
finally | ||
CloseHandle(Pipe); | ||
end; | ||
end; | ||
|
||
function TApiClient.SendAndWaitResponse(Command: IApiCommand; | ||
out Response: string): Boolean; | ||
const | ||
BufferSize = 512; | ||
var | ||
Pipe: THandle; | ||
Buffer: array [0 .. BufferSize - 1] of Char; | ||
lpBytesRead: DWORD; | ||
LastError: DWORD; | ||
|
||
CommandStr: string; | ||
lpMode: DWORD; | ||
begin | ||
Response := ''; | ||
|
||
Pipe := CreateFile(LPCTSTR(FFullPipeName), GENERIC_WRITE or GENERIC_READ, 0, | ||
nil, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, 0); | ||
|
||
if pipe = INVALID_HANDLE_VALUE then | ||
Exit(False); | ||
|
||
try | ||
LastError := GetLastError; | ||
if (LastError <> ERROR_SUCCESS) and (LastError <> ERROR_PIPE_BUSY) then | ||
Exit(False); | ||
|
||
if LastError = ERROR_PIPE_BUSY then | ||
if not WaitNamedPipe(LPCTSTR(FFullPipeName), 8000) then | ||
Exit(False); | ||
|
||
lpMode := PIPE_READMODE_MESSAGE; | ||
if not SetNamedPipeHandleState(Pipe, lpMode, nil, nil) then | ||
Exit(False); | ||
|
||
CommandStr := Command.GetCommand; | ||
Result := TransactNamedPipe(Pipe, | ||
LPCTSTR(CommandStr), CommandStr.Length * Sizeof(char), | ||
@Buffer[0], BufferSize, lpBytesRead, nil); | ||
|
||
if not Result and (GetLastError() <> ERROR_MORE_DATA) then | ||
Exit(False); | ||
|
||
while True do | ||
begin | ||
Response := Response + Copy(Buffer, 0, lpBytesRead div SizeOf(Char)); | ||
|
||
if Result then | ||
Break; | ||
|
||
Result := ReadFile(Pipe, Buffer[0], BufferSize, lpBytesRead, nil); | ||
if not Result and (GetLastError() <> ERROR_MORE_DATA) then | ||
Break; | ||
end; | ||
finally | ||
CloseHandle(Pipe); | ||
end; | ||
end; | ||
|
||
end. |
Oops, something went wrong.