-
Notifications
You must be signed in to change notification settings - Fork 56
/
kntLauncher.dpr
138 lines (112 loc) · 3.77 KB
/
kntLauncher.dpr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
program kntLauncher;
//{.$APPTYPE CONSOLE}
{$APPTYPE GUI}
{$R *.res}
uses
System.SysUtils,
Winapi.Windows,
Winapi.Messages,
kn_Msgs;
var
_OTHER_INSTANCE_HANDLE : hwnd = 0;
ReceivedCommandLine: string;
type
PFindWindowInfo = ^TFindWindowInfo;
TFindWindowInfo = record
WindowHandle: HWND;
end;
function CallInstance: integer; forward;
function EnumWindowsProc(hWnd: HWND; Info: PFindWindowInfo): BOOL; stdcall;
var
ClassName: array[0..255] of Char;
ResultInstance: integer;
begin
Result := True;
if GetClassName(hWnd, ClassName, SizeOf(ClassName)) > 0 then begin
if (StrComp(ClassName, UniqueAppName_KEYNOTE10) = 0) or (StrComp(ClassName, UniqueAppName_KEYNOTE10_dnd) = 0) then begin
_OTHER_INSTANCE_HANDLE:= hWnd;
if CallInstance = 0 then begin
Info^.WindowHandle := hWnd;
Result:= False;
//ShowWindow(hWnd, SW_RESTORE); // See comment to TForm_Main.ExecuteCallArgs, in keynote project
sleep(100);
SetForegroundWindow(hWnd);
end;
end;
end;
end;
function ActivateAValidKNTInstance: boolean;
var
Info: TFindWindowInfo;
Title: String;
p1, p2: integer;
begin
Title:= '';
Result:= False;
p1:= Pos(' -title', ReceivedCommandLine);
if p1 > 0 then begin
p2:= Pos('"', ReceivedCommandLine, p1+8);
if p2 > p1 then
Title:= Copy(ReceivedCommandLine, p1+8, p2-(p1+8));
end;
if Title <> '' then begin
_OTHER_INSTANCE_HANDLE:= FindWindow(PChar(UniqueAppName_KEYNOTE10_dnd), PChar(Title));
if _OTHER_INSTANCE_HANDLE = 0 then
_OTHER_INSTANCE_HANDLE:= FindWindow(PChar(UniqueAppName_KEYNOTE10), PChar(Title));
if (_OTHER_INSTANCE_HANDLE <> 0) and (CallInstance=0) then begin
sleep(100);
SetForegroundWindow(_OTHER_INSTANCE_HANDLE);
Result:= true;
end
end
else begin
Info.WindowHandle := 0;
EnumWindows(@EnumWindowsProc, LPARAM(@Info));
Result:= (Info.WindowHandle <> 0);
end;
end;
function CallInstance: integer;
var
CopyData : TCopyDataStruct;
Args: string;
begin
Args:= ReceivedCommandLine;
copydata.dwData := KNT_MSG_LAUNCHER_CALL;
copydata.cbData:= ByteLength(Args)+1;
copydata.lpData := PChar(Args);
Result:= SendMessage( _OTHER_INSTANCE_HANDLE, WM_COPYDATA, 0, integer( @copydata ));
end;
procedure LaunchNewKNTInstance();
var
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
kntPath: string;
commandLine: string;
begin
FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
StartupInfo.cb := SizeOf(TStartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := SW_HIDE;
commandLine:= StringReplace(GetCommandLine, 'kntLauncher', 'keynote', []) + ' -ignSI -dnd';
if CreateProcess(nil, PChar(CommandLine), nil, nil, False, 0, nil, nil, StartupInfo, ProcessInfo) then begin
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end
else
MessageBox(0, PChar('Error launching: ' + #13#13 + commandLine), 'KntLauncher', MB_ICONWARNING or MB_OK);
end;
begin
try
if ParamCount >= 1 then begin
ReceivedCommandLine:= GetCommandLine;
if not ActivateAValidKNTInstance() then
LaunchNewKNTInstance();
end
else
MessageBox(0, 'Usage:'+#13#13 +'kntLauncher <kenote args>' + #13 +
'(ex: kntLauncher myHelp.knt -jmp"file:///*3|16|5|0|1")', 'KntLauncher', MB_ICONINFORMATION or MB_OK);
except
on E: Exception do
MessageBox(0, PChar('Error: ' + E.Message), 'KntLauncher', MB_ICONWARNING or MB_OK);
end;
end.