2012-04-12 2 views
0

Microsoft Visual C++에서 Delphi에서 만든 다른 응용 프로그램으로 2 시간 동안 메시지를 보내고 자했습니다.두 응용 프로그램간에 VC++ 메시지 전송

델파이에서는 데이터를 읽는 방법을 알고 있습니다. 하지만 정확히 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. 

그리고이 코드를 통해 데이터를 읽어야합니다. 또한 나는 호환되기를 원합니다. 내가 OpenCV의를 사용하고 난 델파이에서 만든 두 번째 응용 프로그램에 메시지를 보내려면, 마이크로 소프트 비주얼 스튜디오의 한 :

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. 

그래서 내 응용 프로그램은 두 부분으로 포함되어 있습니다.

+1

당신은 SendMessage를 사용하여 메시지를 보내는 것을 의미합니까? – GolezTrol

+0

네, windows 파이프 라인을 사용합니다 – user558126

+1

확실하지 않습니다. 그러나 나는 그 중 하나라고 생각합니다. 델파이를 읽는 방법을 알고 있기 때문에 코드를 게시 할 수 있습니까? 그렇다면 어떤 종류의 글을 써야 알 수 있습니까? 또한 VC++ 코드가 필요하기 때문에 실제로는 델파이 문제가 아닙니다. – GolezTrol

답변

1

나는 파이프 라인을 사용하는 방법을 모르겠지만, 나는 다음과 같은 계획하기 전에 사용하고 있습니다 :

사용 WM_COPYDATA 메시지가 SendMessage()를 사용하여. 여기에 참조

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

하고 메시지를 보낼 응용 프로그램에 대한 핸들을 얻을 수 FindWindow를 사용하는 당신이 필요합니다 예를

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

에게 있습니다.

+0

그 코드를 어떻게 사용하는지 모르겠습니다. 나는 MVC++에서 프로그래머가 아니다. 내 질문을 다시 써서 이해하기 쉬워졌다. – user558126

1

WM_GETTEXT 또는 WM_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

+2

왜이 세상에서 'WM_COPYDATA' 자연 의도로?! –

+0

제가 말했듯이, WM_COPYDATA를 사용하는 것이 쉽지만, 다른 한편으로는 재미있는 연습이며 원하는 메시지를 사용할 수 있습니다. 사용하지 않는 것이 좋습니다. :) – GolezTrol

관련 문제