我需要将数据从DLL发送到App,其中" data"是一个变种数组。
我尝试使用SendMessage / WM_COPYDATA,没有运气!
这是我到目前为止所得到的:
// sender's side (dll)
procedure sendData(apphandle: THandle);
var V: Variant;
begin
V = VarArrayOf([1,'Some Text', 123.45, true]);
copyDataStruct.dwData := 100;
copyDataStruct.cbData := ?; <-- what here?
copyDataStruct.lpData := ?; <-- and here? how to put "V"?
SendMessage(apphandle, WM_COPYDATA, Integer(apphandle), Integer(@copyDataStruct)) ;
end;
// receiver's side (same app where dll was called)
procedure WMCopyData(var Msg: TWMCopyData) ;
var V : Variant;
begin
if (Msg.CopyDataStruct.dwData = 100) then
begin
V := Msg.CopyDataStruct.lpData; <-- how to get "V" from lpData?
// do some work with "V" here...
end;
end;
答案 0 :(得分:1)
只要app和DLL使用相同版本的RTL,并且您在同一进程内传递Variant
,就可以将其作为指针传递,例如:
procedure sendData(AppHandle: HWND);
var
V: Variant;
begin
V := VarArrayOf([1,'Some Text', 123.45, true]);
copyDataStruct.dwData := 100;
copyDataStruct.cbData := SizeOf(Pointer);
copyDataStruct.lpData := @V;
SendMessage(AppHandle, WM_COPYDATA, WPARAM(AppHandle), LPARAM(@copyDataStruct));
end;
procedure WMCopyData(var Msg: TWMCopyData);
var
V : PVariant;
begin
if (Msg.CopyDataStruct.dwData = 100) then
begin
V := PVariant(Msg.CopyDataStruct.lpData);
// use V^ as needed...
end;
end;
在这种情况下,您可以删除WM_COPYDATA
,只需使用自定义窗口消息:
const
WM_MYMSG = WM_APP + 1;
procedure sendData(AppHandle: HWND);
var
V: Variant;
begin
V := VarArrayOf([1,'Some Text', 123.45, true]);
SendMessage(AppHandle, WM_MYMSG, 0, LPARAM(@V));
end;
procedure WMMyMsg(var Msg: TMessage);
var
V : PVariant;
begin
V := PVariant(Msg.LParam);
// use V^ as needed...
end;