source: cprs/branches/HealthSevak-CPRS/VA/VAUtils.pas@ 1693

Last change on this file since 1693 was 1693, checked in by healthsevak, 9 years ago

Committing the files for first time to this new branch

File size: 42.8 KB
Line 
1unit VAUtils;
2
3{TODO -oJeremy Merrill -cMessageHandlers : Change component list to use hex address for uComponentList
4search instead of IndexOfObject, so that it used a binary search
5on sorted text.}
6
7interface
8
9uses
10 SysUtils, Windows, Messages, Classes, Graphics, StrUtils, Controls, VAClasses, Forms,
11 SHFolder, ShlObj, PSAPI, ShellAPI, ComObj;
12
13type
14 TShow508MessageIcon = (smiNone, smiInfo, smiWarning, smiError, smiQuestion);
15 TShow508MessageButton = (smbOK, smbOKCancel, smbAbortRetryCancel, smbYesNoCancel,
16 smbYesNo, smbRetryCancel);
17 TShow508MessageResult = (smrOK, srmCancel, smrAbort, smrRetry, smrIgnore, smrYes, smrNo);
18
19function ShowMsg(const Msg, Caption: string; Icon: TShow508MessageIcon = smiNone;
20 Buttons: TShow508MessageButton = smbOK): TShow508MessageResult; overload;
21function Show508Message(const Msg: string): TShow508MessageResult;
22function ShowMsg(const Msg: string; Icon: TShow508MessageIcon = smiNone;
23 Buttons: TShow508MessageButton = smbOK): TShow508MessageResult; overload;
24
25const
26 SHARE_DIR = '\VISTA\Common Files\';
27
28{ returns the Nth piece (PieceNum) of a string delimited by Delim }
29function Piece(const S: string; Delim: char; PieceNum: Integer): string;
30{ returns several contiguous pieces }
31function Pieces(const S: string; Delim: char; FirstNum, LastNum: Integer): string;
32
33// Same as FreeAndNil, but for TString objects only
34// Frees any objects in the TStrings Objects list as well the TStrings object
35procedure FreeAndNilTStringsAndObjects(var Strings);
36
37// Returns true if a screen reader programm is running
38function ScreenReaderActive: boolean;
39
40// Special Coding for Screen Readers only enabled if screen reader was
41// running when the application first started up
42function ScreenReaderSupportEnabled: boolean;
43
44// Returns C:\...\subPath\File format based on maxSize and Canvas font setting
45function GetFileWithShortenedPath(FileName: String; MaxSize: integer; Canvas: TCanvas): string;
46
47const
48 HexChars: array[0..255] of string =
49 ('00','01','02','03','04','05','06','07','08','09','0A','0B','0C','0D','0E','0F',
50 '10','11','12','13','14','15','16','17','18','19','1A','1B','1C','1D','1E','1F',
51 '20','21','22','23','24','25','26','27','28','29','2A','2B','2C','2D','2E','2F',
52 '30','31','32','33','34','35','36','37','38','39','3A','3B','3C','3D','3E','3F',
53 '40','41','42','43','44','45','46','47','48','49','4A','4B','4C','4D','4E','4F',
54 '50','51','52','53','54','55','56','57','58','59','5A','5B','5C','5D','5E','5F',
55 '60','61','62','63','64','65','66','67','68','69','6A','6B','6C','6D','6E','6F',
56 '70','71','72','73','74','75','76','77','78','79','7A','7B','7C','7D','7E','7F',
57 '80','81','82','83','84','85','86','87','88','89','8A','8B','8C','8D','8E','8F',
58 '90','91','92','93','94','95','96','97','98','99','9A','9B','9C','9D','9E','9F',
59 'A0','A1','A2','A3','A4','A5','A6','A7','A8','A9','AA','AB','AC','AD','AE','AF',
60 'B0','B1','B2','B3','B4','B5','B6','B7','B8','B9','BA','BB','BC','BD','BE','BF',
61 'C0','C1','C2','C3','C4','C5','C6','C7','C8','C9','CA','CB','CC','CD','CE','CF',
62 'D0','D1','D2','D3','D4','D5','D6','D7','D8','D9','DA','DB','DC','DD','DE','DF',
63 'E0','E1','E2','E3','E4','E5','E6','E7','E8','E9','EA','EB','EC','ED','EE','EF',
64 'F0','F1','F2','F3','F4','F5','F6','F7','F8','F9','FA','FB','FC','FD','FE','FF');
65
66 DigitTable = '0123456789ABCDEF';
67
68 BinChars: array[0..15] of string =
69 ('0000', // 0
70 '0001', // 1
71 '0010', // 2
72 '0011', // 3
73 '0100', // 4
74 '0101', // 5
75 '0110', // 6
76 '0111', // 7
77 '1000', // 8
78 '1001', // 9
79 '1010', // 10
80 '1011', // 11
81 '1100', // 12
82 '1101', // 13
83 '1110', // 14
84 '1111');// 15
85
86type
87 TFastIntHexRec = record
88 case integer of
89 1: (lw: longword);
90 2: (b1, b2, b3, b4: byte);
91 end;
92
93 TFastWordHexRec = record
94 case integer of
95 1: (w: word);
96 2: (b1, b2: byte);
97 end;
98
99// returns an 8 digit hex number
100function FastIntToHex(Value: LongWord): String;
101
102// returns an 4 digit hex number
103function FastWordToHex(Value: Word): String;
104
105// takes only a 2 digit value - 1 byte - from above table
106function FastHexToByte(HexString: string): byte;
107
108// takes only an 8 digit value - 4 bytes
109function FastHexToInt(HexString: string): LongWord;
110
111// converts am upper case hex string of any length to binary
112function FastHexToBinary(HexString: string): string;
113
114{ returns a cyclic redundancy check for a string }
115function CRCForString(AString: string): DWORD;
116
117// If the string parameter does not end with a back slash, one is appended to the end
118// typically used for file path processing
119function AppendBackSlash(var txt: string): string;
120
121// returns special folder path on the current machine - such as Program Files etc
122// the parameter is a CSIDL windows constant
123function GetSpecialFolderPath(SpecialFolderCSIDL: integer): String;
124
125// returns Program Files path on current machine
126function GetProgramFilesPath: String;
127
128// returns Program Files path on the drive where the currently running application
129// resides, if it is a different drive than the one that contains the current
130// machine's Program Files directory. This is typically used for networked drives.
131function GetAlternateProgramFilesPath: String;
132
133// Get the Window title (Caption) of a window, given only it's handle
134function GetWindowTitle(Handle: HWND): String;
135
136// Get the Window class name string, given only it's window handle
137function GetWindowClassName(Handle: HWND): String;
138
139// Add or Remove a message handler to manage custom messages for an existing TWinControl
140type
141// TVAWinProcMessageEvent is used for raw windows messages not intercepted by the controls
142(*
143// doesn't work when the component's parent is changed, or anything else causes the
144 handle to be recreated.
145 TVAWinProcMessageEvent = function(hWnd: HWND; Msg: UINT;
146 wParam: WPARAM; lParam: LPARAM; var Handled: boolean): LRESULT of object;
147*)
148
149// TVAMessageEvent is used for windows messages that are intercepted by controls and are
150// converted to TMessage records - messages not intercepted in this manner should be
151// caught by TVAWinProcMessageEvent. Note that this is a different event structure
152// than the TMessageEvent used by TApplication, this uses TMessage rather than TMsg.
153 TVAMessageEvent = procedure (var Msg: TMessage; var Handled: Boolean) of object;
154
155//procedure AddMessageHandler(Control: TWinControl; MessageHandler: TVAWinProcMessageEvent); overload;
156procedure AddMessageHandler(Control: TWinControl; MessageHandler: TVAMessageEvent); overload;
157
158//procedure RemoveMessageHandler(Control: TWinControl; MessageHandler: TVAWinProcMessageEvent); overload;
159procedure RemoveMessageHandler(Control: TWinControl; MessageHandler: TVAMessageEvent); overload;
160
161// removes all message handlers, both TVAWinProcMessageEvent and TVAMessageEvent types
162procedure RemoveAllMessageHandlers(Control: TWinControl);
163
164function MessageHandlerCount(Control: TWinControl): integer;
165
166function GetInstanceCount(ApplicationNameAndPath: string): integer; overload;
167function GetInstanceCount: integer; overload;
168
169function AnotherInstanceRunning: boolean;
170
171procedure VersionStringSplit(const VerStr: string; var Val1: integer); overload;
172procedure VersionStringSplit(const VerStr: string; var Val1, Val2: integer); overload;
173procedure VersionStringSplit(const VerStr: string; var Val1, Val2, Val3: integer); overload;
174procedure VersionStringSplit(const VerStr: string; var Val1, Val2, Val3, Val4: integer); overload;
175
176function ExecuteAndWait(FileName: string; Parameters: String = ''): integer;
177
178// when called inside a DLL, returns the fully qualified name of the DLL file
179// must pass an address or a class or procedure that's been defined inside the DLL
180function GetDLLFileName(Address: Pointer): string;
181
182const
183 { values that can be passed to FileVersionValue }
184 FILE_VER_COMPANYNAME = 'CompanyName';
185 FILE_VER_FILEDESCRIPTION = 'FileDescription';
186 FILE_VER_FILEVERSION = 'FileVersion';
187 FILE_VER_INTERNALNAME = 'InternalName';
188 FILE_VER_LEGALCOPYRIGHT = 'LegalCopyright';
189 FILE_VER_ORIGINALFILENAME = 'OriginalFilename';
190 FILE_VER_PRODUCTNAME = 'ProductName';
191 FILE_VER_PRODUCTVERSION = 'ProductVersion';
192 FILE_VER_COMMENTS = 'Comments';
193
194function FileVersionValue(const AFileName, AValueName: string): string;
195
196// compares up to 4 pieces of a numeric version, returns true if CheckVersion is >= OriginalVersion
197// allows for . and , delimited version numbers
198function VersionOK(OriginalVersion, CheckVersion: string): boolean;
199
200implementation
201
202function Piece(const S: string; Delim: char; PieceNum: Integer): string;
203{ returns the Nth piece (PieceNum) of a string delimited by Delim }
204var
205 i: Integer;
206 Strt, Next: PChar;
207begin
208 i := 1;
209 Strt := PChar(S);
210 Next := StrScan(Strt, Delim);
211 while (i < PieceNum) and (Next <> nil) do
212 begin
213 Inc(i);
214 Strt := Next + 1;
215 Next := StrScan(Strt, Delim);
216 end;
217 if Next = nil then Next := StrEnd(Strt);
218 if i < PieceNum then Result := '' else SetString(Result, Strt, Next - Strt);
219end;
220
221function Pieces(const S: string; Delim: char; FirstNum, LastNum: Integer): string;
222{ returns several contiguous pieces }
223var
224 PieceNum: Integer;
225begin
226 Result := '';
227 for PieceNum := FirstNum to LastNum do Result := Result + Piece(S, Delim, PieceNum) + Delim;
228 if Length(Result) > 0 then Delete(Result, Length(Result), 1);
229end;
230
231//type
232// TShow508MessageIcon = (smiNone, smiInfo, smiWarning, smiError, smiQuestion);
233// TShow508MessageButton = (smbOK, smbOKCancel, smbAbortRetryCancel, smbYesNoCancel,
234// smbYesNo, smbRetryCancel);
235// TShow508MessageResult = (smrOK, srmCancel, smrAbort, smrRetry, smrIgnore, smrYes, smrNo);
236
237function ShowMsg(const Msg, Caption: string; Icon: TShow508MessageIcon = smiNone;
238 Buttons: TShow508MessageButton = smbOK): TShow508MessageResult; overload;
239var
240 Flags, Answer: Longint;
241 Title: string;
242begin
243 Flags := MB_TOPMOST;
244 case Icon of
245 smiInfo: Flags := Flags OR MB_ICONINFORMATION;
246 smiWarning: Flags := Flags OR MB_ICONWARNING;
247 smiError: Flags := Flags OR MB_ICONERROR;
248 smiQuestion: Flags := Flags OR MB_ICONQUESTION;
249 end;
250 case Buttons of
251 smbOK: Flags := Flags OR MB_OK;
252 smbOKCancel: Flags := Flags OR MB_OKCANCEL;
253 smbAbortRetryCancel: Flags := Flags OR MB_ABORTRETRYIGNORE;
254 smbYesNoCancel: Flags := Flags OR MB_YESNOCANCEL;
255 smbYesNo: Flags := Flags OR MB_YESNO;
256 smbRetryCancel: Flags := Flags OR MB_RETRYCANCEL;
257 end;
258 Title := Caption;
259 if Title = '' then
260 Title := Application.Title;
261 Answer := Application.MessageBox(PChar(Msg), PChar(Title), Flags);
262 case Answer of
263 IDCANCEL: Result := srmCancel;
264 IDABORT: Result := smrAbort;
265 IDRETRY: Result := smrRetry;
266 IDIGNORE: Result := smrIgnore;
267 IDYES: Result := smrYes;
268 IDNO: Result := smrNo;
269 else Result := smrOK; // IDOK
270 end;
271end;
272
273function ShowMsg(const Msg: string; Icon: TShow508MessageIcon = smiNone;
274 Buttons: TShow508MessageButton = smbOK): TShow508MessageResult;
275var
276 Caption: string;
277begin
278 Caption := '';
279 case Icon of
280 smiWarning: Caption := ' Warning';
281 smiError: Caption := ' Error';
282 smiQuestion: Caption := ' Inquiry';
283 smiInfo: Caption := ' Information';
284 end;
285 Caption := Application.Title + Caption;
286 Result := ShowMsg(Msg, Caption, Icon, Buttons);
287end;
288
289function Show508Message(Const Msg: String): TShow508MessageResult;
290begin
291 Result := ShowMsg(msg);
292end;
293
294
295procedure FreeAndNilTStringsAndObjects(var Strings);
296var
297 i: integer;
298 list: TStrings;
299begin
300 list := TStrings(Strings);
301 for I := 0 to List.Count - 1 do
302 if assigned(list.Objects[i]) then
303 list.Objects[i].Free;
304 FreeAndNil(list);
305end;
306
307
308function ScreenReaderActive: boolean;
309var
310 ListStateOn : longbool;
311 Success: longbool;
312begin
313 //Determine if a screen reader is currently being used.
314 Success := SystemParametersInfo(SPI_GETSCREENREADER, 0, @ListStateOn,0);
315 if Success and ListStateOn then
316 Result := TRUE
317 else
318 Result := FALSE;
319end;
320
321var
322 CheckScreenReaderSupport: boolean = TRUE;
323 uScreenReaderSupportEnabled: boolean = FALSE;
324
325function ScreenReaderSupportEnabled: boolean;
326begin
327 if CheckScreenReaderSupport then
328 begin
329 uScreenReaderSupportEnabled := ScreenReaderActive;
330 CheckScreenReaderSupport := FALSE;
331 end;
332 Result := uScreenReaderSupportEnabled;
333end;
334
335const
336 DOTS = '...';
337 DOTS_LEN = Length(DOTS) + 2;
338
339// Returns C:\...\subPath\File format based on maxSize and Canvas font setting
340function GetFileWithShortenedPath(FileName: String; MaxSize: integer; Canvas: TCanvas): string;
341var
342 len, count, p, first, last: integer;
343
344begin
345 Result := FileName;
346 count := 0;
347 p := 0;
348 first := 0;
349 last := 0;
350
351 repeat
352 p := PosEx('\', Result, p+1);
353 if p > 0 then inc(count);
354 if first = 0 then
355 begin
356 first := p;
357 last := p+1;
358 end;
359 until p = 0;
360
361 repeat
362 len := Canvas.TextWidth(Result);
363 if (len > MaxSize) and (count > 0) then
364 begin
365 if count > 1 then
366 begin
367 p := last;
368 while(Result[p] <> '\') do inc(p);
369 Result := copy(Result,1,first) + DOTS + copy(Result,p,MaxInt);
370 last := first + DOTS_LEN;
371 end
372 else
373 Result := copy(Result, last, MaxInt);
374 dec(count);
375 end;
376 until (len <= MaxSize) or (count < 1);
377end;
378
379// returns an 8 digit hex number
380function FastIntToHex(Value: LongWord): String;
381var
382 v: TFastIntHexRec;
383begin
384 v.lw:= Value;
385 Result := HexChars[v.b4] + HexChars[v.b3] + HexChars[v.b2] + HexChars[v.b1];
386end;
387
388// returns an 4 digit hex number
389function FastWordToHex(Value: Word): String;
390var
391 v: TFastWordHexRec;
392begin
393 v.w:= Value;
394 Result := HexChars[v.b2] + HexChars[v.b1];
395end;
396
397const
398 b1Mult = 1;
399 b2Mult = b1Mult * 16;
400 b3Mult = b2Mult * 16;
401 b4Mult = b3Mult * 16;
402 b5Mult = b4Mult * 16;
403 b6Mult = b5Mult * 16;
404 b7Mult = b6Mult * 16;
405 b8Mult = b7Mult * 16;
406
407// takes only a 2 digit value - 1 byte - from above table
408function FastHexToByte(HexString: string): byte;
409begin
410 Result := ((pos(HexString[2], DigitTable) - 1) * b1Mult) +
411 ((pos(HexString[1], DigitTable) - 1) * b2Mult);
412end;
413
414// takes only an 8 digit value - 4 bytes
415function FastHexToInt(HexString: string): LongWord;
416begin
417 Result := ((pos(HexString[8], DigitTable) - 1) * b1Mult) +
418 ((pos(HexString[7], DigitTable) - 1) * b2Mult) +
419 ((pos(HexString[6], DigitTable) - 1) * b3Mult) +
420 ((pos(HexString[5], DigitTable) - 1) * b4Mult) +
421 ((pos(HexString[4], DigitTable) - 1) * b5Mult) +
422 ((pos(HexString[3], DigitTable) - 1) * b6Mult) +
423 ((pos(HexString[2], DigitTable) - 1) * b7Mult) +
424 ((pos(HexString[1], DigitTable) - 1) * b8Mult);
425end;
426
427// converts a hex string to binary
428function FastHexToBinary(HexString: string): string;
429var
430 i, len, val: integer;
431 chr: string;
432begin
433 len := length(HexString);
434 Result := '';
435 for I := 1 to len do
436 begin
437 chr := HexString[i];
438 val := pos(chr, DigitTable);
439 if val > 0 then
440 Result := Result + BinChars[val-1]
441 end;
442end;
443
444const
445{ copied from ORFn - table for calculating CRC values }
446 CRC32_TABLE: array[0..255] of DWORD =
447 ($0, $77073096, $EE0E612C, $990951BA, $76DC419, $706AF48F, $E963A535, $9E6495A3,
448 $EDB8832, $79DCB8A4, $E0D5E91E, $97D2D988, $9B64C2B, $7EB17CBD, $E7B82D07, $90BF1D91,
449 $1DB71064, $6AB020F2, $F3B97148, $84BE41DE, $1ADAD47D, $6DDDE4EB, $F4D4B551, $83D385C7,
450 $136C9856, $646BA8C0, $FD62F97A, $8A65C9EC, $14015C4F, $63066CD9, $FA0F3D63, $8D080DF5,
451 $3B6E20C8, $4C69105E, $D56041E4, $A2677172, $3C03E4D1, $4B04D447, $D20D85FD, $A50AB56B,
452 $35B5A8FA, $42B2986C, $DBBBC9D6, $ACBCF940, $32D86CE3, $45DF5C75, $DCD60DCF, $ABD13D59,
453 $26D930AC, $51DE003A, $C8D75180, $BFD06116, $21B4F4B5, $56B3C423, $CFBA9599, $B8BDA50F,
454 $2802B89E, $5F058808, $C60CD9B2, $B10BE924, $2F6F7C87, $58684C11, $C1611DAB, $B6662D3D,
455 $76DC4190, $1DB7106, $98D220BC, $EFD5102A, $71B18589, $6B6B51F, $9FBFE4A5, $E8B8D433,
456 $7807C9A2, $F00F934, $9609A88E, $E10E9818, $7F6A0DBB, $86D3D2D, $91646C97, $E6635C01,
457 $6B6B51F4, $1C6C6162, $856530D8, $F262004E, $6C0695ED, $1B01A57B, $8208F4C1, $F50FC457,
458 $65B0D9C6, $12B7E950, $8BBEB8EA, $FCB9887C, $62DD1DDF, $15DA2D49, $8CD37CF3, $FBD44C65,
459 $4DB26158, $3AB551CE, $A3BC0074, $D4BB30E2, $4ADFA541, $3DD895D7, $A4D1C46D, $D3D6F4FB,
460 $4369E96A, $346ED9FC, $AD678846, $DA60B8D0, $44042D73, $33031DE5, $AA0A4C5F, $DD0D7CC9,
461 $5005713C, $270241AA, $BE0B1010, $C90C2086, $5768B525, $206F85B3, $B966D409, $CE61E49F,
462 $5EDEF90E, $29D9C998, $B0D09822, $C7D7A8B4, $59B33D17, $2EB40D81, $B7BD5C3B, $C0BA6CAD,
463 $EDB88320, $9ABFB3B6, $3B6E20C, $74B1D29A, $EAD54739, $9DD277AF, $4DB2615, $73DC1683,
464 $E3630B12, $94643B84, $D6D6A3E, $7A6A5AA8, $E40ECF0B, $9309FF9D, $A00AE27, $7D079EB1,
465 $F00F9344, $8708A3D2, $1E01F268, $6906C2FE, $F762575D, $806567CB, $196C3671, $6E6B06E7,
466 $FED41B76, $89D32BE0, $10DA7A5A, $67DD4ACC, $F9B9DF6F, $8EBEEFF9, $17B7BE43, $60B08ED5,
467 $D6D6A3E8, $A1D1937E, $38D8C2C4, $4FDFF252, $D1BB67F1, $A6BC5767, $3FB506DD, $48B2364B,
468 $D80D2BDA, $AF0A1B4C, $36034AF6, $41047A60, $DF60EFC3, $A867DF55, $316E8EEF, $4669BE79,
469 $CB61B38C, $BC66831A, $256FD2A0, $5268E236, $CC0C7795, $BB0B4703, $220216B9, $5505262F,
470 $C5BA3BBE, $B2BD0B28, $2BB45A92, $5CB36A04, $C2D7FFA7, $B5D0CF31, $2CD99E8B, $5BDEAE1D,
471 $9B64C2B0, $EC63F226, $756AA39C, $26D930A, $9C0906A9, $EB0E363F, $72076785, $5005713,
472 $95BF4A82, $E2B87A14, $7BB12BAE, $CB61B38, $92D28E9B, $E5D5BE0D, $7CDCEFB7, $BDBDF21,
473 $86D3D2D4, $F1D4E242, $68DDB3F8, $1FDA836E, $81BE16CD, $F6B9265B, $6FB077E1, $18B74777,
474 $88085AE6, $FF0F6A70, $66063BCA, $11010B5C, $8F659EFF, $F862AE69, $616BFFD3, $166CCF45,
475 $A00AE278, $D70DD2EE, $4E048354, $3903B3C2, $A7672661, $D06016F7, $4969474D, $3E6E77DB,
476 $AED16A4A, $D9D65ADC, $40DF0B66, $37D83BF0, $A9BCAE53, $DEBB9EC5, $47B2CF7F, $30B5FFE9,
477 $BDBDF21C, $CABAC28A, $53B39330, $24B4A3A6, $BAD03605, $CDD70693, $54DE5729, $23D967BF,
478 $B3667A2E, $C4614AB8, $5D681B02, $2A6F2B94, $B40BBE37, $C30C8EA1, $5A05DF1B, $2D02EF8D);
479
480{ returns a cyclic redundancy check for a string }
481function CRCForString(AString: string): DWORD;
482var
483 i: Integer;
484begin
485 Result:=$FFFFFFFF;
486 for i := 1 to Length(AString) do
487 Result:=((Result shr 8) and $00FFFFFF) xor
488 CRC32_TABLE[(Result xor Ord(AString[i])) and $000000FF];
489end;
490
491function AppendBackSlash(var txt: string): string;
492begin
493 if RightStr(txt,1) <> '\' then
494 txt := txt + '\';
495 Result := txt;
496end;
497
498// returns special folder path on the current machine - such as Program Files etc
499// the parameter is a CSIDL windows constant
500function GetSpecialFolderPath(SpecialFolderCSIDL: integer): String;
501var
502 Path: array[0..Max_Path] of Char;
503begin
504 Path := '';
505 SHGetSpecialFolderPath(0, Path, SpecialFolderCSIDL, false);
506 Result := Path;
507 AppendBackSlash(Result);
508end;
509
510// returns Program Files path on current machine
511function GetProgramFilesPath: String;
512begin
513 Result := GetSpecialFolderPath(CSIDL_PROGRAM_FILES);
514end;
515
516// returns Program Files path on the drive where the currently running application
517// resides, if it is a different drive than the one that contains the current
518// machine's Program Files directory. This is typically used for networked drives.
519// Note that tnis only works if the mapping to the network is at the root drive
520function GetAlternateProgramFilesPath: String;
521var
522 Dir, Dir2: string;
523
524begin
525 Dir := GetProgramFilesPath;
526 Dir2 := ExtractFileDrive(Application.ExeName);
527 AppendBackSlash(Dir2);
528 Dir2 := Dir2 + 'Program Files\';
529 If (UpperCase(Dir) = UpperCase(Dir2)) then
530 Result := ''
531 else
532 Result := Dir2;
533end;
534
535// Get the Window title (Caption) of a window, given only it's handle
536function GetWindowTitle(Handle: HWND): String;
537begin
538 SetLength(Result, 240);
539 SetLength(Result, GetWindowText(Handle, PChar(Result), Length(Result)));
540end;
541
542function GetWindowClassName(Handle: HWND): String;
543begin
544 SetLength(Result, 240);
545 SetLength(Result, GetClassName(Handle, PChar(Result), Length(Result)));
546end;
547
548type
549(*
550 TVACustomWinProcInterceptor = class
551 private
552 FOldWinProc: Pointer;
553 FHexHandle: string;
554 FComponent: TWinControl;
555 procedure Initialize;
556 protected
557 constructor Create(Component: TWinControl); virtual;
558 function NewWindowProc(hWnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; virtual;
559// property OldWindowProc: Pointer read FOldWinProc;
560// property Component: TWinControl read FComponent;
561 public
562 destructor Destroy; override;
563 end;
564*)
565(*
566 TVAWinProcMessageHandler = class(TVACustomWinProcInterceptor)
567 private
568 FMessageHandlerList: TVAMethodList;
569 function DoMessageHandlers(hWnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM; var MessageHandled: boolean): LRESULT;
570 protected
571 constructor Create(Component: TWinControl); override;
572 function NewWindowProc(hWnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; override;
573 public
574 destructor Destroy; override;
575 function HandlerCount: integer;
576 procedure AddMessageHandler(event: TVAWinProcMessageEvent);
577 procedure RemoveMessageHandler(event: TVAWinProcMessageEvent);
578 end;
579*)
580
581 TVACustomMessageEventInterceptor = class
582 private
583 FOldWndMethod: TWndMethod;
584 FComponent: TWinControl;
585 protected
586 constructor Create(Component: TWinControl); virtual;
587 procedure NewMessageHandler(var Message: TMessage); virtual;
588// property OldWndMethod: TWndMethod read FOldWndMethod;
589// property Component: TWinControl read FComponent;
590 public
591 destructor Destroy; override;
592 end;
593
594 TVAMessageEventHandler = class(TVACustomMessageEventInterceptor)
595 private
596 FMessageHandlerList: TVAMethodList;
597 procedure DoMessageHandlers(var Message: TMessage; var MessageHandled: boolean);
598 protected
599 constructor Create(Component: TWinControl); override;
600 procedure NewMessageHandler(var Message: TMessage); override;
601 public
602 destructor Destroy; override;
603 function HandlerCount: integer;
604 procedure AddMessageHandler(event: TVAMessageEvent);
605 procedure RemoveMessageHandler(event: TVAMessageEvent);
606 end;
607
608(*
609 TVAWinProcAccessClass = class(TWinControl)
610 public
611 property DefWndProc;
612 end;
613*)
614
615 TVAWinProcMonitor = class(TComponent)
616 protected
617 procedure Notification(AComponent: TComponent; Operation: TOperation); override;
618 public
619 procedure RemoveFromList(AComponent: TComponent);
620 end;
621
622
623var
624// uWinProcMessageHandlers: TStringList = nil;
625 uEventMessageHandlers: TStringList = nil;
626 uHandlePointers: TStringlist = nil;
627 uWinProcMonitor: TVAWinProcMonitor = nil;
628 uMessageHandlerSystemRunning: boolean = FALSE;
629
630procedure InitializeMessageHandlerSystem;
631begin
632 if not uMessageHandlerSystemRunning then
633 begin
634// uWinProcMessageHandlers := TStringList.Create;
635// uWinProcMessageHandlers.Sorted := TRUE;
636// uWinProcMessageHandlers.Duplicates := dupAccept;
637 uEventMessageHandlers := TStringList.Create;
638 uEventMessageHandlers.Sorted := TRUE;
639 uEventMessageHandlers.Duplicates := dupAccept;
640 uHandlePointers := TStringList.Create;
641 uHandlePointers.Sorted := TRUE; // allows for faster binary searching
642 uHandlePointers.Duplicates := dupAccept;
643 uWinProcMonitor := TVAWinProcMonitor.Create(nil);
644 uMessageHandlerSystemRunning := TRUE;
645 end;
646end;
647
648procedure CleanupMessageHandlerSystem;
649
650 procedure Clear(var list: TStringList; FreeObjects: boolean = false);
651 var
652 i: integer;
653 begin
654 if assigned(list) then
655 begin
656 if FreeObjects then
657 begin
658 for I := 0 to list.Count - 1 do
659 list.Objects[i].Free;
660 end;
661 FreeAndNil(list);
662 end;
663 end;
664
665begin
666// Clear(uWinProcMessageHandlers, TRUE);
667 Clear(uEventMessageHandlers, TRUE);
668 Clear(uHandlePointers);
669 if assigned(uWinProcMonitor) then
670 FreeAndNil(uWinProcMonitor);
671 uMessageHandlerSystemRunning := FALSE;
672end;
673
674(*
675procedure AddMessageHandler(Control: TWinControl; MessageHandler: TVAWinProcMessageEvent);
676var
677 HexID: string;
678 idx: integer;
679 Handler: TVAWinProcMessageHandler;
680
681begin
682 InitializeMessageHandlerSystem;
683 HexID := FastIntToHex(LongWord(Control));
684 idx := uWinProcMessageHandlers.IndexOf(HexID);
685 if idx < 0 then
686 begin
687 Handler := TVAWinProcMessageHandler.Create(Control);
688 uWinProcMessageHandlers.AddObject(HexID, Handler);
689 end
690 else
691 Handler := TVAWinProcMessageHandler(uWinProcMessageHandlers.Objects[idx]);
692 Handler.AddMessageHandler(MessageHandler);
693end;
694*)
695
696procedure AddMessageHandler(Control: TWinControl; MessageHandler: TVAMessageEvent);
697var
698 HexID: string;
699 idx: integer;
700 Handler: TVAMessageEventHandler;
701
702begin
703 InitializeMessageHandlerSystem;
704 HexID := FastIntToHex(LongWord(Control));
705 idx := uEventMessageHandlers.IndexOf(HexID);
706 if idx < 0 then
707 begin
708 Handler := TVAMessageEventHandler.Create(Control);
709 uEventMessageHandlers.AddObject(HexID, Handler);
710 end
711 else
712 Handler := TVAMessageEventHandler(uEventMessageHandlers.Objects[idx]);
713 Handler.AddMessageHandler(MessageHandler);
714end;
715
716(*
717procedure RemoveMessageHandler(Control: TWinControl;
718 MessageHandler: TVAWinProcMessageEvent);
719var
720 HexID: string;
721 idx: integer;
722 Handler: TVAWinProcMessageHandler;
723
724begin
725 if not uMessageHandlerSystemRunning then exit;
726 HexID := FastIntToHex(LongWord(Control));
727 idx := uWinProcMessageHandlers.IndexOf(HexID);
728 if idx >= 0 then
729 begin
730 Handler := TVAWinProcMessageHandler(uWinProcMessageHandlers.Objects[idx]);
731 Handler.RemoveMessageHandler(MessageHandler);
732 if Handler.HandlerCount = 0 then
733 begin
734 Handler.Free;
735 uWinProcMessageHandlers.Delete(idx);
736 end;
737 end;
738end;
739*)
740
741procedure RemoveMessageHandler(Control: TWinControl; MessageHandler: TVAMessageEvent);
742var
743 HexID: string;
744 idx: integer;
745 Handler: TVAMessageEventHandler;
746
747begin
748 if not uMessageHandlerSystemRunning then exit;
749 HexID := FastIntToHex(LongWord(Control));
750 idx := uEventMessageHandlers.IndexOf(HexID);
751 if idx >= 0 then
752 begin
753 Handler := TVAMessageEventHandler(uEventMessageHandlers.Objects[idx]);
754 Handler.RemoveMessageHandler(MessageHandler);
755 if Handler.HandlerCount = 0 then
756 begin
757 Handler.Free;
758 uEventMessageHandlers.Delete(idx);
759 end;
760 end;
761end;
762
763procedure RemoveAllMessageHandlers(Control: TWinControl);
764var
765 HexID: string;
766 idx: integer;
767// Handler: TVAWinProcMessageHandler;
768 EventHandler: TVAMessageEventHandler;
769
770begin
771 if not uMessageHandlerSystemRunning then exit;
772 HexID := FastIntToHex(LongWord(Control));
773
774 (*
775 idx := uWinProcMessageHandlers.IndexOf(HexID);
776
777 if idx >= 0 then
778 begin
779 Handler := TVAWinProcMessageHandler(uWinProcMessageHandlers.Objects[idx]);
780 Handler.Free;
781 uWinProcMessageHandlers.Delete(idx);
782 end;
783 *)
784
785 idx := uEventMessageHandlers.IndexOf(HexID);
786 if idx >= 0 then
787 begin
788 EventHandler := TVAMessageEventHandler(uEventMessageHandlers.Objects[idx]);
789 EventHandler.Free;
790 uEventMessageHandlers.Delete(idx);
791 end;
792
793 Control.RemoveFreeNotification(uWinProcMonitor);
794end;
795
796function MessageHandlerCount(Control: TWinControl): integer;
797var
798 HexID: string;
799 idx: integer;
800// Handler: TVAWinProcMessageHandler;
801 EventHandler: TVAMessageEventHandler;
802
803begin
804 Result := 0;
805 if not uMessageHandlerSystemRunning then exit;
806
807 HexID := FastIntToHex(LongWord(Control));
808
809(* idx := uWinProcMessageHandlers.IndexOf(HexID);
810
811 if idx >= 0 then
812 begin
813 Handler := TVAWinProcMessageHandler(uWinProcMessageHandlers.Objects[idx]);
814 result := Handler.HandlerCount;
815 end;
816*)
817
818 idx := uEventMessageHandlers.IndexOf(HexID);
819 if idx >= 0 then
820 begin
821 EventHandler := TVAMessageEventHandler(uEventMessageHandlers.Objects[idx]);
822 inc(Result, EventHandler.HandlerCount);
823 end;
824end;
825
826 { TVACustomWinProc }
827
828(*
829constructor TVACustomWinProcInterceptor.Create(Component: TWinControl);
830begin
831 if not Assigned(Component) then
832 raise EInvalidPointer.Create('Component parameter unassigned');
833 FComponent := Component;
834 Initialize;
835end;
836
837destructor TVACustomWinProcInterceptor.Destroy;
838var
839 idx: integer;
840begin
841 if Assigned(FComponent) then
842 begin
843 try
844 TVAWinProcAccessClass(FComponent).DefWndProc := FOldWinProc;
845 except // just in case FComponent has been destroyed
846 end;
847 end;
848 idx := uHandlePointers.IndexOf(FHexHandle);
849 if idx >= 0 then
850 uHandlePointers.Delete(idx);
851 inherited;
852end;
853
854function TVACustomWinProcInterceptor.NewWindowProc(hWnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT;
855begin
856{
857 if (Msg = SOME_MESSAGE) then
858 begin
859 ...
860 Result := S_OK;
861 end
862 else
863}
864 Result := CallWindowProc(FOldWinProc, hWnd, Msg, WParam, LParam);
865end;
866
867
868function BaseWindowProc(hWnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
869var
870 idx: integer;
871
872begin
873 idx := uHandlePointers.IndexOf(FastIntToHex(hWnd)); // does binary search on sorted string list
874 if idx >= 0 then
875 Result := TVACustomWinProcInterceptor(uHandlePointers.Objects[idx]).NewWindowProc(hWnd, Msg, wParam, lParam)
876 else
877 Result := 0; // should never happen
878end;
879
880procedure TVACustomWinProcInterceptor.Initialize;
881var
882 idx: integer;
883begin
884 InitializeMessageHandlerSystem;
885 FComponent.HandleNeeded;
886 FHexHandle := FastIntToHex(FComponent.Handle);
887 idx := uHandlePointers.IndexOf(FHexHandle);
888 if idx < 0 then
889 uHandlePointers.AddObject(FHexHandle, Self)
890 else
891 uHandlePointers.Objects[idx] := Self;
892 FComponent.FreeNotification(uWinProcMonitor);
893 FOldWinProc := TVAWinProcAccessClass(FComponent).DefWndProc;
894 TVAWinProcAccessClass(FComponent).DefWndProc := @BaseWindowProc;
895end;
896*)
897
898{ TVAWinProcMonitor }
899
900
901// assumes object is responsible for deleting instance of TVACustomWinProc
902procedure TVAWinProcMonitor.Notification(AComponent: TComponent;
903 Operation: TOperation);
904begin
905 inherited;
906 if (Operation = opRemove) and (AComponent is TWinControl) then
907 RemoveFromList(AComponent);
908end;
909
910procedure TVAWinProcMonitor.RemoveFromList(AComponent: TComponent);
911begin
912 if AComponent is TWinControl then
913 RemoveAllMessageHandlers(TWinControl(AComponent));
914end;
915
916
917{ TVACustomMessageEventInterceptor }
918
919constructor TVACustomMessageEventInterceptor.Create(Component: TWinControl);
920begin
921 if not Assigned(Component) then
922 raise EInvalidPointer.Create('Component parameter unassigned');
923 FComponent := Component;
924 FComponent.FreeNotification(uWinProcMonitor);
925 FOldWndMethod := FComponent.WindowProc;
926 FComponent.WindowProc := NewMessageHandler;
927end;
928
929destructor TVACustomMessageEventInterceptor.Destroy;
930begin
931 FComponent.WindowProc := FOldWndMethod;
932 inherited;
933end;
934
935procedure TVACustomMessageEventInterceptor.NewMessageHandler(
936 var Message: TMessage);
937begin
938 FOldWndMethod(Message);
939end;
940
941{ TVAWinProcNotifier }
942
943(*
944procedure TVAWinProcMessageHandler.AddMessageHandler(event: TVAWinProcMessageEvent);
945begin
946 FMessageHandlerList.Add(TMethod(event));
947end;
948
949constructor TVAWinProcMessageHandler.Create(Component: TWinControl);
950begin
951 FMessageHandlerList := TVAMethodList.Create;
952 inherited Create(Component);
953end;
954
955destructor TVAWinProcMessageHandler.Destroy;
956begin
957 inherited;
958 FMessageHandlerList.Free;
959end;
960
961function TVAWinProcMessageHandler.DoMessageHandlers(hWnd: HWND; Msg: UINT;
962 wParam: WPARAM; lParam: LPARAM; var MessageHandled: boolean): LRESULT;
963var
964 Method: TMethod;
965 i: integer;
966begin
967 MessageHandled := FALSE;
968 Result := 0;
969 for i := 0 to FMessageHandlerList.Count - 1 do
970 begin
971 Method := FMessageHandlerList[i];
972 Result := TVAWinProcMessageEvent(Method)(hWnd, Msg, wParam, lParam, MessageHandled);
973 if MessageHandled then
974 break;
975 end;
976end;
977
978function TVAWinProcMessageHandler.HandlerCount: integer;
979begin
980 Result := FMessageHandlerList.Count;
981end;
982
983function TVAWinProcMessageHandler.NewWindowProc(hWnd: HWND; Msg: UINT; wParam: WPARAM;
984 lParam: LPARAM): LRESULT;
985var
986 MessageHandled: boolean;
987
988begin
989 Result := DoMessageHandlers(hWnd, Msg, wParam, lParam, MessageHandled);
990 if not MessageHandled then
991 Result := CallWindowProc(FOldWinProc, hWnd, Msg, WParam, LParam);
992end;
993
994procedure TVAWinProcMessageHandler.RemoveMessageHandler(event: TVAWinProcMessageEvent);
995begin
996 FMessageHandlerList.Remove(TMethod(event));
997end;
998*)
999
1000{ TVAMessageEventHandler }
1001
1002procedure TVAMessageEventHandler.AddMessageHandler(event: TVAMessageEvent);
1003begin
1004 FMessageHandlerList.Add(TMethod(event));
1005end;
1006
1007constructor TVAMessageEventHandler.Create(Component: TWinControl);
1008begin
1009 FMessageHandlerList := TVAMethodList.Create;
1010 inherited Create(Component);
1011end;
1012
1013destructor TVAMessageEventHandler.Destroy;
1014begin
1015 inherited;
1016 FMessageHandlerList.Free;
1017end;
1018
1019procedure TVAMessageEventHandler.DoMessageHandlers(var Message: TMessage;
1020 var MessageHandled: boolean);
1021var
1022 Method: TMethod;
1023 i: integer;
1024
1025begin
1026 MessageHandled := FALSE;
1027 for i := 0 to FMessageHandlerList.Count - 1 do
1028 begin
1029 Method := FMessageHandlerList[i];
1030 TVAMessageEvent(Method)(Message, MessageHandled);
1031 if MessageHandled then
1032 break;
1033 end;
1034end;
1035
1036function TVAMessageEventHandler.HandlerCount: integer;
1037begin
1038 Result := FMessageHandlerList.Count;
1039end;
1040
1041procedure TVAMessageEventHandler.NewMessageHandler(var Message: TMessage);
1042var
1043 MessageHandled: boolean;
1044
1045begin
1046 DoMessageHandlers(Message, MessageHandled);
1047 if not MessageHandled then
1048 FOldWndMethod(Message);
1049end;
1050
1051procedure TVAMessageEventHandler.RemoveMessageHandler(event: TVAMessageEvent);
1052begin
1053 FMessageHandlerList.Remove(TMethod(event));
1054end;
1055
1056
1057
1058type
1059 TDataArray = record
1060 private
1061 FCapacity: integer;
1062 procedure SetCapacity(Value: integer);
1063 public
1064 Data: array of DWORD;
1065 Count: integer;
1066 procedure Clear;
1067 function Size: integer;
1068 property Capacity: integer read FCapacity write SetCapacity;
1069 end;
1070
1071{ TDataArray }
1072
1073procedure TDataArray.Clear;
1074begin
1075 SetCapacity(0);
1076 SetCapacity(128);
1077end;
1078
1079procedure TDataArray.SetCapacity(Value: integer);
1080begin
1081 if FCapacity <> Value then
1082 begin
1083 FCapacity := Value;
1084 SetLength(Data, Value);
1085 if Count >= Value then
1086 Count := Value - 1;
1087 end;
1088end;
1089
1090
1091function TDataArray.Size: integer;
1092begin
1093 Result := FCapacity * SizeOf(DWORD);
1094end;
1095
1096var
1097 PIDList: TDataArray;
1098 ModuleHandles: TDataArray;
1099
1100function GetInstanceCount(ApplicationNameAndPath: string): integer; overload;
1101var
1102 i, j: DWORD;
1103 name: string;
1104 process: THandle;
1105 Output: DWORD;
1106 current: string;
1107 ok: BOOL;
1108 done: boolean;
1109
1110 function ListTooSmall(var Data: TDataArray): boolean;
1111 var
1112 ReturnCount: integer;
1113 begin
1114 Data.Count := 0;
1115 ReturnCount := Output div SizeOf(DWORD);
1116 Result := (ReturnCount >= Data.Capacity);
1117 if Result then
1118 Data.Capacity := Data.Capacity * 2
1119 else
1120 Data.Count := ReturnCount;
1121 end;
1122
1123begin
1124 Result := 0;
1125 current := UpperCase(ApplicationNameAndPath);
1126 PIDList.Clear;
1127 repeat
1128 done := TRUE;
1129 ok := EnumProcesses(pointer(PIDList.Data), PIDList.Size, Output);
1130 if ok and ListTooSmall(PIDList) then
1131 done := FALSE;
1132 until done or (not ok);
1133 if ok then
1134 begin
1135 for I := 0 to PIDList.Count - 1 do
1136 begin
1137 Process := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, FALSE, PIDList.Data[i]);
1138 if Process <> 0 then
1139 begin
1140 try
1141 ModuleHandles.Clear;
1142 repeat
1143 done := TRUE;
1144 ok := EnumProcessModules(Process, Pointer(ModuleHandles.Data), ModuleHandles.Size, Output);
1145 if ok and ListTooSmall(ModuleHandles) then
1146 done := FALSE;
1147 until done or (not ok);
1148 if ok then
1149 begin
1150 for j := 0 to ModuleHandles.Count - 1 do
1151 begin
1152 SetLength(name, MAX_PATH*2);
1153 SetLength(name, GetModuleFileNameEx(Process, ModuleHandles.Data[j], PChar(name), MAX_PATH*2));
1154 name := UpperCase(name);
1155 if name = current then
1156 begin
1157 inc(Result);
1158 break;
1159 end;
1160 end;
1161 end;
1162 finally
1163 CloseHandle(Process);
1164 end;
1165 end;
1166 end;
1167 end;
1168 PIDList.SetCapacity(0);
1169 ModuleHandles.SetCapacity(0);
1170end;
1171
1172
1173function GetInstanceCount: integer;
1174begin
1175 Result := GetInstanceCount(ParamStr(0));
1176end;
1177
1178function AnotherInstanceRunning: boolean;
1179begin
1180 Result := (GetInstanceCount > 1);
1181end;
1182
1183procedure VersionStringSplit(const VerStr: string; var Val1: integer);
1184var
1185 dummy2, dummy3, dummy4: integer;
1186begin
1187 VersionStringSplit(VerStr, Val1, dummy2, dummy3, dummy4);
1188end;
1189
1190procedure VersionStringSplit(const VerStr: string; var Val1, Val2: integer);
1191var
1192 dummy3, dummy4: integer;
1193begin
1194 VersionStringSplit(VerStr, Val1, Val2, dummy3, dummy4);
1195end;
1196
1197procedure VersionStringSplit(const VerStr: string; var Val1, Val2, Val3: integer);
1198var
1199 dummy4: integer;
1200begin
1201 VersionStringSplit(VerStr, Val1, Val2, Val3, dummy4);
1202end;
1203
1204procedure VersionStringSplit(const VerStr: string; var Val1, Val2, Val3, Val4: integer);
1205var
1206 temp: string;
1207
1208 function GetNum: integer;
1209 var
1210 idx: integer;
1211
1212 begin
1213 idx := pos('.', temp);
1214 if idx < 1 then
1215 idx := Length(temp) + 1;
1216 Result := StrToIntDef(copy(temp, 1, idx-1), 0);
1217 delete(temp, 1, idx);
1218 end;
1219
1220begin
1221 temp := VerStr;
1222 Val1 := GetNum;
1223 Val2 := GetNum;
1224 Val3 := GetNum;
1225 Val4 := GetNum;
1226end;
1227
1228const
1229 FILE_VER_PREFIX = '\StringFileInfo\';
1230// FILE_VER_COMMENTS = '\StringFileInfo\040904E4\Comments';
1231
1232function FileVersionValue(const AFileName, AValueName: string): string;
1233type
1234 TValBuf = array[0..255] of Char;
1235 PValBuf = ^TValBuf;
1236
1237var
1238 VerSize, ValSize, AHandle: DWORD;
1239 VerBuf: Pointer;
1240 ValBuf: PValBuf;
1241 Output, Query: string;
1242 POutput: PChar;
1243begin
1244 Result := '';
1245 VerSize:=GetFileVersionInfoSize(PChar(AFileName), AHandle);
1246 if VerSize > 0 then
1247 begin
1248 GetMem(VerBuf, VerSize);
1249 try
1250 GetFileVersionInfo(PChar(AFileName), AHandle, VerSize, VerBuf);
1251 VerQueryValue(VerBuf, PChar('\VarFileInfo\Translation'), Pointer(ValBuf), ValSize);
1252 Query := FILE_VER_PREFIX + IntToHex(LoWord(PLongInt(ValBuf)^),4)+
1253 IntToHex(HiWord(PLongInt(ValBuf)^),4)+
1254 '\'+AValueName;
1255 VerQueryValue(VerBuf, PChar(Query), Pointer(ValBuf), ValSize);
1256 SetString(Output, ValBuf^, ValSize);
1257 POutput := PChar(Output);
1258 Result := POutput;
1259 finally
1260 FreeMem(VerBuf);
1261 end;
1262 end;
1263end;
1264
1265// compares up to 4 pieces of a numeric version, returns true if CheckVersion is >= OriginalVersion
1266// allows for . and , delimited version numbers
1267function VersionOK(OriginalVersion, CheckVersion: string): boolean;
1268var
1269 v1, v2, v3, v4, r1, r2, r3, r4: Integer;
1270
1271 function GetV(var Version: string): integer;
1272 var
1273 idx: integer;
1274 delim: string;
1275 begin
1276 if pos('.', Version) > 0 then
1277 delim := '.'
1278 else
1279 delim := ',';
1280 idx := pos(delim, version);
1281 if idx < 1 then
1282 idx := length(Version) + 1;
1283 Result := StrToIntDef(copy(version, 1, idx-1), 0);
1284 delete(version, 1, idx);
1285 end;
1286
1287 procedure parse(const v: string; var p1, p2, p3, p4: integer);
1288 var
1289 version: string;
1290 begin
1291 version := v;
1292 p1 := GetV(version);
1293 p2 := GetV(version);
1294 p3 := GetV(version);
1295 p4 := GetV(version);
1296 end;
1297
1298begin
1299 parse(OriginalVersion, r1, r2, r3, r4);
1300 parse(CheckVersion, v1, v2, v3, v4);
1301 Result := FALSE;
1302 if v1 > r1 then
1303 Result := TRUE
1304 else if v1 = r1 then
1305 begin
1306 if v2 > r2 then
1307 Result := TRUE
1308 else if v2 = r2 then
1309 begin
1310 if v3 > r3 then
1311 Result := TRUE
1312 else if v3 = r3 then
1313 begin
1314 if v4 >= r4 then
1315 Result := TRUE
1316 end;
1317 end;
1318 end;
1319end;
1320
1321function ExecuteAndWait(FileName: string; Parameters: String = ''): integer;
1322var
1323 exec, shell: OleVariant;
1324 line: string;
1325
1326begin
1327 if copy(FileName,1,1) <> '"' then
1328 line := '"' + FileName + '"'
1329 else
1330 line := FileName;
1331 if Parameters <> '' then
1332 line := line + ' ' + Parameters;
1333 shell := CreateOleObject('WScript.Shell');
1334 try
1335 exec := shell.Exec(line);
1336 try
1337 While exec.status = 0 do
1338 Sleep(100);
1339 Result := Exec.ExitCode;
1340 finally
1341 VarClear(exec);
1342 end;
1343 finally
1344 VarClear(shell);
1345 end;
1346end;
1347
1348{
1349function ExecuteAndWait(FileName: string; Parameters: String = ''): DWORD;
1350var
1351 SEI:TShellExecuteInfo;
1352begin
1353 FillChar(SEI,SizeOf(SEI),0);
1354 with SEI do begin
1355 cbSize:=SizeOf(SEI);
1356 lpVerb:='open';
1357 lpFile:=PAnsiChar(FileName);
1358 lpDirectory := PAnsiChar(ExtractFileDir(FileName));
1359 if Parameters <> '' then
1360 lpParameters := PAnsiChar(Parameters);
1361 nShow:=SW_SHOW;
1362 fMask:=SEE_MASK_NOCLOSEPROCESS;
1363 end;
1364 ShellExecuteEx(@SEI);
1365 WaitForSingleObject(SEI.hProcess, INFINITE);
1366 if not GetExitCodeProcess(SEI.hProcess, Result) then
1367 Result := 0;
1368 CloseHandle(SEI.hProcess);
1369end;
1370 }
1371
1372// when called inside a DLL, returns the fully qualified name of the DLL file
1373// must pass an address or a class or procedure that's been defined inside the DLL
1374function GetDLLFileName(Address: Pointer): string;
1375var
1376 ProcessHandle: THandle;
1377 Output: DWORD;
1378 i, max: integer;
1379 ModuleHandles: array[0..1023] of HMODULE;
1380 info: _MODULEINFO;
1381 pinfo: LPMODULEINFO;
1382 adr: Int64;
1383
1384begin
1385 Result := '';
1386 ProcessHandle := GetCurrentProcess;
1387 if EnumProcessModules(ProcessHandle, @ModuleHandles, sizeof(ModuleHandles), output) then
1388 begin
1389 adr := Int64(Address);
1390 max := (output div sizeof(HMODULE))-1;
1391 pinfo := @info;
1392 for i := 0 to max do
1393 begin
1394 if GetModuleInformation(ProcessHandle, ModuleHandles[i], pinfo, sizeof(_MODULEINFO)) then
1395 begin
1396 if (adr > Int64(info.lpBaseOfDll)) and (adr < (Int64(info.lpBaseOfDll) + info.SizeOfImage)) then
1397 begin
1398 SetLength(Result, MAX_PATH);
1399 SetLength(Result, GetModuleFileName(ModuleHandles[i], PChar(Result), Length(Result)));
1400 break;
1401 end;
1402 end;
1403 end;
1404 end;
1405end;
1406
1407initialization
1408 ScreenReaderSupportEnabled;
1409
1410finalization
1411 CleanupMessageHandlerSystem;
1412
1413end.
1414
Note: See TracBrowser for help on using the repository browser.