Detect Windows x32-x64 example: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Detect Windows x32-x64 example
| Old | New | Line | |
|---|---|---|---|
| 1 | + | Sometimes you want to know the OS bitness. For Windows, works for me on Vista x64 with FPC x86 compiler - thanks to [German Lazarus forum](http://www.lazarusforum.de/viewtopic.php?f=55&t=5287%7Cthe): | |
| 2 | + | ||
| 3 | + | ``` pascal | |
| 4 | + | program bitness; | |
| 5 | + | ||
| 6 | + | {$mode objfpc}{$H+} | |
| 7 | + | {$APPTYPE CONSOLE} | |
| 8 | + | uses {$IFDEF UNIX} {$IFDEF UseCThreads} | |
| 9 | + | cthreads, {$ENDIF} {$ENDIF} | |
| 10 | + | Classes, | |
| 11 | + | SysUtils, | |
| 12 | + | Windows; | |
| 13 | + | ||
| 14 | + | function IsWindows64: boolean; | |
| 15 | + | { | |
| 16 | + | Detect if we are running on 64 bit Windows or 32 bit Windows, | |
| 17 | + | independently of bitness of this program. | |
| 18 | + | Original source: | |
| 19 | + | http://www.delphipraxis.net/118485-ermitteln-ob-32-bit-oder-64-bit-betriebssystem.html | |
| 20 | + | modified for FreePascal in German Lazarus forum: | |
| 21 | + | http://www.lazarusforum.de/viewtopic.php?f=55&t=5287 | |
| 22 | + | } | |
| 23 | + | {$ifdef WIN32} //Modified KpjComp for 64bit compile mode | |
| 24 | + | type | |
| 25 | + | TIsWow64Process = function( // Type of IsWow64Process API fn | |
| 26 | + | Handle: Windows.THandle; var Res: Windows.BOOL): Windows.BOOL; stdcall; | |
| 27 | + | var | |
| 28 | + | IsWow64Result: Windows.BOOL; // Result from IsWow64Process | |
| 29 | + | IsWow64Process: TIsWow64Process; // IsWow64Process fn reference | |
| 30 | + | begin | |
| 31 | + | // Try to load required function from kernel32 | |
| 32 | + | IsWow64Process := TIsWow64Process(Windows.GetProcAddress( | |
| 33 | + | Windows.GetModuleHandle('kernel32'), 'IsWow64Process')); | |
| 34 | + | if Assigned(IsWow64Process) then | |
| 35 | + | begin | |
| 36 | + | // Function is implemented: call it | |
| 37 | + | if not IsWow64Process(Windows.GetCurrentProcess, IsWow64Result) then | |
| 38 | + | raise SysUtils.Exception.Create('IsWindows64: bad process handle'); | |
| 39 | + | // Return result of function | |
| 40 | + | Result := IsWow64Result; | |
| 41 | + | end | |
| 42 | + | else | |
| 43 | + | // Function not implemented: can't be running on Wow64 | |
| 44 | + | Result := False; | |
| 45 | + | {$else} //if were running 64bit code, OS must be 64bit :) | |
| 46 | + | begin | |
| 47 | + | Result := True; | |
| 48 | + | {$endif} | |
| 49 | + | end; | |
| 50 | + | ||
| 51 | + | begin | |
| 52 | + | try | |
| 53 | + | if IsWindows64 then | |
| 54 | + | begin | |
| 55 | + | writeln('This operating system is 64 bit.'); | |
| 56 | + | end | |
| 57 | + | else | |
| 58 | + | begin | |
| 59 | + | writeln('This operating system is 32 bit.'); | |
| 60 | + | end; | |
| 61 | + | except | |
| 62 | + | on E: exception do | |
| 63 | + | begin | |
| 64 | + | writeln('Could not determine bitness of operating system. Error details: ' + E.ClassName+'/'+E.Message); | |
| 65 | + | end; | |
| 66 | + | end; | |
| 67 | + | end. | |
| 68 | + | ``` | |
| 69 | + | ||
| 70 | + | --- | |
| 71 | + | *From the Free Pascal wiki, page "Detect Windows x32-x64 example", as it stood on 2020-02-13 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 72 | + | Category: Code Snippets |
