急!急!急!怪!我用postmessage传一个字符串,接收时只能收到8个以内的字符,源码如下 (100分)

  • 主题发起人 主题发起人 wh_sun
  • 开始时间 开始时间
W

wh_sun

Unregistered / Unconfirmed
GUEST, unregistred user!
我换一个参数来发送问题解决了,但我不知道为什么,希望大家指点
var
s:string;
myh:Thandle;
begin
s:='1234567890';
postmessage(handle,mymessage,longint(pchar(s)),myh);
end;

//接收时
WndProc(var Msg: TMessage);
var
p:pchar;
s1:string;
begin
if msg=mymessaga then

begin
p:=pchar(msg.wparam);
s1:=p;
//这时发现 s1:='12345678 '后面的字符没有了
end;

end;
请那位大侠指教
但我用
var
s:string;
myh:Thandle;
begin
s:='1234567890';
postmessage(handle,mymessage,myh,longint(pchar(s)));
end;

//接收时
WndProc(var Msg: TMessage);
var
p:pchar;
s1:string;
begin
if msg=mymessaga then

begin
p:=pchar(msg.lparam);
s1:=p;
//这时发现 s1:='1234567890';没有问题,为什么,这两个参数有什么区别?
end;

end;
 
利用发送CopyData消息来传字符串
var
hMutex : THandle;
ds: TCopyDataStruct;
begin
hMutex :=FindWindow(UploadExeClass,nil);
if hMutex = 0 then
begin
if Not FileExists(ExtractFilePath(Application.ExeName)+UploadExeName) then
begin
MessageBox(Handle,'系统没有安装资料上传程序,请咨询系统管理员!','操作提示',MB_OK Or MB_ICONINFORMATION);
Exit;
end;
WinExec(PChar(UploadExeName),SW_SHOW);
Application.ProcessMessages;
While hMutex = 0do
hMutex := FindWindow(UploadExeClass,nil);
end;
//+4 = (包含3个";"和本身的1)
ds.cbData := Length (URL)+Length(AgentID)+Length(DatumName)+Length(DatumBrief) + 4;
GetMem (ds.lpData,ds.cbData);
//为传递的数据区分配内存
Try
StrCopy (ds.lpData, PChar(URL+';'+AgentID+';'+DatumName+';'+DatumBrief));//要传递的字符串
Application.ProcessMessages;
SendMessage (hMutex, WM_COPYDATA, Handle,Cardinal(@ds));
// 发送WM_COPYDATA消息
Finally
FreeMem(ds.lpData);
end;
end;
 
to oldsleep35:
很怪,我按照明你的方法写了一个发送的过程如下,但收到的字符也不是我发出的,为什么会这样?
procedure send_msg(socket:Thandle);
var
// hMutex : THandle;
ds: TCopyDataStruct;
s:string;
begin
socket:=somehandle;
s:='abcdefghijklmnopqrstuvwxyz_2abcdefghijklmnopqrstuvwxyx_3abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';
ds.cbData := Length (S)+1 ;
GetMem (ds.lpData,ds.cbData);
//为传递的数据区分配内存
Try
StrCopy (ds.lpData, PChar(S));//要传递的字符串
//Application.ProcessMessages;
SendMessage (handle, WM_COPYDATA,socket,Cardinal(@ds));
// 发送WM_COPYDATA消息
Finally
FreeMem(ds.lpData);
end;

end;

接收时
procedure WndProc(var Msg: TMessage);
VAR
p:pchar;
commend: string;
mysockhandle:Thandle;
begin

if Msg.Msg = WM_COPYDATA then
begin
p:=pchar(msg.lparam);
commend:=String(p);
mysockhandle:=msg.WPARAM;
showmessage(commend);
end;

inherited;
end;
 
我来试试你的代码!
 
这么写:(你要稍作修改)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure DatumEditMessage(var CopyDataMessage:TWMCOPYDATA);message WM_COPYDATA;
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.DatumEditMessage(var CopyDataMessage: TWMCOPYDATA);
begin
ShowMessage(Trim(StrPas(CopyDataMessage.CopyDataStruct.lpData)));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
hMutex : THandle;
ds: TCopyDataStruct;
begin
hMutex :=FindWindow(nil,'Form1');
if hMutex = 0 then
begin
Application.ProcessMessages;
While hMutex = 0do
hMutex := FindWindow(nil,'Form1');
end;
//+4 = (包含3个";"和本身的1)
ds.cbData := Length('abcdefghijklmnopqrstuvwxyz_2abcdefghijklmnopqrstuvwxyx_3abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ')+1;
GetMem (ds.lpData,ds.cbData);
//为传递的数据区分配内存
Try
StrCopy (ds.lpData, PChar('abcdefghijklmnopqrstuvwxyz_2abcdefghijklmnopqrstuvwxyx_3abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ'));
Application.ProcessMessages;
SendMessage (hMutex, WM_COPYDATA, Handle,Cardinal(@ds));
// 发送WM_COPYDATA消息
Finally
FreeMem(ds.lpData);
end;
end;

end.
 
楼主,这样可以了
自己改一下吧!
 
to oldsleep35
首先感谢你对我的问题的关注,你的代码我试过了可以
只是,我在sendmessage中要传两个参数,一个是字符串-用;msg.lparam;还有一个参数object.handle;所以我用wndproc来处理这两个,我把你的代码修改后如下
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure DatumEditMessage(var CopyDataMessage:TWMCOPYDATA);message WM_COPYDATA;
procedure WndProc(var Msg: TMessage);
override;
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.DatumEditMessage(var CopyDataMessage: TWMCOPYDATA);
begin
ShowMessage(Trim(StrPas(CopyDataMessage.CopyDataStruct.lpData)));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
hMutex : THandle;
ds: TCopyDataStruct;
begin
hMutex :=FindWindow(nil,'Form1');
if hMutex = 0 then
begin
Application.ProcessMessages;
While hMutex = 0do
hMutex := FindWindow(nil,'Form1');
end;
//+4 = (包含3个";"和本身的1)
ds.cbData := Length('abcdefghijklmnopqrstuvwxyz_2abcdefghijklmnopqrstuvwxyx_3abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ')+1;
GetMem (ds.lpData,ds.cbData);
//为传递的数据区分配内存
Try
StrCopy (ds.lpData, PChar('abcdefghijklmnopqrstuvwxyz_2abcdefghijklmnopqrstuvwxyx_3abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ'));
Application.ProcessMessages;
SendMessage (hMutex, WM_COPYDATA, Handle,Cardinal(@ds));
// 发送WM_COPYDATA消息
Finally
FreeMem(ds.lpData);
end;
end;

procedure TForm1.WndProc(var Msg: TMessage);
VAR
dd:TCopyDataStruct;
//能不能在这里把它取出来?
p:pchar;
commend: string;
mysockhandle:Thandle;
begin

if Msg.Msg = WM_COPYDATA then
begin
//dd:=msg.lparam;
p:=pchar(msg.lparam);
{dd:TCopyDataStruct;
//能不能在这里把它取出来?
使commend:=dd.lpdata}
commend:=String(p);
commend:=Trim(StrPas(dd.lpData))
mysockhandle:=msg.WPARAM;
//这是另外一个参数,是我必要的
showmessage(commend);
end;

inherited;

end;

end.
 
这么做:
procedure TForm1.WndProc(var Msg: TMessage);
VAR
dd:TCopyDataStruct;
//能不能在这里把它取出来?
p:pchar;
commend: string;
mysockhandle:Thandle;
begin
if Msg.Msg = WM_COPYDATA then
begin
ShowMessage(Trim(StrPas(TWMCOPYDATA(Msg).CopyDataStruct.lpData)));//这么做就可以了
//dd:=msg.lparam;
//p:=pchar(msg.lparam);
{dd:TCopyDataStruct;
// 使commend:=dd.lpdata}
//commend:=String(p);
//commend:=Trim(StrPas(dd.lpData));
mysockhandle:=msg.WPARAM;
//这是另外一个参数,是我必要的
showmessage(commend);
end;

inherited;
 
s:string;
全局变量
 
to oldsleep35:
我给你加分了,用你这种方式能够解决这个问题,尽管我还是不知道为什么,用wparam来传递字符串不行,而用lparam传递字符串行
 
后退
顶部