Delphi/C++Builder - TDdeClientConv の SetLink が失敗
Delphi/C++Builder - TDdeClientConv の SetLink が失敗
SetLink(ServiceName, TopicName) の第二引数に通常の変数を渡すと、メソッドが失敗する
↓失敗する関数
SetLink(ServiceName, TopicName) の第二引数に通常の変数を渡すと、メソッドが失敗する
↓失敗する関数
function PostDDE_NG(const AppName, DdeService, DdeTopic, SendMacro: string): Boolean; begin with TDdeClientConv.Create(nil) do begin try ServiceApplication := AppName; ConnectMode := ddeAutomatic; if SetLink(DdeService, DdeTopic) then begin // ←失敗 try Result := ExecuteMacro(SendMacro, True); finally CloseLink; end; end; finally Free; end; end; end;Topic を 'hogehoge' みたいに決め打ち指定するか const AAA = 'hogehoge'; したものを入れると関数は成功。 それじゃ、関数にした意味が無いので、PChar を介して指定すると、変数でもいけます
function PostDDE(const AppName, DdeService, DdeTopic, SendMacro: string): Boolean;
var pDdeTopic: PChar;
begin
pDdeTopic := StrAlloc(Length(DdeTopic) + 1);
StrCopy(pDdeTopic, PChar(DdeTopic));
try
with TDdeClientConv.Create(nil) do begin
try
ServiceApplication := AppName;
ConnectMode := ddeAutomatic;
if SetLink(DdeService, StrPas(pDdeTopic)) then begin
try
Result := ExecuteMacro(SendMacro, True);
finally
CloseLink;
end;
end;
finally
Free;
end;
end;
finally
StrDispose(pDdeTopic);
end;
end;
関数の使い方
PostDDE('c:\TestApp.exe' { 送信先のアプリケーションパス },
'TestApp' {一般的に実行ファイルの.exe を除いたもの },
'TopicName' {サーバ側 TDdeServerConv の Nameプロパティ},
'Macro Message');
受け側アプリは TDdeServerConv コンポーネントを置いて、Nameプロパティを 'TopicName' の部分と同じに合わせれば、OnExecuteMacro イベントへ、'Macro Message' が受信される
コメント