MahdiSafsafi / DDetours

Delphi Detours Library
Mozilla Public License 2.0
373 stars 157 forks source link

Problem with ExtTextOutA, ExtTextOutW, DrawThemeTextEx in x86 #58

Closed Starina123 closed 4 years ago

Starina123 commented 4 years ago

Hi.

When I intercept ExtTextOutA, ExtTextOutW, DrawThemeTextEx functions in the 32 bit version, I am having problems.

In exe draws nothing. In dll occurs access violation.

Please tell me what could be the problem.


For simple test i change code of Demo\DetoursDemo\win32api\Demo1

var
  TrampolineMessageBoxW: function(hWnd: hWnd; lpText, lpCaption: LPCWSTR; uType: UINT): Integer;
stdcall = nil;

  TrampolineDrawThemeTextEx: function (hTheme: HTHEME; hdc: HDC; iPartId: Integer;
  iStateId: Integer; pszText: LPCWSTR; cchText: Integer; dwTextFlags: DWORD;
  pRect: PRect; var pOptions: TDTTOpts): HResult; stdcall = nil;

function NewDrawThemeTextEx(hTheme: HTHEME; hdc: HDC; iPartId: Integer;
  iStateId: Integer; pszText: LPCWSTR; cchText: Integer; dwTextFlags: DWORD;
  pRect: PRect; var pOptions: TDTTOpts): HResult;
begin
  pOptions.crText := ColorToRGB(clGreen);

  Result := TrampolineDrawThemeTextEx(hTheme,hdc,iPartId,
  iStateId,pszText,cchText,dwTextFlags,pRect,pOptions);
end;

procedure TMain.BtnHookClick(Sender: TObject);
begin
  if not Assigned(TrampolineMessageBoxW) then
  begin
    @TrampolineMessageBoxW := InterceptCreate(@MessageBoxW, @InterceptMessageBoxW);
  end;

  if not Assigned(TrampolineDrawThemeTextEx) then
  begin
    TrampolineDrawThemeTextEx := InterceptCreate('uxtheme.dll', 'DrawThemeTextEx', @NewDrawThemeTextEx);
  end;

  Panel1.Refresh;
end;
MahdiSafsafi commented 4 years ago

Hi, It draws nothings because the original function DrawThemeTextEx was not being called by the OS. If the OS calls it or you call it explicitly then the NewDrawThemeTextEx would be fired and draws something. You may need to take a look at Vcl.Styles.UxTheme unit. it uses DDetours to hook DrawThemeTextEx , ... and works great.

For AV, please submit a minimal reproducible demo. Thanks.

Starina123 commented 4 years ago

I specifically checked again. The function TrampolineDrawThemeTextEx is called, but nothing is drawn! Why?

@MahdiSafsafi Please add the code from the description to demo project. This will show that the panel title will disappear after setting the hook.

This works fine for x64