Unicode support in madCodeHook

c++ / delphi package - dll injection and api hooking
jossanjo
Posts: 18
Joined: Thu Jul 15, 2004 9:30 am

Post by jossanjo »

Ok - sounds perfect!

Another question,

I have a routine to get the exe file name from a window handle but I am thinking of using your 'Window(windowHnd).OwnerProcess.ExeFile' instead.

My only question is if the 'Window(windowHnd).OwnerProcess.ExeFile' will support exe-files with Unicode names?

Kind Regards
Thomas
madshi
Site Admin
Posts: 10764
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

No, it's ANSI only right now.
jossanjo
Posts: 18
Joined: Thu Jul 15, 2004 9:30 am

Post by jossanjo »

Do you have any suggestions how I may get a unicode exe file name from a window handle? I am searching but I can't find anything...

Kind Regards
Thomas
madshi
Site Admin
Posts: 10764
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Are the toolhelp functions available in wide? Not sure. You could try psapi.dll, but it only works in the NT family.
jossanjo
Posts: 18
Joined: Thu Jul 15, 2004 9:30 am

Post by jossanjo »

Hmm yes I found Process32FirstW and Process32NextW so I guess I can make it work somehow... :o
jossanjo
Posts: 18
Joined: Thu Jul 15, 2004 9:30 am

Post by jossanjo »

Hehe by the way it seems like it is your code (enumStuff) I am about to add unicode support to: http://www.howtodothings.com/viewarticl ... rticle=343
:D

Was this before the Madshi lib became commercial?

/Thomas
madshi
Site Admin
Posts: 10764
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Yep, that's an older work from me. It's still available for download under http://madshi.net/enumStuff.zip .
jossanjo
Posts: 18
Joined: Thu Jul 15, 2004 9:30 am

Post by jossanjo »

Hi,

I'm sorry but I don't really get how to use your encryption/decryption methods on widestring data. :cry: This is what I tried but with not luck when I tried with Unicode chars (it works with ansi though).

//First encrypt the data - let say I have typed in 'ьфвырш рфы ф пкуфе дши'
procedure TForm1.TntButton2Click(Sender: TObject);
var w : widestring;
begin
w := TntEdit1.Text; //uses Tnt controls for Unicode support instead of VCL
Encrypt(PWideChar(w), Length(w), 'sdf');
TntEdit2.Text := Encode(w); //Save the encrypted and encoded data
end;

//Then try to get the original data back
procedure TForm1.TntButton1Click(Sender: TObject);
var w : widestring;
begin
w := Decode(TntEdit2.Text);
Decrypt(PWideChar(w), Length(w), 'sdf');
TntEdit3.Text := w;
end;

Thanks
Thomas
madshi
Site Admin
Posts: 10764
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

"Length(w)" is not correct. This returns the number of *characters*, not the size of the data. You know, with wide strings each character is 2 bytes long. So you have to use "Length(w) * sizeOf(wideChar)".
jossanjo
Posts: 18
Joined: Thu Jul 15, 2004 9:30 am

Post by jossanjo »

Thanks but I still don't get it to work. This is what I changed to:

procedure TForm1.TntButton2Click(Sender: TObject);
var w : widestring;
begin
w := TntEdit1.Text;
Encrypt(PWideChar(w), Length(w) * sizeOf(WideChar), 'sdf');
TntEdit2.Text := Encode(w);
end;

procedure TForm1.TntButton1Click(Sender: TObject);
var w : widestring;
begin
w := Decode(TntEdit2.Text);
Decrypt(PWideChar(w), Length(w) * sizeOf(WideChar), 'sdf');
TntEdit3.Text := w;
end;

Kind Regards
Thomas
madshi
Site Admin
Posts: 10764
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

When you call Encode like that, Delphi converts the wideString to ansiString. You need to call the other Encode procedure (the one with pointer/len). Also when getting a string back from the Decode function this is not really a string. It's a Delphi dynamic string, but it will contain the wideString data.

Basically stop thinking in string terms. Imagine the wideString would be pure data like bitmap pixels. As a result you can't use the string-variations of the encrypt/encode/decrypt/decode functions, but you need to use the buffer variations instead.

Perhaps it would be easier for you to understand, if you would convert the wideString into a real allocated buffer. It's not really necessary, but it might help you understanding how you need to do it.
jossanjo
Posts: 18
Joined: Thu Jul 15, 2004 9:30 am

Post by jossanjo »

This is driving me nuts. I just can't get it to work!

Do you have a short example on this? I looked in your documentation but I can't find it.

Please just a short example that shows how to do it.

Thanks
Thomas
madshi
Site Admin
Posts: 10764
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Code: Select all

var ws : wideString;
    s1 : string;
begin
  ws := 'test';
  ShowMessage(ws);
  Encrypt(pointer(ws), Length(ws) * sizeOf(wideChar), 'password');
  s1 := Encode(pointer(ws), Length(ws) * sizeOf(wideChar));
  ShowMessage(s1);
  s1 := Decode(pointer(s1), Length(s1));
  Decrypt(pchar(s1), Length(s1), 'password');
  ws := PWideChar(@s1[1]);
  ShowMessage(ws);
It's not that difficult, or is it?
jossanjo
Posts: 18
Joined: Thu Jul 15, 2004 9:30 am

Post by jossanjo »

Well for me it was! :o

Thanks a lot! :crazy:
/Thomas
jossanjo
Posts: 18
Joined: Thu Jul 15, 2004 9:30 am

Post by jossanjo »

Hi,

One last question about the Unicode support in your lib: is there a Unicode alternative to your SysFolder function? It only returns a string and guess that this folder may be named something in Unicode characters in e.g. China.

Thanks
Thomas
Post Reply