| 1 |
{ |
| 2 |
Everything search engine interface via IPC |
| 3 |
|
| 4 |
Copyright (C) 2017 Alexander Koblov ([email protected]) |
| 5 |
|
| 6 |
Based on Everything command line interface source |
| 7 |
|
| 8 |
Copyright (C) 2016 David Carpenter |
| 9 |
|
| 10 |
Permission is hereby granted, free of charge, to any person obtaining |
| 11 |
a copy of this software and associated documentation files (the |
| 12 |
"Software"), to deal in the Software without restriction, including |
| 13 |
without limitation the rights to use, copy, modify, merge, publish, |
| 14 |
distribute, sublicense, and/or sell copies of the Software, and to |
| 15 |
permit persons to whom the Software is furnished to do so, subject to |
| 16 |
the following conditions: |
| 17 |
|
| 18 |
The above copyright notice and this permission notice shall be included |
| 19 |
in all copies or substantial portions of the Software. |
| 20 |
|
| 21 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 22 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 23 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 24 |
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 25 |
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 26 |
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 27 |
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 28 |
} |
| 29 |
|
| 30 |
unit everything; |
| 31 |
|
| 32 |
{$mode objfpc}{$H+} |
| 33 |
|
| 34 |
interface |
| 35 |
|
| 36 |
uses |
| 37 |
Classes, SysUtils, Windows; |
| 38 |
|
| 39 |
const |
| 40 |
COPYDATA_IPCTEST_QUERYCOMPLETEW = 0; |
| 41 |
MSGFLT_RESET = 0; |
| 42 |
MSGFLT_ALLOW = 1; |
| 43 |
MSGFLT_DISALLOW = 2; |
| 44 |
|
| 45 |
EVERYTHING_IPC_COPYDATAQUERYW = 2; |
| 46 |
|
| 47 |
EVERYTHING_IPC_SEARCH_WNDCLASS = 'EVERYTHING'; |
| 48 |
EVERYTHING_IPC_WNDCLASS = 'EVERYTHING_TASKBAR_NOTIFICATION'; |
| 49 |
|
| 50 |
EVERYTHING_IPC_ALLRESULTS = $FFFFFFFF; // all results |
| 51 |
|
| 52 |
EVERYTHING_IPC_FOLDER = $00000001; // The item is a folder. (its a file if not set) |
| 53 |
EVERYTHING_IPC_DRIVE = $00000002; // The folder is a drive. Path will be an empty string. |
| 54 |
|
| 55 |
// search flags for querys |
| 56 |
EVERYTHING_IPC_MATCHCASE = $00000001; // match case |
| 57 |
EVERYTHING_IPC_REGEX = $00000008; // enable regex |
| 58 |
|
| 59 |
type |
| 60 |
PChangeFilterStruct = ^TChangeFilterStruct; |
| 61 |
TChangeFilterStruct = record |
| 62 |
cbSize: DWORD; |
| 63 |
ExtStatus: DWORD; |
| 64 |
end; |
| 65 |
|
| 66 |
{$push}{$packrecords 1} |
| 67 |
|
| 68 |
TEVERYTHING_IPC_QUERYW = record |
| 69 |
// the window that will receive the new results. |
| 70 |
reply_hwnd: HWND; |
| 71 |
|
| 72 |
// the value to set the dwData member in the COPYDATASTRUCT struct |
| 73 |
// sent by Everything when the query is complete. |
| 74 |
reply_copydata_message: ULONG_PTR; |
| 75 |
|
| 76 |
// search flags (see EVERYTHING_MATCHCASE | EVERYTHING_MATCHWHOLEWORD | EVERYTHING_MATCHPATH) |
| 77 |
search_flags: DWORD; |
| 78 |
|
| 79 |
// only return results after 'offset' results (0 to return the first result) |
| 80 |
// useful for scrollable lists |
| 81 |
offset: DWORD; |
| 82 |
|
| 83 |
// the number of results to return |
| 84 |
// zero to return no results |
| 85 |
// EVERYTHING_IPC_ALLRESULTS to return ALL results |
| 86 |
max_results: DWORD; |
| 87 |
|
| 88 |
// null terminated string. arbitrary sized search_string buffer. |
| 89 |
search_string: WCHAR; |
| 90 |
end; |
| 91 |
|
| 92 |
PEVERYTHING_IPC_ITEMW = ^TEVERYTHING_IPC_ITEMW; |
| 93 |
TEVERYTHING_IPC_ITEMW = record |
| 94 |
// item flags |
| 95 |
flags: DWORD; |
| 96 |
|
| 97 |
// The offset of the filename from the beginning of the list structure. |
| 98 |
// (wchar_t *)((char *)everything_list + everythinglist->name_offset) |
| 99 |
filename_offset: DWORD; |
| 100 |
|
| 101 |
// The offset of the filename from the beginning of the list structure. |
| 102 |
// (wchar_t *)((char *)everything_list + everythinglist->path_offset) |
| 103 |
path_offset: DWORD; |
| 104 |
end; |
| 105 |
|
| 106 |
PEVERYTHING_IPC_LISTW = ^TEVERYTHING_IPC_LISTW; |
| 107 |
TEVERYTHING_IPC_LISTW = record |
| 108 |
// the total number of folders found. |
| 109 |
totfolders: DWORD; |
| 110 |
|
| 111 |
// the total number of files found. |
| 112 |
totfiles: DWORD; |
| 113 |
|
| 114 |
// totfolders + totfiles |
| 115 |
totitems: DWORD; |
| 116 |
|
| 117 |
// the number of folders available. |
| 118 |
numfolders: DWORD; |
| 119 |
|
| 120 |
// the number of files available. |
| 121 |
numfiles: DWORD; |
| 122 |
|
| 123 |
// the number of items available. |
| 124 |
numitems: DWORD; |
| 125 |
|
| 126 |
// index offset of the first result in the item list. |
| 127 |
offset: DWORD; |
| 128 |
|
| 129 |
// arbitrary sized item list. |
| 130 |
// use numitems to determine the actual number of items available. |
| 131 |
items: TEVERYTHING_IPC_ITEMW; |
| 132 |
end; |
| 133 |
|
| 134 |
{$pop} |
| 135 |
|
| 136 |
type |
| 137 |
TFoundCallback = procedure(FileName: PWideChar); |
| 138 |
|
| 139 |
var |
| 140 |
ChangeWindowMessageFilterEx: function(hWnd: HWND; message: UINT; action: DWORD; filter: PChangeFilterStruct): BOOL; stdcall; |
| 141 |
|
| 142 |
procedure Start(FileMask: String; Flags: Integer; pr: TFoundCallback); |
| 143 |
|
| 144 |
implementation |
| 145 |
|
| 146 |
function SendQuery(hwnd: HWND; num: DWORD; search_string: PWideChar; search_flags: integer): Boolean; |
| 147 |
var |
| 148 |
query: ^TEVERYTHING_IPC_QUERYW; |
| 149 |
len: Int32; |
| 150 |
size: Int32; |
| 151 |
everything_hwnd: HWND; |
| 152 |
cds: COPYDATASTRUCT; |
| 153 |
begin |
| 154 |
everything_hwnd:= FindWindow(EVERYTHING_IPC_WNDCLASS, nil); |
| 155 |
if (everything_hwnd <> 0) then |
| 156 |
begin |
| 157 |
len := StrLen(search_string); |
| 158 |
|
| 159 |
size := SizeOf(TEVERYTHING_IPC_QUERYW) - SizeOf(WideChar) + len * SizeOf(WideChar) + SizeOf(WideChar); |
| 160 |
|
| 161 |
query := GetMem(size); |
| 162 |
if Assigned(query) then |
| 163 |
begin |
| 164 |
query^.offset := 0; |
| 165 |
query^.max_results := num; |
| 166 |
query^.reply_copydata_message := COPYDATA_IPCTEST_QUERYCOMPLETEW; |
| 167 |
query^.search_flags := search_flags; |
| 168 |
query^.reply_hwnd := hwnd; |
| 169 |
StrLCopy(@query^.search_string, search_string, len); |
| 170 |
|
| 171 |
cds.cbData := size; |
| 172 |
cds.dwData := EVERYTHING_IPC_COPYDATAQUERYW; |
| 173 |
cds.lpData := query; |
| 174 |
|
| 175 |
if (SendMessage(everything_hwnd, WM_COPYDATA, WPARAM(hwnd), LPARAM(@cds)) <> 0) then |
| 176 |
begin |
| 177 |
//HeapFree(GetProcessHeap(),0,query); |
| 178 |
|
| 179 |
//return 1; |
| 180 |
REsult:= True; |
| 181 |
end |
| 182 |
else |
| 183 |
begin |
| 184 |
//write(L"Everything IPC service not running.\n"); |
| 185 |
end; |
| 186 |
|
| 187 |
FreeMem(query); |
| 188 |
end; |
| 189 |
end; |
| 190 |
|
| 191 |
Result:= False; |
| 192 |
end; |
| 193 |
|
| 194 |
function EVERYTHING_IPC_ITEMPATHW(list: PEVERYTHING_IPC_LISTW; item: PEVERYTHING_IPC_ITEMW): PWideChar; |
| 195 |
begin |
| 196 |
Result:= PWideChar(PByte(list) + item^.path_offset); |
| 197 |
end; |
| 198 |
|
| 199 |
function EVERYTHING_IPC_ITEMFILENAMEW(list: PEVERYTHING_IPC_LISTW; item: PEVERYTHING_IPC_ITEMW): PWideChar; |
| 200 |
begin |
| 201 |
Result:= PWideChar(PByte(list) + item^.filename_offset); |
| 202 |
end; |
| 203 |
|
| 204 |
procedure listresultsW(hwnd2: HWND; list: PEVERYTHING_IPC_LISTW); |
| 205 |
var |
| 206 |
I: Integer; |
| 207 |
Item: PEVERYTHING_IPC_ITEMW; |
| 208 |
CallB: TFoundCallback; |
| 209 |
Result: PWideChar; |
| 210 |
Res: UnicodeString; |
| 211 |
begin |
| 212 |
CallB:= TFoundCallback(GetWindowLongPtr(hwnd2, GWL_USERDATA)); |
| 213 |
|
| 214 |
for i:=0 to list^.numitems - 1 do |
| 215 |
begin |
| 216 |
Item:= PEVERYTHING_IPC_ITEMW(@list^.items) + i; |
| 217 |
if (Item^.flags and EVERYTHING_IPC_DRIVE) <> 0 then |
| 218 |
begin |
| 219 |
//WriteLn(WideString(EVERYTHING_IPC_ITEMFILENAMEW(list, Item))); |
| 220 |
Result:= EVERYTHING_IPC_ITEMFILENAMEW(list, Item); |
| 221 |
end |
| 222 |
else |
| 223 |
begin |
| 224 |
//WriteLn(WideString(EVERYTHING_IPC_ITEMPATHW(list, Item))); |
| 225 |
|
| 226 |
//WriteLn(WideString(EVERYTHING_IPC_ITEMFILENAMEW(list, Item))); |
| 227 |
Res:= UnicodeString(EVERYTHING_IPC_ITEMPATHW(list, Item)) + PathDelim + UnicodeString(EVERYTHING_IPC_ITEMFILENAMEW(list, Item)); |
| 228 |
Result:= PWideChar(Res);//EVERYTHING_IPC_ITEMFILENAMEW(list, Item); |
| 229 |
end; |
| 230 |
CallB(REsult); |
| 231 |
end; |
| 232 |
CallB(nil); |
| 233 |
PostQuitMessage(0); |
| 234 |
end; |
| 235 |
|
| 236 |
function window_proc(hwnd: HWND; msg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; |
| 237 |
var |
| 238 |
cds: PCOPYDATASTRUCT; |
| 239 |
begin |
| 240 |
case msg of |
| 241 |
WM_COPYDATA: |
| 242 |
begin |
| 243 |
cds := PCOPYDATASTRUCT(lParam); |
| 244 |
if cds^.dwData = COPYDATA_IPCTEST_QUERYCOMPLETEW then |
| 245 |
begin |
| 246 |
listresultsW(hwnd, PEVERYTHING_IPC_LISTW(cds^.lpData)); |
| 247 |
Exit(1); |
| 248 |
end; |
| 249 |
end; |
| 250 |
end; |
| 251 |
Result:= DefWindowProc(hwnd, msg, wParam, lParam); |
| 252 |
end; |
| 253 |
|
| 254 |
procedure Start(FileMask: String; Flags: Integer; pr: TFoundCallback); |
| 255 |
var |
| 256 |
wcex: WNDCLASSEX; |
| 257 |
hwnd2: HWND; |
| 258 |
HH: HMODULE; |
| 259 |
lpMsg: TMsg; |
| 260 |
begin |
| 261 |
ZeroMemory(@wcex, SizeOf(wcex)); |
| 262 |
wcex.cbSize := sizeof(wcex); |
| 263 |
wcex.hInstance := System.HINSTANCE;; |
| 264 |
wcex.lpfnWndProc := @window_proc; |
| 265 |
wcex.lpszClassName := 'IPCTEST'; |
| 266 |
|
| 267 |
if (RegisterClassEx(@wcex) = 0) then |
| 268 |
begin |
| 269 |
WriteLn('failed to register IPCTEST window class'); |
| 270 |
|
| 271 |
|
| 272 |
end; |
| 273 |
|
| 274 |
hwnd2 := CreateWindow( |
| 275 |
'IPCTEST', |
| 276 |
'', |
| 277 |
0, |
| 278 |
0,0,0,0, |
| 279 |
0,0,HINSTANCE,nil); |
| 280 |
|
| 281 |
HH:= LoadLibrary('user32.dll'); |
| 282 |
Pointer(ChangeWindowMessageFilterEx) := GetProcAddress(HH, 'ChangeWindowMessageFilterEx'); |
| 283 |
ChangeWindowMessageFilterEx(hwnd2, WM_COPYDATA, MSGFLT_ALLOW, nil); |
| 284 |
|
| 285 |
SetWindowLongPtr(hwnd2, GWL_USERDATA, LONG_PTR(pr)); |
| 286 |
|
| 287 |
sendquery(hwnd2,EVERYTHING_IPC_ALLRESULTS,PWideChar(WideString(FileMask)), Flags); |
| 288 |
while (True) do |
| 289 |
begin |
| 290 |
if (PeekMessage(lpmsg, 0,0,0,0)) then |
| 291 |
begin |
| 292 |
if not GetMessage(lpmsg,0,0,0) then Exit; |
| 293 |
|
| 294 |
// let windows handle it. |
| 295 |
TranslateMessage(lpmsg); |
| 296 |
DispatchMessage(lpmsg); |
| 297 |
end |
| 298 |
else |
| 299 |
begin |
| 300 |
WaitMessage(); |
| 301 |
end; |
| 302 |
end; |
| 303 |
|
| 304 |
end; |
| 305 |
|
| 306 |
end. |
| 307 |
|