Problem with ExtTextOutW

c++ / delphi package - dll injection and api hooking
Post Reply
Knomer
Posts: 4
Joined: Tue Jun 25, 2013 12:16 pm

Problem with ExtTextOutW

Post by Knomer »

Delphi XE2, InjectLibrary(hProcess, 'hook.dll')
log.txt: some of the text is correct, other - trash (attached picture - text from standart control TEdit), why?
Help me please!!!

Code: Select all

library hook;

{$IMAGEBASE $5a800000}

uses
  Windows, Classes, SysUtils, madCodeHook, madRemote, madStrings;

procedure AppendStrToFileAW(str: WideString; fileName: string);
var
  Outfile: TStreamWriter;
begin
  Outfile := TStreamWriter.Create(fileName, True);
  try
    Outfile.WriteLine(str);
  finally
    Outfile.Free;
  end;
end;

procedure Log(text: WideString);
var mutex: dword;
begin
  mutex := CreateGlobalMutex('HookLoadLibraryLogMutex');
  if mutex <> 0 then
  begin
    if WaitForSingleObject(mutex, 100) = WAIT_OBJECT_0 then
    begin
      AppendStrToFileAW(text, 'd:\log.txt');
      ReleaseMutex(mutex);
    end;
    CloseHandle(mutex);
  end;
end;

var 
  ExtTextOutWNextHook: function (theDC: HDC; 
                                                     nXStart, nYStart: integer; 
						     fuOptions: Longint; 
						     Rect: PRect; 
						     str: PWideChar; 
						     count: Longint; 
						     dx: PInteger): bool; stdcall;


function ExtTextOutWCallbackProc(theDC: HDC; 
                                                    nXStart, nYStart: integer; 
						    fuOptions: Longint; 
						    Rect: PRect; 
						    str: PWideChar; 
						    count: Longint; 
						    dx: PInteger): bool; stdcall;
begin
  ExtTextOutWNextHook(theDC, nXStart, nYStart, fuOptions, Rect, str, count, dx);
  Log(str);
end;

begin
  HookAPI('gdi32.dll',    'ExtTextOutW', @ExtTextOutWCallbackProc, @ExtTextOutWNextHook);
end.
Attachments
12345.png
12345.png (550 Bytes) Viewed 6788 times
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Re: Problem with ExtTextOutW

Post by madshi »

I'm not sure why this happens. One possible explanation would be that the "str: PWideChar" parameter is not guaranteed to be zero terminated - but you treat it that way. You may have to take "count: Longint" into account.
Knomer
Posts: 4
Joined: Tue Jun 25, 2013 12:16 pm

Re: Problem with ExtTextOutW

Post by Knomer »

I try to deal with the length of the string...
trying to replace the text in the control of another window (), trash again :(
is replaced by the correct number of symbols, but these symbols wrong

Code: Select all

function ExtTextOutWCallbackProc(theDC: HDC; nXStart, nYStart: integer; fuOptions: Longint; Rect: PRect; str: PWideChar; count: Longint; dx: PInteger): bool; stdcall;
var
  i: Integer;
  ws: WideString;
  len: Integer;
begin
  SetLength(ws, count);
  for i := 1 to count do ws[i] := '1';
  ExtTextOutWNextHook(theDC, nXStart, nYStart, fuOptions, Rect, PWideChar(ws), count, dx);
end;
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Re: Problem with ExtTextOutW

Post by madshi »

Well, what can I say? Maybe the control is using a font which has weird signs where you expect numbers?

One thing I just noted: You should use "result := ExtTextOutWNextHook(...)". Currently you're not setting the result value at all.
Knomer
Posts: 4
Joined: Tue Jun 25, 2013 12:16 pm

Re: Problem with ExtTextOutW

Post by Knomer »

Thanks. I will try and if I can solve the problem (I hope I can) -I'll immediately buy madhook.
Knomer
Posts: 4
Joined: Tue Jun 25, 2013 12:16 pm

Re: Problem with ExtTextOutW

Post by Knomer »

I did it.

Code: Select all

function ExtTextOutWCallbackProc(theDC: HDC; nXStart, nYStart: integer; fuOptions: Longint; Rect: PRect; str: PWideChar; count: Longint; dx: PInteger): bool; stdcall;
begin
  Result := ExtTextOutWNextHook(theDC, nXStart, nYStart, fuOptions, Rect, str, count, dx);
  if fuOptions and ETO_GLYPH_INDEX = ETO_GLYPH_INDEX then
    Log(DecodeGlyph(theDC, str, count))
  else
    Log(str);
end;
for function DecodeGlyph I used: GetFontUnicodeRanges and GetGlyphIndicesW

I'm happy. Now I can buy the madhook!!!
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Re: Problem with ExtTextOutW

Post by madshi »

Good to hear! Let's move buying talks to email, thanks.
Post Reply