bryan7
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);
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.
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;
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;
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
thanks
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.
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.
ASKER
Typo: "you'll see". How come there is no edit function ;(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
I doubled the points instead so you got more points than you would otherwise with the original points*A