2010-09-20 30 views

回答

2

Warren是解碼郵件的關鍵,取決於您如何在發件人和收件人應用程序中定義您的COPYDATASTRUCT記錄並使用GetLParam函數。

檢查這個代碼

//the sender application, this is a simple console app 
namespace ConsoleApplication_PrismSendData; 

interface 
uses 
    System.Runtime.InteropServices; 
type 
    [StructLayout(LayoutKind.Sequential)] 
    CopyDataStruct = public record //the record to be passed by the WM_COPYDATA message. 
    public 
    var  dwData: IntPtr; 
    var  cbData: System.Int32; 
    var  [MarshalAs(UnmanagedType.LPStr)] //in this case using a pointer to a null-terminated array of ANSI characters. 
    lpData: System.String; 
    end; 

    ConsoleApp = class 
    public 
    const  WM_COPYDATA: System.Int32 = $4a;//WM_COPYDATA 0x004A 
    class method Main(args: array of string); 
    [DllImport('user32.dll', EntryPoint := 'FindWindow')] 
    class method FindWindow(lpClasName: System.String; lpWindowName: System.String): System.IntPtr; external; 
    [DllImport('user32.dll', EntryPoint := 'SendMessage')] 
    class method SendMessage(hWnd: System.IntPtr; Msg: System.Int32; wParam: System.Int32; var lParam: CopyDataStruct): System.Int32; external; 
    end; 


implementation 

class method ConsoleApp.Main(args: array of string); 
var 
cd : CopyDataStruct; 
WHnd : IntPtr; 
begin 
cd.dwData := IntPtr(0);//Dummy 
cd.lpData := 'Hello from Delphi Prism - Console Application';//message to send 
cd.cbData := cd.lpData.Length;//set the length of the message 
WHnd  := FindWindow(nil, 'PrismForm');//find the handle for the window of the receiver app 
if (WHnd<>System.IntPtr.Zero) then //check if <> to 0 
begin 
    SendMessage(WHnd, WM_COPYDATA, 0, var cd); //Send the message to the app 
    Console.WriteLine('Message sent'); 
end 
else 
Console.WriteLine('Window not found'); 
Console.ReadKey(); 
end; 

end. 

現在的接收器應用程序的代碼。

首先你必須重寫WndProc方法,要做到這一點,你只是寫這樣的事情。

MainForm = partial class(System.Windows.Forms.Form) //this is your form 
    private 
    const  WM_COPYDATA: System.Int32 = $4a; //declare the const for WM_COPYDATA message 
    protected 
    method Dispose(disposing: Boolean); override; 
    method WndProc(var m: Message); override;//override WndProc method to process the messages 
    public 
    constructor; 
    end; 

然後聲明發件人應用的相同的結構來處理消息

[StructLayout(LayoutKind.Sequential)] 
    CopyDataStruct = public record 
    public 
    var  dwData: IntPtr; 
    var  cbData: System.Int32; 
    var  [MarshalAs(UnmanagedType.LPStr)] 
    lpData: System.String; 
    end; 

終於實現WndProc方法這樣

method MainForm.WndProc(var m: Message); 
var 
cd: CopyDataStruct; 
s : System.String; 
begin 
    case m.Msg of 
    WM_COPYDATA: 
       begin 
        //the GetLParam function convert the LParam value an object. 
        //the typeof function obtains the type of an object 
        cd:= m.GetLParam(typeof(CopyDataStruct)) as CopyDataStruct; 
        s := cd.lpData.Substring(0,cd.cbData); 
        // do your stuff here 
       end; 
    end; 
    inherited WndProc(var m); 
end; 
+0

這工作完全感謝,但我似乎如果lpData變量不是字符串,則會遇到問題。任何想法,我可以做到這一點與lpData變量,例如,一個整數數組?我沒有成功嘗試編碼爲LPArray ByValArray和SafeArray。 – 2010-09-27 10:45:55