Recent

Author Topic: getting handle of my lazarus app  (Read 14395 times)

daz

  • Full Member
  • ***
  • Posts: 112
    • http://matt-shaffer.com
getting handle of my lazarus app
« on: December 27, 2007, 07:23:19 pm »
I thought Handle was the built in var for it but when I use SetWindowPos with it it gives me error code 1400: handle not found... :/

Oh and when I try to set a different app with SetWindowPos it gives me an access violation.

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2519
RE: getting handle of my lazarus app
« Reply #1 on: December 28, 2007, 11:10:52 am »
you can oly use handles withing laz applications. Handle is not a var, but a property of a window. So which handle wasn't found ?
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

daz

  • Full Member
  • ***
  • Posts: 112
    • http://matt-shaffer.com
RE: getting handle of my lazarus app
« Reply #2 on: December 28, 2007, 06:49:45 pm »
I tried passing Handle to the function SetWindowPos and it said that the handle wasn't found... Well... maybe I defined the function wrong? Should I use

function SetWindowPos(hand,i1,i2,i3,i4,i5,i6:integer):integer; external 'USER32';
or
function SetWindowPos(hand:thandle; i1,i2,i3,i4,i5,i6:integer):integer; external 'USER32';

or something else entirely?

edit: Meh, both of those return the error

Phil

  • Hero Member
  • *****
  • Posts: 2750
Re: RE: getting handle of my lazarus app
« Reply #3 on: December 28, 2007, 07:35:13 pm »
Quote from: "daz"
or something else entirely?

edit: Meh, both of those return the error


Pass in the control's Handle property. For example, Edit1.Handle.

For documetation on SetWindowPos, see the Microsoft Win32 specs.

Note that SetWindowPos may not be implemented on all widgetsets yet.

Thanks.

-Phil

daz

  • Full Member
  • ***
  • Posts: 112
    • http://matt-shaffer.com
RE: Re: RE: getting handle of my lazarus app
« Reply #4 on: December 28, 2007, 08:23:09 pm »
I tried Form1.Handle which still returned the error.  How could SetWindowPos not be implemented? I mean, it's returning something at least and it's part of the win api not lazarus..

OnixJr

  • Full Member
  • ***
  • Posts: 172
    • http://www.brlazarus.kit.net
RE: Re: RE: getting handle of my lazarus app
« Reply #5 on: January 02, 2008, 02:52:30 am »
daz: post your code...
Portal Lazarus Brasil - http://lazaruspascal.codigolivre.org.br/portal.php
Lazarus BOOK (in portuguese) - http://lazarus-book.blogspot.com
<hipernetjr@yahoo.com.br> - Curitiba/Brazil

daz

  • Full Member
  • ***
  • Posts: 112
    • http://matt-shaffer.com
RE: Re: RE: getting handle of my lazarus app
« Reply #6 on: January 02, 2008, 07:25:20 pm »
Well one thing I figured out: winopen works fine in dev-pascal but not so much in lazarus - so there's the first problem...
Code: [Select]

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Timer1: TTimer;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation
function SetWindowPos(hand,i1,i2,i3,i4,i5,i6:integer):integer; external 'USER32';
function GetLastError():integer; external 'USER32';
function FindWindowA(leaveBlank: pchar; winTitle: pchar):Integer; external 'USER32';

{ TForm1 }
function winopen(theTitle:pchar):Real; stdcall;
var poshand:integer;
begin
 poshand:=FindWindowA(nil,theTitle);
 if(poshand>0) then
 begin
      Result:=poshand;
 end
 else
 begin
     Result:=0;
 end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin

end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  //if(winopen('project1')>0) then begin showmessage('Open'); end
  //else begin showmessage('Grrr lazarus! You screwed up again!'); end;
  if(SetWindowPos(Form1.Handle,-1,0,0,0,0,strtoint(edit1.text))>0) then begin
           showmessage('Worked?');
  end else
  begin
       label2.caption:=inttostr(getlasterror());
  end;
end;

initialization
  {$I unit1.lrs}

end.

Again, I've tried many different things such as changing how setwindowpos is defined (making hand:thandle) etc.. type in a number in the box and that'll be the last value sent to setwindowpos - I made it to see if changing some stuff would make it work.

Phil

  • Hero Member
  • *****
  • Posts: 2750
Re: RE: Re: RE: getting handle of my lazarus app
« Reply #7 on: January 02, 2008, 08:38:44 pm »
Quote from: "daz"
Again, I've tried many different things such as changing how setwindowpos is defined (making hand:thandle) etc.. type in a number in the box and that'll be the last value sent to setwindowpos - I made it to see if changing some stuff would make it work.


This works fine here with both Delphi and FPC:

program test1;

{$IFDEF FPC}
 {$MODE DELPHI}
{$ENDIF}

uses Windows;

var
  TitleStr : string;
begin

  TitleStr := 'Microsoft Excel';
 
  if FindWindow(nil, PChar(TitleStr)) <> 0 then
    WriteLn(TitleStr, ' is running')
  else
    WriteLn(TitleStr, ' not running');
 
end.


Try commenting out your external declarations and add Windows to uses.

Don't use Real. The result is HWND.

Note that the title has to match exactly I believe, so if your app has a document open that's part of the title you need to include it.

Thanks.

-Phil

daz

  • Full Member
  • ***
  • Posts: 112
    • http://matt-shaffer.com
RE: Re: RE: Re: RE: getting handle of my lazarus app
« Reply #8 on: January 02, 2008, 09:40:47 pm »
Can I replace HWND with Thandle? Cause lazarus doesn't have HWND..Making it say 'uses windows' and commenting out my declarations gives an error. Changing the code to this:
Code: [Select]

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Timer1: TTimer;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation
function SetWindowPos(hand:thandle; i1,i2,i3,i4,i5,i6:integer):integer; external 'USER32';
function GetLastError():integer; external 'USER32';
function FindWindowA(leaveBlank: pchar; winTitle: pchar):Integer; external 'USER32';

{ TForm1 }
function winopen(theTitle:pchar):thandle;
var poshand:thandle;
begin
 poshand:=FindWindowA(nil,theTitle);
 if(poshand<>0) then
 begin
      Result:=poshand;
 end
 else
 begin
     Result:=0;
 end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin

end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  //if(winopen('project1')>0) then begin showmessage('Open'); end
  //else begin showmessage('Grrr lazarus! You screwed up again!'); end;
  if(SetWindowPos(winopen('project1'),-1,0,0,0,0,strtoint(edit1.text))>0) then begin
           showmessage('Worked?');
  end else
  begin
       label2.caption:=inttostr(getlasterror());
  end;
end;

initialization
  {$I unit1.lrs}

end.

gives an access violation.

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2519
Re: RE: Re: RE: Re: RE: getting handle of my lazarus app
« Reply #9 on: January 03, 2008, 10:48:40 am »
Quote from: "daz"
Can I replace HWND with Thandle? Cause lazarus doesn't have HWND..Making it say 'uses windows' and commenting out my declarations gives an error.

add windows as first to your uses clause of the form unit. You don't need your own declarations. If you add lcltype too then you have HWND.
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

duncanparsons

  • Jr. Member
  • **
  • Posts: 83
RE: Re: RE: Re: RE: Re: RE: getting handle of my lazarus app
« Reply #10 on: January 03, 2008, 11:51:19 am »
SetBounds might be your way forward?

DSP

daz

  • Full Member
  • ***
  • Posts: 112
    • http://matt-shaffer.com
RE: Re: RE: Re: RE: Re: RE: getting handle of my lazarus app
« Reply #11 on: January 04, 2008, 01:46:14 am »
OK I have windows and lcltype as my first two 'uses' statements and the only error I get is me not understanding how setwindowpos works apparently.. but the error is.. "Got longbool, expected longint" Now what exactly is the 'longbool' in this: if(SetWindowPos(winopen('project1'),-1,0,0,0,0,strtoint(edit1.text))>0)
I changed all the thandles I made earlier to HWNDs and it doesn't complain about this :) Thanks so far you guys, I'll probably end up having to google this problem anyway as it's not so much a lazarus problem anymore (but still dunno what a longbool is o_O)

Phil

  • Hero Member
  • *****
  • Posts: 2750
Re: RE: Re: RE: Re: RE: Re: RE: getting handle of my lazarus
« Reply #12 on: January 04, 2008, 04:26:02 am »
Quote from: "daz"

if(SetWindowPos(winopen('project1'),-1,0,0,0,0,strtoint(edit1.text))>0)


The function result of SetWindowPos is BOOL to indicate success or failure. You're comparing it against an integer.

Do you have a WinAPI reference? That would probably help. You can also look at the source of the FPC Windows unit to see how these API functions are declared.

Thanks.

-Phil

daz

  • Full Member
  • ***
  • Posts: 112
    • http://matt-shaffer.com
RE: Re: RE: Re: RE: Re: RE: Re: RE: getting handle of my laz
« Reply #13 on: January 04, 2008, 04:35:46 am »
Well stupid msdn doesn't help much xD And wow, can't belive I never thought of looking through that source. Thanks.

OMG! :D :D It worked.. after all this time.

random question: I have damn small linux on my laptop, do you think lazarus would work on it? Does it require the gtk or anything? (dsl doesn't come with gtk by default, and in general isn't the best choice for linux.. but the machine is bad: 233mhz, 98mb ram.)

 

TinyPortal © 2005-2018