VC ++在两个应用程序之间发送消息 [英] VC++ sending message between two application
问题描述
2个小时以来,我一直在搜索从Microsoft Visual C ++向在Delphi中创建的另一个应用程序发送消息.
I have been searching to send a message from Microsoft Visual C++ to another application created in Delphi for 2 hours.
在delphi中,我知道如何读取数据.但是我不知道如何在MVC ++中发送消息
In delphi I know how to read the data. But I don't know exactly how to send a message in MVC++
希望您能给我一个密码.
I hope you can get me a code.
因此,对于下一个代码,我想要在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.
使用此代码,我应该读取数据.我也想兼容.
And with this code I should read the data. Also I want to be compatible.
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中创建的应用程序发送消息.
So my application contains two parts: One in Microsoft Visual Studio, i use opencv and i want to send a message to the second application, which is created in Delphi.
推荐答案
您可以使用WM_GETTEXT
或WM_COPYDATA
消息在应用程序之间来回发送数据缓冲区.我曾经寻找一种发送缓冲区的方法,如WM_GETTEXT
那样,只是带有不同的消息.原始代码可以在这里找到:
You can use the WM_GETTEXT
or WM_COPYDATA
message to send buffers of data back and forth between applications. I once searched for a way to send a buffer like WM_GETTEXT
does, only with a different message. The original code can be found here:
http://www.nldelphi.com/forum/showthread. php?p = 275167#post275167
我不知道一切是否仍然有效(从那以后就没有使用过),但是那时候确实如此.
I don't know if everything still works (haven't used it since), but it did back then.
// 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
It's probably just as easy to use WM_COPYDATA
, though. :D
这篇关于VC ++在两个应用程序之间发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!