2012-04-12 72 views
0

我一直在尋找從Microsoft Visual C++發送消息到在Delphi中創建的另一個應用程序2個小時。VC++發送消息在兩個應用程序之間

在delphi中,我知道如何讀取數據。但我不知道如何發送一條消息在MVC++

我希望你能給我一個代碼。

因此,對於下一個代碼,我想在Microsoft Visual Studio C++ 2010中進行翻譯,我的項目是一個控制檯項目。

const 
    MY_MESSAGE = WM_USER + 4242; 

type 
    TForm1 = class(TForm) 
    Button1: TButton; 

procedure Button1Click(Sender: TObject); 

    end; 
var 
    Form1: TForm1; 
implementation 
{$R *.DFM} 

procedure TForm1.Button1Click(Sender: TObject); 
var 
    txt: string; 
begin 
    txt := 'Hello World'; 
    SendMessage(Form1.Handle, MY_MESSAGE, 0, DWORD(PChar(txt))); 
end; 


end. 

並且用這段代碼我應該讀取數據。我也想兼容。

const 
    MY_MESSAGE = WM_USER + 4242; 

type 
    TForm1 = class(TForm) 

    // Handler that receive the Message 

procedure MessageReceiver(var msg: TMessage); message MY_MESSAGE; 
    end; 
var 
    Form1: TForm1; 
implementation 
{$R *.DFM} 


procedure TForm1.MessageReceiver(var msg: TMessage); 
var 
    txt: PChar; 
begin 
    txt := PChar(msg.lParam); 
    msg.Result := 1; 
    ShowMessage(txt); 
end; 
end. 

所以我的應用程序包括兩個部分:一是在Microsoft Visual Studio,我使用OpenCV的,我想將消息發送到第二個應用程序,這是在Delphi中創建。

+1

您的意思是使用SendMessage發送消息? – GolezTrol 2012-04-12 15:08:34

+0

是的,使用Windows管道 – user558126 2012-04-12 15:10:17

+1

我不確定,但我認爲它是一個或另一個。既然你知道如何閱讀德爾福,你可以發佈該代碼,所以有人知道你需要什麼樣的寫作對應?另外,因爲你需要VC++代碼,所以這實際上不是Delphi問題。 – GolezTrol 2012-04-12 15:14:17

回答

1

我不知道如何使用管道,但我下面的方案之前已經使用:使用SendMessage()

使用WM_COPYDATA消息。這裏是一個參考

http://msdn.microsoft.com/en-us/library/windows/desktop/ms649011(v=vs.85).aspx

和例子

http://msdn.microsoft.com/en-us/library/windows/desktop/ms649009(v=vs.85).aspx

您將需要使用FindWindow得到一個句柄,你想將消息發送到應用程序。

+0

我不知道如何使用該代碼。我不是MVC++中的程序員謝謝,我重寫了我的問題,以便更容易理解 – user558126 2012-04-12 15:36:32

1

您可以使用WM_GETTEXTWM_COPYDATA消息在應用程序之間來回發送數據緩衝區。我曾經搜索過一種方式來發送像WM_GETTEXT這樣的緩衝區,只能用不同的消息。原代碼可以在這裏找到:

http://www.nldelphi.com/forum/showthread.php?p=275167#post275167

我不知道,如果一切仍然有效(沒有因爲使用它),但它確實當時的情況。

// The order (first Buffer, then BufferLength) seems more sensible, although with 
// WM_SETTEXT they are actually the other way around. 
function SendTextMessage(Handle: THandle; Msg: Integer; Buffer: Pointer; BufferLength: Integer): Cardinal; 
var 
    ProcessHandle: THandle; 
    ProcessId: Cardinal; 
    VirtualBuffer: Pointer; 
begin 
    // Get the id of process to which the handle belongs. 
    GetWindowThreadProcessID(Handle, @ProcessId); 
    ProcessHandle := OpenProcess(PROCESS_ALL_ACCESS, False, ProcessId); 

    if ProcessHandle = 0 then 
    RaiseLastWin32Error; 

    // Allocate a virtual buffer in the process 
    VirtualBuffer := VirtualAllocEx(ProcessHandle, nil, BufferLength, 
          MEM_COMMIT, PAGE_READWRITE); 
    if VirtualBuffer = nil then 
    RaiseLastWin32Error; 

    try 
    // Send a message to the handle, passing the virtual pointer as a buffer 
    Result := SendMessage(Handle, Msg, BufferLength, Integer(VirtualBuffer)); 

    // Read the resulting value from the virtual buffer into the given buffer 
    if not ReadProcessMemory(ProcessHandle, VirtualBuffer, Buffer, Result, Result) then 
     RaiseLastWin32Error; 

    finally 
    VirtualFreeEx(ProcessHandle, VirtualBuffer, BufferLength, MEM_RELEASE); 
    end; 

end; 

,並調用它是這樣的:

var 
    h: THandle; 
    b: array[0..1024] of Char; 
begin 
    h := Cardinal(StrToInt(Edit1.Text)); 
    // Not like this 
    //SendMessage(h, WM_GETTEXT, 1024, Integer(@b)); 

    // But like this 
    SendTextMessage(h, WM_USER+1, @b, 1024 * SizeOf(Char)); 
    ShowMessage(b); 

閱讀信息是這樣的:

procedure WM_USERPLUS1(var Msg: TWMGetText); message WM_USER+1; 


procedure TForm2.WM_USERPLUS1(var Msg: TWMGetText); 
begin 
    with Msg do 
    Result := StrLen(StrLCopy(PChar(Text), PChar('Hallo wereld'), TextMax - 1)) * SizeOf(Char); 
end; 

這可能只是因爲使用方便WM_COPYDATA,雖然。 :D

+0

謝謝,我重寫了我的問題,以便更容易理解 – user558126 2012-04-12 15:35:47

+2

爲什麼要這樣做而不是使用'WM_COPYDATA'作爲大自然的目的?! – 2012-04-12 15:52:43

+0

就像我說過的,使用WM_COPYDATA可能同樣簡單,但另一方面這是一個有趣的練習,它允許您使用任何想要的消息。隨意不要使用它。 :) – GolezTrol 2012-04-12 16:28:30

相關問題