Forum > Other

getting handle of my lazarus app

<< < (2/3) > >>

OnixJr:
daz: post your code...

daz:
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: ---
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.

--- End code ---

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:

--- 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.
--- End quote ---


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:
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: ---
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.

--- End code ---

gives an access violation.

Marc:

--- 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.

--- End quote ---

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.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version