delphi调用VC动态库,动态库向窗体发送自定义消息。请问怎么接收消息,并显示接收到的字符
procedure TTestVcDLLForm.WMBarIcon(var Message:TMessage);
var
b:byte;
buffer:pchar;
begin
GetMem(Buffer,86*2+1); StrCopy(Buffer,Pchar(Message.LParam));
edit1.Text:=inttostr(Message.LParam);
edit2.Text:=Buffer;
FreeMem(Buffer);
//b:=strtoint(Pchar(Message.LParam));
//edit2.Text:=inttostr(b);
//edit2.Text:=inttostr(Message.WParam);showmessage('ok');
end;
procedure TTestVcDLLForm.WMBarIcon(var Message:TMessage);
var
b:byte;
buffer:pchar;
begin
GetMem(Buffer,86*2+1); StrCopy(Buffer,Pchar(Message.LParam));
edit1.Text:=inttostr(Message.LParam);
edit2.Text:=Buffer;
FreeMem(Buffer);
//b:=strtoint(Pchar(Message.LParam));
//edit2.Text:=inttostr(b);
//edit2.Text:=inttostr(Message.WParam);showmessage('ok');
end;
begin
edit1.Text:=PChar(Pointer(Message.LParam));
end;
WParam:1 //长度
要看它指向的内存,那才是你的字符所在要贴出vc代码看看
PProject Project1.exe raised exception class EInvalidPointer with message'Invalid pointer operation' Process stopped.Use Step or Run to continue.
这好象是指针问题,可我没有定义指针啊
showmessage('ok');大概如此试下,不是很确定
TTestVdDLLForm=Class(TForm)
private
//以下面必须定义
procedure WMBarIcon(var Msg:TMessage); message WMBarIcon;
end;procedure TTestVcDLLForm.WMBarIcon(var Msg:TMessage);
begin
ShowMessage(IntToStr(Msg.lParam));
end;
const MYMSG_SENDSTR=WM_USER+1981;
extern "C" __declspec(dllexport) void WINAPI SendMsgTo(HWND wnd)
{
char * a=new char[20];
a="quanqi";
SendMessage(wnd,MYMSG_SENDSTR,(int)a,0);
}
//以下是我的Delphi测试代码
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
MYMSG_SENDMSG=WM_USER+1981;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure VCMSG(var Msg:TMessage);message MYMSG_SENDMSG;
end;var
Form1: TForm1;implementation{$R *.dfm}
procedure SendMsgTo(wnd:THandle);stdcall;external 'pro3.dll' index 1;procedure TForm1.Button1Click(Sender: TObject);
begin
SendMsgTo(Handle);
end;procedure TForm1.VCMSG(var Msg: TMessage);
begin
ShowMessage(PChar(Pointer(Msg.WParam)));
end;end.