Link to home
Start Free TrialLog in
Avatar of bryan7
bryan7Flag for Japan

asked on

DDE.SetLink fails if running in a new thread

I'm trying to use DDE.SetLink within a new thread, but the call fails right away, while it works normally if I don't do it threaded. What is wrong?


function mIRCinfo(P: Pointer) : LongInt; stdcall;
var DDE: TDDEClientConv;
begin
  try
    DDE := TDDEClientConv.Create(nil);
    if DDE.SetLink(mIRCSet.Service, mIRCSet.Topic) then
    begin
     DDE.OpenLink;
     DDE.PokeData(mIRCSet.Topic, PChar(mIRCSet.Command));
     DDE.CloseLink;
    end
    else
      showmessage('fail');
  finally
    DDE.Free;
  end;
end;
 
 
 
Var    thr : THandle;
          thrID : DWORD;
 
   thr:= CreateThread(nil, 0, @mIRCinfo, nil, 0, thrID);

Open in new window

Avatar of 2266180
2266180
Flag of United States of America image

you need to call coinitialize and cofinalize when the thread starts, respectivly ends, from the created thread. in your case, use another try finally which will incorporate the 2 calls.
Avatar of bryan7

ASKER

it still fails
function mIRCInfoThreaded(P : Pointer): LongInt; stdcall;
var DDE: TDDEClientConv;
begin
 try
   CoInitialize(nil);
  try
    DDE := TDDEClientConv.Create(nil);
    if DDE.SetLink(mIRCIntoSet.Service, mIRCIntoSet.Topic) then
    begin
     DDE.OpenLink;
//     DDE.PokeData(mIRCIntoSet.Topic, PChar(mIRCIntoSet.Command));
     DDE.CloseLink;
    end
    else showmessage('Failed to stablish link with DDE');
  finally
    DDE.Free;
  end;
 finally
   CoUninitialize;
 end;
end;

Open in new window

what about:
uses ..., ActiveX;
 
....
 
function mIRCInfoThreaded(P : Pointer): LongInt; stdcall;
var DDE: TDDEClientConv;
begin
 try
   CoInitializeEx(nil, 0);
  try
    DDE := TDDEClientConv.Create(nil);
    if DDE.SetLink(mIRCIntoSet.Service, mIRCIntoSet.Topic) then
    begin
     DDE.OpenLink;
//     DDE.PokeData(mIRCIntoSet.Topic, PChar(mIRCIntoSet.Command));
     DDE.CloseLink;
    end
    else showmessage('Failed to stablish link with DDE');
  finally
    DDE.Free;
  end;
 finally
   CoUninitialize;
 end;
end;

Open in new window

Avatar of bryan7

ASKER

Same result, SetLink fails.
can you post a zip with a small demo which has this behaviour so I have something to work with?
thanks
Avatar of bryan7

ASKER

Open a new project, and simple paste the 2 functions and the code on form create:

You'll have to enable DDE in mirc, in Options, Other, DDE, "Enable DDE Server"

Then oncreate, you'll the non-threaded one works, and the other one fails.
unit Unit5;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ActiveX, ddeman;
 
type
  TForm5 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form5: TForm5;
 
implementation
 
{$R *.dfm}
 
function mIRCInfoThreaded(P : Pointer): LongInt; stdcall;
var DDE: TDDEClientConv;
begin
 try
   CoInitializeEx(nil, 0);
  try
    DDE := TDDEClientConv.Create(nil);
    if DDE.SetLink('mIRC', 'COMMAND') then
    begin
     DDE.OpenLink;
     DDE.CloseLink;
     showmessage('thread: this works');
    end
    else showmessage('thread: Failed to stablish link with DDE');
  finally
    DDE.Free;
  end;
 finally
   CoUninitialize;
 end;
end;
 
procedure mIRCInfoNotThreaded;
var DDE: TDDEClientConv;
begin
  try
    DDE := TDDEClientConv.Create(nil);
    if DDE.SetLink('mIRC', 'COMMAND') then
    begin
     DDE.OpenLink;
     DDE.CloseLink;
     showmessage('no thread: this works');
    end
    else showmessage('no thread: Failed to stablish link with DDE');
  finally
    DDE.Free;
  end;
end;
 
procedure TForm5.FormCreate(Sender: TObject);
Var thr : THandle;
    thrID : DWORD;
begin
  thr:= CreateThread(nil, 0, @mIRCinfoThreaded, nil, 0, thrID);
  mIRCInfoNotThreaded;
end;
 
end.

Open in new window

Avatar of bryan7

ASKER

Typo: "you'll see". How come there is no edit function ;(
ASKER CERTIFIED SOLUTION
Avatar of 2266180
2266180
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bryan7

ASKER

I see. Guess I'll just leave it be then and use the old way. Thanks!
B grade? I solve your problem correctly, give you alternative solutions, and you give me a B grade? Guess you didn't read the FAQ and help section not to mention the grading tips.

no problem, you just made it on my blacklist which basically means that I will no longer answer any of your questions.

no need to anwser as I've unsubscribed.

enjoy.
Avatar of bryan7

ASKER

I doubled the points instead so you got more points than you would otherwise with the original points*A