Skip to content

Commit

Permalink
Add project files
Browse files Browse the repository at this point in the history
  • Loading branch information
artar94 committed Nov 15, 2020
1 parent 181f73b commit e5fed36
Show file tree
Hide file tree
Showing 287 changed files with 66,377 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Api/Api.Message.Sender.pas
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.
42 changes: 42 additions & 0 deletions Api/Api.Message.pas
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.
123 changes: 123 additions & 0 deletions Api/Api.Pipe.Client.pas
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.
Loading

0 comments on commit e5fed36

Please sign in to comment.