Recent

Author Topic: Symbol - Handle - is not portable  (Read 2300 times)

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Symbol - Handle - is not portable
« on: February 19, 2019, 08:30:20 am »
Hi to all

When compiling the below code following warning is displayed. Why this warning is displayed only for Application.Handle and not for Form1.Handle. Bit confused. This code is windows specific. Because


Quote
Form1.Handle = HWND = HANDLE = System.Thandle;
Application.Handle = THandle = System.THandle; 


Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   a: LONG_PTR;
  4. begin
  5.  a :=  GetWindowLongPtr(Application.Handle, GWL_WNDPROC);
  6.  ShowMessage(a.ToString);
  7. end;

Code: Pascal  [Select][+][-]
  1. unit1.pas(37,37) Warning: Symbol "Handle" is not portable

Is there any other method to get the handle of Application. Without using Application.Handle. Or is there any work around for this warning.
Holiday season is online now. :-)

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Symbol - Handle - is not portable
« Reply #1 on: February 19, 2019, 08:51:05 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   a: LONG_PTR;
  4. begin
  5.  a :=  GetWindowLongPtr(Application.Handle, GWL_WNDPROC);
  6.  ShowMessage(a.ToString);
  7. end;

Code: Pascal  [Select][+][-]
  1. unit1.pas(37,37) Warning: Symbol "Handle" is not portable
You're using GetWindowLongPtr using Application.Handle.  I don't know much about the Lazarus classes/objects but, I seriously doubt Application.Handle is a window handle which would explain why you get a warning with that one and not with Form1's handle (that one is a window handle.)

Hopefully someone who knows the Lazarus framework can confirm that.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Symbol - Handle - is not portable
« Reply #2 on: February 19, 2019, 08:57:01 am »
No: on Windows it is a real WINAPI handle. And those are of course not portable. E.g. GetWindowLongPtr and such can not be used on other platforms and the format / type of the handle may differ (is different) on other platforms.  In this case it is the use of the handle that triggers the warning. If you would use Tform.handle in the same manner, you will get the same warning. Under windows all handles are system.THandle which is on windows a windows specific handle.
« Last Edit: February 19, 2019, 09:04:11 am by Thaddy »
Specialize a type, not a var.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Symbol - Handle - is not portable
« Reply #3 on: February 19, 2019, 09:12:51 am »
Nope, there won't be the same warning:
Code: Pascal  [Select][+][-]
  1. // Forms.pp:
  2.   TApplication = class(TCustomApplication)
  3.   public
  4.     // ...
  5.     property Handle: THandle read GetHandle write SetHandle; platform;
  6.     // ...
  7.   end;
  8.  
  9. // Controls.pp (TForm is a TWinControl)
  10.   TWinControl = class(TControl)
  11.   public
  12.     // ...
  13.     property Handle: HWND read GetHandle write SetHandle;  
  14.     // ...
  15.   end;
  16.  

As you can see TApplication.Handle has the platform directive, TWinControl.Handle has not. The thing is that for some widgetsets the handle of the application is simply 0 (e.g. the custom drawn one for Android) while a TWinControl always has a valid handle, cause there the handle is part of its core functionality.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Symbol - Handle - is not portable
« Reply #4 on: February 19, 2019, 09:42:31 am »
Is there any other method to get the handle of Application. Without using Application.Handle. Or is there any work around for this warning.

No, and if there was a way, that would be unportable too, so should get the same warning.

If you are not interested in portability warnings, filter them out (right click in messages and configure it), or use relevant -vw parameters to disable specific warnings.

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Re: Symbol - Handle - is not portable
« Reply #5 on: February 19, 2019, 10:08:44 am »


No, and if there was a way, that would be unportable too, so should get the same warning.

If you are not interested in portability warnings, filter them out (right click in messages and configure it), or use relevant -vw parameters to disable specific warnings.

If i am not getting it wrong then, here unportable means that it cannot be ported on other os's. But will work nicely on Windows.

My application is windows only. Should i consider this warning as serious. Or just mute it and continue my work further.

Are there any consequences if i consider to disable this warning using -vw parameter.

Operating System targeted is only Windows
« Last Edit: February 19, 2019, 10:13:10 am by Deepaak »
Holiday season is online now. :-)

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Re: Symbol - Handle - is not portable
« Reply #6 on: February 19, 2019, 10:17:18 am »
No: on Windows it is a real WINAPI handle. And those are of course not portable. E.g. GetWindowLongPtr and such can not be used on other platforms and the format / type of the handle may differ (is different) on other platforms.  In this case it is the use of the handle that triggers the warning. If you would use Tform.handle in the same manner, you will get the same warning. Under windows all handles are system.THandle which is on windows a windows specific handle.

Attaching screenshot. Form1.Handle and Application.Handle results are different.
Holiday season is online now. :-)

 

TinyPortal © 2005-2018