Recent

Author Topic: How to define OS version on compilation stage?  (Read 4557 times)

anna

  • Sr. Member
  • ****
  • Posts: 426
How to define OS version on compilation stage?
« on: July 02, 2015, 07:22:05 pm »
I need to use Windows7 specific function declaration when application is used on Win7 and do not use, when it runs on WinXP
Code: [Select]
type
  DYNAMIC_TIME_ZONE_INFORMATION = record
       Bias : LONG;
       StandardName : array[0..31] of WCHAR;
       StandardDate : SYSTEMTIME;
       StandardBias : LONG;
       DaylightName : array[0..31] of WCHAR;
       DaylightDate : SYSTEMTIME;
       DaylightBias : LONG;
       TimeZoneKeyName:array[0..127] of WCHAR;
       DynamicDaylightTimeDisabled:boolean;
    end;
  LPDYNAMIC_TIME_ZONE_INFORMATION = ^DYNAMIC_TIME_ZONE_INFORMATION;

function SetDynamicTimeZoneInformation(lpDynamicTimeZoneInformation:LPDYNAMIC_TIME_ZONE_INFORMATION):WINBOOL; external 'kernel32' name 'SetDynamicTimeZoneInformation';

But when I start it on WinXP , I get error "Entry point of SetDynamicTimeZoneInformation not found in kernel32.dll".
WinXP's kernel32 has not this API. So I need some thing like if-then-else statement with version detection bun outside of begin-end block.
What should I do?
« Last Edit: July 02, 2015, 07:27:10 pm by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: How to define OS version on compilation stage?
« Reply #1 on: July 02, 2015, 07:29:56 pm »
In SysUtils:

- function CheckWin32Version(Major,Minor : Integer ): Boolean;
- or directly using the Win32MajorVersion, Win32MinorVersion variables.

Anyway, you'll also need to dynamically load this function from the kernel32 .dll (i.e. you can't use a static binding in your case).

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: How to define OS version on compilation stage?
« Reply #2 on: July 02, 2015, 07:36:27 pm »
In SysUtils:

- function CheckWin32Version(Major,Minor : Integer ): Boolean;
- or directly using the Win32MajorVersion, Win32MinorVersion variables.

Anyway, you'll also need to dynamically load this function from the kernel32 .dll (i.e. you can't use a static binding in your case).
Do you mean GetProcAddress? Can you give piece of code?
WinXP SP3 Pro Russian 32-bit (5.1.2600)

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: How to define OS version on compilation stage?
« Reply #3 on: July 02, 2015, 07:58:30 pm »
Do you mean GetProcAddress? Can you give piece of code?

Thats' it.

A basic sample:
Code: [Select]
uses
  ..., Windows;

procedure TForm1.Button1Click(Sender: TObject);
var PAddrGetDiskFreeSpaceExW: function(lpDirectoryName: LPCWSTR; var lpFreeBytesAvailableToCaller, lpTotalNumberOfBytes, lpTotalNumberOfFreeBytes: int64): BOOL; stdcall;
var FreeSpaceAvailable, TotalSpace, AllFree: int64;
begin
  FARPROC(PAddrGetDiskFreeSpaceExW) :=
    GetProcAddress(GetModuleHandle('kernel32.dll'), 'GetDiskFreeSpaceExW');
  if Assigned(PAddrGetDiskFreeSpaceExW) then
    begin
      // Your code...
      PAddrGetDiskFreeSpaceExW('C:\', FreeSpaceAvailable, TotalSpace, AllFree);
      ShowMessage('Function Found: FreeSpace for Disk C='+IntToStr(FreeSpaceAvailable));
    end
  else
    ShowMessage('** Error** Function NOT Found');
end;

It's valid for {$mode objfpc}. In case of {$mode delphi}, use @xxxx instead of FARPROC(xxxx).
« Last Edit: July 02, 2015, 10:27:44 pm by ChrisF »

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: How to define OS version on compilation stage?
« Reply #4 on: July 02, 2015, 09:56:54 pm »
I found little different example : http://wiki.freepascal.org/DLL_dynamically_load/de .
WinXP SP3 Pro Russian 32-bit (5.1.2600)

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: How to define OS version on compilation stage?
« Reply #5 on: July 02, 2015, 10:19:08 pm »
No, it's in fact the same thing.

This only difference is that the system .dll "kernel32.dll" is already loaded into your application (unless you've got a very specific one; I'm not even sure it's possible with FPC or any Windows executable ?).

Anyway, concretely it means that you don't need to load it. The rest of the code is then identical, once you've loaded it and get a Handle on it.

While your link deals with a user's dll: which you'll usually have to load before using it.


** Edit ** And according to your first post, you need to get a function from the "kernel32.dll"; so, my sample...
« Last Edit: July 02, 2015, 11:03:29 pm by ChrisF »

 

TinyPortal © 2005-2018