Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help multithreading in dll

Status
Not open for further replies.

cascot

Programmer
Jan 30, 2002
127
CA
I'm very much a Delphi beginner, so please feel free to tear my poor 'newbie' coding to pieces (good or bad, it will all help).

I am trying to write code to close Outlook security dialog boxes. The ultimate intention being to create a dll which I can then call from Microsoft Access using VBA.

Thus far I have created a dll of sorts, which (probably in a very crude way) closes the dialog boxes. I have also created a little Delphi program with two buttons that calls a procedure within that dll. This proves that (at least on the surface) the code within the dll, as well as the dll itself, appears to work. My problem is that before I can call the dll based procedure from my Access application it will have to be altered to initiate a seperate thread for the procedure, otherwise the procedure completes before the next line within the access app executes (which causes the Oulook boxes to appear). This is where I have thus far failed.

Can anyone point me in the right direction?

Here is the dll code...

Code:
library KillOLdlg;

uses
  Windows, Messages, SysUtils, Classes;

var
  hTemp, hTemp2 : HWND;
  TheText : String;
  TextLen : Integer;
  DlgNbr : Integer;

Const
  btnText = 'Yes';

{$R *.res}

Procedure CloseDlg(DlgStage : Integer); StdCall;
var
  prevhwnd : Hwnd;
  dlgCheckCount : Integer;
begin
  DlgNbr := DlgStage;
  {Get handle of foreground window}
  prevhwnd := GetForegroundWindow();
  dlgCheckCount := 0;
  hTemp := 0;
  {Check for Outlook dialog for up to 5 seconds}
  while (hTemp = 0) and (dlgCheckCount < 100) do
  begin
    Sleep(50);
    hTemp := FindWindow('#32770',nil);
    TextLen := GetWindowTextLength(hTemp);
    SetLength(TheText,TextLen);
    GetWindowText(hTemp, PChar(TheText), Length(TheText)+1);
    TheText := String(PChar(TheText));
    if TheText <> 'Microsoft Office Outlook' then
      hTemp := 0;
    Inc(dlgCheckCount);
  end;
  if hTemp <> 0 then
  begin
    if DlgStage = 2 then
      sleep (5500); // Wait 5.5 seconds
    SetFocus(hTemp);
    hTemp2 := FindWindowEx(hTemp,0,'Button',PChar(btnText));
    SendMessage(hTemp2, BM_CLICK, 5, 5);
    {Set focus back to original foreground window}
    SetForegroundWindow(prevhwnd);
  end;
end;

exports CloseDlg;
begin
end.

And just for completeness, the little program I am currently using to call the procedure...

Code:
unit OLdlg;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

procedure CloseDlg(DlgStage: Integer);stdcall; external 'iwsKillOLdlg2.dll'

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  CloseDlg(1);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  CloseDlg(2);
end;

end.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top