Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi Windowsversion ermitteln in einer Konsolenanwendung? (https://www.delphipraxis.net/11462-windowsversion-ermitteln-einer-konsolenanwendung.html)

mrmiagi 7. Nov 2003 09:19


Windowsversion ermitteln in einer Konsolenanwendung?
 
Hallo,
habe diesen Code von MathiasSimmack hier im Forum gefunden. Wollte mal wissen wie ich diesen code in einer Konsolenanwendung verwenden kann?? Wäre sehr dankbar.

Delphi-Quellcode:
// 
// taken from PSDK Feb 2003 - "Getting the System version"
// (ms-help://MS.PSDK.1033/sysinfo/base/getting_the_system_version.htm)
// 
function GetWinVersion: string;
var
  osvi            : TOSVersionInfo;
  bOsVersionInfoEx : boolean;
  key             : HKEY;
  szProductType   : array[0..79]of char;
  dwBuflen        : dword;
begin
  // Try calling GetVersionEx using the OSVERSIONINFOEX structure.
  // If that fails, try using the OSVERSIONINFO structure.
  ZeroMemory(@osvi,sizeof(TOSVersionInfo));
  osvi.dwOSVersionInfoSize := sizeof(TOSVersionInfo);

  bOsVersionInfoEx := GetVersionEx(osvi);
  if(not bOsVersionInfoEx) then begin
    osvi.dwOSVersionInfoSize := VERSIONINFOSIZE;

    if(not GetVersionEx(osvi)) then begin
      Result := 'Fehler bei der Ermittlung der Windows-Version';
      exit;
    end;
  end;

  case osvi.dwPlatformId of
    // Test for the Windows NT product family.
    VER_PLATFORM_WIN32_NT:
      begin
        // Test for the specific product family.
        if(osvi.dwMajorVersion = 5) and (osvi.dwMinorVersion = 2) then
          Result := 'Microsoft Windows Server 2003 family, ';

        if(osvi.dwMajorVersion = 5) and (osvi.dwMinorVersion = 1) then
          Result := 'Microsoft Windows XP ';

        if(osvi.dwMajorVersion = 5) and (osvi.dwMinorVersion = 0) then
          Result := 'Microsoft Windows 2000 ';

        if(osvi.dwMajorVersion <= 4) then
          Result := 'Microsoft Windows NT ';

        // Test for specific product on Windows NT 4.0 SP6 and later.
        if(bOsVersionInfoEx) then begin
          // Test for the workstation type.
          if(osvi.wProductType = VER_NT_WORKSTATION) then begin
            if(osvi.dwMajorVersion = 4) then
              Result := Result + 'Workstation 4.0 '
            else if(osvi.wSuiteMask and VER_SUITE_PERSONAL <> 0) then
              Result := Result + 'Home Edition '
            else
              Result := Result + 'Professional ';
          end
          // Test for the server type.
          else if(osvi.wProductType = VER_NT_SERVER) then begin
            if(osvi.dwMajorVersion = 5) and
              (osvi.dwMinorVersion = 2) then
            begin // Win 2003 
              if(osvi.wSuiteMask and VER_SUITE_DATACENTER <> 0) then
                Result := Result + 'Datacenter Edition '
              else if(osvi.wSuiteMask and VER_SUITE_ENTERPRISE <> 0) then
                Result := Result + 'Enterprise Edition '
              else if(osvi.wSuiteMask = VER_SUITE_BLADE) then
                Result := Result + 'Web Edition '
              else
                Result := Result + 'Standard Edition ';
            end // Win 2000 
            else if(osvi.dwMajorVersion = 5) and
              (osvi.dwMinorVersion = 0) then
            begin
              if(osvi.wSuiteMask and VER_SUITE_DATACENTER <> 0) then
                Result := Result + 'Datacenter Server '
              else if(osvi.wSuiteMask and VER_SUITE_ENTERPRISE <> 0) then
                Result := Result + 'Advanced Server '
              else
                Result := Result + 'Server ';
            end
            else begin // Windows NT 4.0 
              if(osvi.wSuiteMask and VER_SUITE_ENTERPRISE <> 0) then
                Result := Result + 'Server 4.0, Enterprise Edition '
              else
                Result := Result + 'Server 4.0 ';
            end;
          end
        end
        // Test for specific product on Windows NT 4.0 SP5 and earlier
        else begin
          dwBufLen := sizeof(szProductType);

          if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
            'SYSTEM\CurrentControlSet\Control\ProductOptions',0,
            KEY_QUERY_VALUE,key) = ERROR_SUCCESS) then
          try
            ZeroMemory(@szProductType,sizeof(szProductType));

            if(RegQueryValueEx(key,'ProductType',nil,nil,
              @szProductType,@dwBufLen) <> ERROR_SUCCESS) or
              (dwBufLen > sizeof(szProductType)) then
            ZeroMemory(@szProductType,sizeof(szProductType));
          finally
            RegCloseKey(key);
          end;

          if(lstrcmpi('WINNT',szProductType) = 0) then
            Result := Result + 'Workstation ';
          if(lstrcmpi('LANMANNT',szProductType) = 0) then
            Result := Result + 'Server ';
          if(lstrcmpi('SERVERNT',szProductType) = 0) then
            Result := Result + 'Advanced Server ';

          Result := Format('%s%d.%d',[Result,osvi.dwMajorVersion,
            osvi.dwMinorVersion]);
        end;

        // Display service pack (if any) and build number.
        if(osvi.dwMajorVersion = 4) and
          (lstrcmpi(osvi.szCSDVersion,'Service Pack 6') = 0) then
        begin
          // Test for SP6 versus SP6a.
          if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
            'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Hotfix\Q246009',
            0,KEY_QUERY_VALUE,key) = ERROR_SUCCESS) then

            Result := Format('%sService Pack 6a (Build %d)',[Result,
              osvi.dwBuildNumber and $ffff])
          else
            // Windows NT 4.0 prior to SP6a
            Result := Format('%s%s (Build %d)',[Result,
              osvi.szCSDVersion,osvi.dwBuildNumber and $ffff]);
          RegCloseKey(key);
        end
        // Windows NT 3.51 and earlier or Windows 2000 and later
        else begin
          Result := Format('%s%s (Build %d)',[Result,
            osvi.szCSDVersion,osvi.dwBuildNumber and $ffff]);
        end;
      end;
    // Test for the Windows 95 product family.
    VER_PLATFORM_WIN32_WINDOWS:
      begin
        if(osvi.dwMajorVersion = 4) and
          (osvi.dwMinorVersion = 0) then
        begin
          Result := 'Microsoft Windows 95 ';
          if(osvi.szCSDVersion[0] = 'C') or
            (osvi.szCSDVersion[0] = 'B') then Result := Result + 'OSR2 ';
        end;

        if(osvi.dwMajorVersion = 4) and
          (osvi.dwMinorVersion = 10) then
        begin
          Result := 'Microsoft Windows 98 ';
          if(osvi.szCSDVersion[0] = 'A') then Result:= Result + 'SE ';
        end;

        if(osvi.dwMajorVersion = 4) and
          (osvi.dwMinorVersion = 90) then
        begin
          Result := 'Microsoft Windows Millennium Edition';
        end;
      end;
    VER_PLATFORM_WIN32s:
      Result := 'Microsoft Win32s';
  end;
end;


Source:
unit Windows_Fragment;

interface

uses
  Windows;

type
  POSVersionInfoA = ^TOSVersionInfoA;
  POSVersionInfoW = ^TOSVersionInfoW;
  POSVersionInfo = POSVersionInfoA;
  _OSVERSIONINFOA = record
    dwOSVersionInfoSize: DWORD;
    dwMajorVersion: DWORD;
    dwMinorVersion: DWORD;
    dwBuildNumber: DWORD;
    dwPlatformId: DWORD;
    szCSDVersion: array[0..127] of AnsiChar; { Maintenance string for PSS usage } 
    wServicePackMajor,
    wServicePackMinor,
    wSuiteMask        : word;
    wProductType,
    wReserved         : byte;
  end;
  {$EXTERNALSYM _OSVERSIONINFOA} 
  _OSVERSIONINFOW = record
    dwOSVersionInfoSize: DWORD;
    dwMajorVersion: DWORD;
    dwMinorVersion: DWORD;
    dwBuildNumber: DWORD;
    dwPlatformId: DWORD;
    szCSDVersion: array[0..127] of WideChar; { Maintenance string for PSS usage } 
    wServicePackMajor,
    wServicePackMinor,
    wSuiteMask        : word;
    wProductType,
    wReserved         : byte;
  end;
  {$EXTERNALSYM _OSVERSIONINFOW} 
  _OSVERSIONINFO = _OSVERSIONINFOA;
  TOSVersionInfoA = _OSVERSIONINFOA;
  TOSVersionInfoW = _OSVERSIONINFOW;
  TOSVersionInfo = TOSVersionInfoA;
  OSVERSIONINFOA = _OSVERSIONINFOA;
  {$EXTERNALSYM OSVERSIONINFOA} 
  {$EXTERNALSYM OSVERSIONINFO} 
  OSVERSIONINFOW = _OSVERSIONINFOW;
  {$EXTERNALSYM OSVERSIONINFOW} 
  {$EXTERNALSYM OSVERSIONINFO} 
  OSVERSIONINFO = OSVERSIONINFOA;

const
  {$EXTERNALSYM VERSIONINFOSIZEA} 
  VERSIONINFOSIZEA = sizeof(TOSVersionInfoA) - 
    (sizeof(word) * 3) - (sizeof(byte) * 2);
  {$EXTERNALSYM VERSIONINFOSIZEW} 
  VERSIONINFOSIZEW = sizeof(TOSVersionInfoW) - 
    (sizeof(word) * 3) - (sizeof(byte) * 2);
  {$EXTERNALSYM VERSIONINFOSIZE} 
  VERSIONINFOSIZE  = VERSIONINFOSIZEA;


const
  // 
  // RtlVerifyVersionInfo() os product type values
  // 
  VER_NT_WORKSTATION                 = $0000001;
  VER_NT_DOMAIN_CONTROLLER           = $0000002;
  VER_NT_SERVER                      = $0000003;

  VER_SERVER_NT                      = $80000000;
  VER_WORKSTATION_NT                 = $40000000;
  VER_SUITE_SMALLBUSINESS            = $00000001;
  VER_SUITE_ENTERPRISE               = $00000002;
  VER_SUITE_BACKOFFICE               = $00000004;
  VER_SUITE_COMMUNICATIONS           = $00000008;
  VER_SUITE_TERMINAL                 = $00000010;
  VER_SUITE_SMALLBUSINESS_RESTRICTED = $00000020;
  VER_SUITE_EMBEDDEDNT               = $00000040;
  VER_SUITE_DATACENTER               = $00000080;
  VER_SUITE_SINGLEUSERTS             = $00000100;
  VER_SUITE_PERSONAL                 = $00000200;
  VER_SUITE_BLADE                    = $00000400;
  VER_SUITE_EMBEDDED_RESTRICTED      = $00000800;
  VER_SUITE_SECURITY_APPLIANCE       = $00001000;




function GetVersionExA(var lpVersionInformation: TOSVersionInfo): BOOL; stdcall;
{$EXTERNALSYM GetVersionExA} 
function GetVersionExW(var lpVersionInformation: TOSVersionInfo): BOOL; stdcall;
{$EXTERNALSYM GetVersionExW} 
function GetVersionEx(var lpVersionInformation: TOSVersionInfo): BOOL; stdcall;
{$EXTERNALSYM GetVersionEx} 

implementation

function GetVersionExA; external kernel32 name 'GetVersionExA';
function GetVersionExW; external kernel32 name 'GetVersionExW';
function GetVersionEx; external kernel32 name 'GetVersionExA';

end.

Phoenix 7. Nov 2003 09:41

Re: Windowsversion ermitteln in einer Konsolenanwendung?
 
Mit
Delphi-Quellcode:
myString := GetWinVersion;
Du musst die Unit unten (Windows_Fragment) in eine eigene Datei packen und dann noch

uses
Windows, Windows_Fragment;

In die Konsolenanwendung reinschreiben.

mrmiagi 7. Nov 2003 09:58

Re: Windowsversion ermitteln in einer Konsolenanwendung?
 
Erstaml danke für deine schnelle Antwort. Das habe ich schon gemacht, aber beim combilieren kommt ein Fehler an dieser Stelle
Delphi-Quellcode:
 if(osvi.wProductType = VER_NT_WORKSTATION) then begin
[Fehler] Project1.dpr(52): Undefinierter Bezeichner: 'wProductType'

Führ ich den Quellcode dagegen in einer normalen Anwendung aus, funktioniert das einwandfrei!

mrmiagi 13. Nov 2003 10:05

Re: Windowsversion ermitteln in einer Konsolenanwendung?
 
Kann es denn niemand mal testen, oder so?
Wäre sehr dankbar! :-D

Hansa 13. Nov 2003 11:41

Re: Windowsversion ermitteln in einer Konsolenanwendung?
 
Äh, stehe ich jetzt auf dem Schlauch, oder ist das zu ungenau oder was ?

Du brauchst doch nur VER einzugeben.

MathiasSimmack 13. Nov 2003 11:56

Re: Windowsversion ermitteln in einer Konsolenanwendung?
 
Zitat:

Zitat von mrmiagi
Kann es denn niemand mal testen, oder so?
Wäre sehr dankbar! :-D

Wie dankbar? :mrgreen:
Ich hab´s selbst probiert:
Delphi-Quellcode:
{$APPTYPE CONSOLE}
program console;

uses
  Windows,
  Windows_Fragment,
  SysUtils;

function GetWinVersion: string;
{ ... }

begin
  MessageBox(0,pchar(GetWinVersion),'Info',0);
end.
Wichtig ist die Reihenfolge der Units! Wenn du
Delphi-Quellcode:
uses
  Windows_Fragment,
  Windows,
  SysUtils;
schreibst, dann siehst du die Fehlermeldung, weil in dem Fall die Original-Windows.pas hinter der Fragmentdatei geladen wird. Dadurch wird das alte Record genommen, und das kennt (in meinem Fall: Delphi 5) die erweiterten Membervariablen nicht.

So wie ich den obigen Auszug gepostet habe, so funktioniert´s bei mir ohne Fehlermeldung.

Gruß.

Luckie 13. Nov 2003 14:12

Re: Windowsversion ermitteln in einer Konsolenanwendung?
 
@Hansa: Und wie bekommst du das Ergebnis von Ver in dein Programm, um damit weiterzuarbeiten? :gruebel:

MathiasSimmack 13. Nov 2003 14:15

Re: Windowsversion ermitteln in einer Konsolenanwendung?
 
*hüstel* Was is´n das? VER? :?

Praktikant 13. Nov 2003 15:58

Re: Windowsversion ermitteln in einer Konsolenanwendung?
 
starte mal die Windows Eingabeaufforderung und gib ver ein :zwinker:

Hansa 13. Nov 2003 17:27

Re: Windowsversion ermitteln in einer Konsolenanwendung?
 
Oh, das weiß sogar der "Praktikant" :mrgreen:


Alle Zeitangaben in WEZ +1. Es ist jetzt 01:56 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz