2014-09-03 2 views
0

J'ai besoin d'envoyer des données de DLL à App, où "données" est un tableau de variante.Delphi: Comment envoyer un tableau de Variant de DLL à Application en utilisant Windows API "SendMessage"?

J'ai essayé d'utiliser SendMessage/WM_COPYDATA, sans aucune chance!

C'est ce que je reçois jusqu'à présent:

// 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

'WM_COPYDATA' envoie un seul bloc de données contiguës. Un tableau Delphi est peu susceptible d'être structuré comme ça. –

+0

Sérialiser le tableau et utiliser WM_COPYDATA. Ou utilisez COM pour une vie facile. –

Répondre

1

Tant que l'application et DLL utilisent la même version de RTL, et vous passez le Variant à l'intérieur du même processus, vous pouvez passer comme un pointeur, par exemple:

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; 

Dans ce cas, vous pouvez déposer WM_COPYDATA et il suffit d'utiliser une fenêtre personnalisée message au lieu:

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; 
+0

Super! Il fonctionne comme un charme! Merci! – Christian

Questions connexes