How do I get a string in return of the SendIpcMessage

c++ / delphi package - dll injection and api hooking
Post Reply
ch1c4um
Posts: 6
Joined: Wed Jun 27, 2012 7:09 pm

How do I get a string in return of the SendIpcMessage

Post by ch1c4um »

How do I get a string in return of the SendIpcMessage, na unit uIPCsender.

Receptor ipc:

Code: Select all

unit uIPCReceiver;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, madCodeHook;

type

   TAppDataFolder = record
      Path : array[0..254] of Char;
   end; PAppDataFolder = ^TAppDataFolder;

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure OnNewRec(name: pchar; messageBuf: pointer; messageLen: dword;
   answerBuf: pointer; answerLen: dword); stdcall;
var
  mfStruct: TAppDataFolder;
  waitFor: Boolean;
  source: string;
begin
  
   if Name = 'AppDataFolder_' then
   begin
     with PAppDataFolder( MessageBuf )^ do
      begin
         // i need set return here
      end;

   end
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  CreateIpcQueue( 'AppDataFolder_', OnNewRec );
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  DestroyIpcQueue('AppDataFolder_');
end;

end.
Sender Ipc:

Code: Select all

unit uIPCsender;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, madCodeHook, StdCtrls;

type
  TAppDataFolder = record
      Path : array[0..254] of Char;
   end; PAppDataFolder = ^TAppDataFolder;

  TForm1 = class(TForm)
    btn1: TButton;
    edt1: TEdit;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
var
  mfStruct: TAppDataFolder;
  waitFor: Boolean;
  source: string;
begin
  source := 'test';

  FillMemory( @mfStruct, SizeOf(TAppDataFolder), 0 );
  CopyMemory(@mfStruct.Path[0], @source[1], Length(source));

  if SendIpcMessage('AppDataFolder_', @mfStruct, SizeOf(TAppDataFolder),
     @waitFor, SizeOf(Boolean)) then
  begin
    showmessage(PChar( @mfStruct.Path[0] ));
  end
  else
    showmessage('not');
end;

end.
someone help me?
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Re: How do I get a string in return of the SendIpcMessage

Post by madshi »

Currently you're using feeding "waitFor: Boolean" to SendIpcMessage to get a reply from the IPC queue owner. However, your IPC queue callback function "OnNewRec" doesn't even touch "answerBuf".

What you should do is:

(1) Define a return structure type, e.g. "type TAppDataFolderReply = ..." with all the reply data you need.
(2) Then declare a local variable of type TAppDataFolderReply and feed that to SendIpcMessage.
(3) In "OnNewRec" typecase "answerBuf" to "PAppDataFolderReply(answerBuf)", then you can set all the reply fields.
ch1c4um
Posts: 6
Joined: Wed Jun 27, 2012 7:09 pm

Re: How do I get a string in return of the SendIpcMessage

Post by ch1c4um »

Tks Madshi
Post Reply