Hooking GetOpenFileNameA API

c++ / delphi package - dll injection and api hooking
Post Reply
Pacino
Posts: 8
Joined: Thu May 05, 2005 3:17 pm

Hooking GetOpenFileNameA API

Post by Pacino »

In my application, I use madcodehook to hook GetOpenFileNameA API expecting that the openfile dialog won't appear but return with a desired filename.

Code: Select all

var
GetOpenFilenameANextHook: function (var OpenFile: TOpenFilenameA): Bool; stdcall;
....
HookAPI('Comdlg32.dll', 'GetOpenFileNameA',@myGetOpenFileNameA, @GetOpenFilenameANextHook);
....
function myGetOpenFileNameA(var OpenFile: TOpenFilenameA): Bool; stdcall;
var
szFile:PCHAR;
begin
  showmsg('HOoked');
  GetMem(szFile,260);
  ZeroMemory(szFile,260);
  szFile:='c:\1.txt';

  ZeroMemory(@OpenFile, sizeof(TOpenFilenameA));
  OpenFile.lStructSize := sizeof(TOpenFilenameA);
  OpenFile.hwndOwner := Hinstance;
  OpenFile.lpstrFile := szFile;
  OpenFile.nMaxFile := 260;
  OpenFile.lpstrFilter := pchar('All'#0'*.*'#0'Text'#0'*.TXT'#0);
  OpenFile.nFilterIndex := 1;
  OpenFile.lpstrFileTitle := nil;
  OpenFile.nMaxFileTitle := 0;
  OpenFile.lpstrInitialDir := nil;
  OpenFile.Flags := OFN_PATHMUSTEXIST or OFN_FILEMUSTEXIST;
  result:=true;
end;
The application raises an error when calling GetOpenFileNameA after pop up the message dialog "HOoked".
Can any one help me?
Thanks
dcsoft
Posts: 380
Joined: Sat Dec 11, 2004 2:11 am
Location: San Francisco Bay Area, CA USA
Contact:

Re: Hooking GetOpenFileNameA API

Post by dcsoft »

Pacino wrote: The application raises an error when calling GetOpenFileNameA after pop up the message dialog "HOoked".
It seems to be a mistake to call GetMem(szFile,...). The caller of your hook should have already allocated the memory to put the filename.

In addition, you shouldn't change the OpenFile struct by calling ZeroMemory, and filling in all the fields. The caller should do this.

In C (I don't know Delphi well), I would implement your hook like this:

Code: Select all

function myGetOpenFileNameA(var OpenFile: TOpenFilenameA): Bool; stdcall; 
begin 
  lstrcpy (OpenFile.lpstrFile, "c:\\1.txt");
  result:=true; 
end; 
Pacino
Posts: 8
Joined: Thu May 05, 2005 3:17 pm

Re: Hooking GetOpenFileNameA API

Post by Pacino »

dcsoft wrote: It seems to be a mistake to call GetMem(szFile,...). The caller of your hook should have already allocated the memory to put the filename.

In addition, you shouldn't change the OpenFile struct by calling ZeroMemory, and filling in all the fields. The caller should do this.

In C (I don't know Delphi well), I would implement your hook like this:

Code: Select all

function myGetOpenFileNameA(var OpenFile: TOpenFilenameA): Bool; stdcall; 
begin 
  lstrcpy (OpenFile.lpstrFile, "c:\\1.txt");
  result:=true; 
end; 
Thank you for your answer :D
I changed my code according to your suggestion and the application raises no error now. But unfortunately, it seems nothing happened to the application; it is supposed to do something with the text file and change content of its components (like listview etc.).

Below I will describe the routine of my application.
//Without HOOK
Start the application->Create a child form by clicking on "Options" button->Click "OpenFile" button and an openfile dialog appears->Select a file and click OK to return->Do something with the File
//With HOOK
Almost the same with the routine just said, but the openfile dialog won't appear and the application will process a desired file directly.

Now the problem is, the application do nothing with the desired file.

Need for your advice, thank you :confused:
madshi
Site Admin
Posts: 10766
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

You say you've changed your code but you didn't show us your new code yet. So we can't say what's wrong now.
Pacino
Posts: 8
Joined: Thu May 05, 2005 3:17 pm

Post by Pacino »

madshi wrote:You say you've changed your code but you didn't show us your new code yet. So we can't say what's wrong now.

Code: Select all

function myGetOpenFileNameA(var OpenFile: TOpenFilenameA): Bool; stdcall;
begin
  showmsg('HOoked');
  strcopy(OpenFile.lpstrFile,'c:\1.txt');
  result:=true;
end;
Other code keeps the same.
//bow

By the way, the code works with another simple demo.

Code: Select all

function myGetOpenFileNameA(var OpenFile: TOpenFilenameA): Bool; stdcall;
begin
  showmessage('HOoked');
  strcopy(OpenFile.lpstrFile,'c:\1.txt');
  result:=true;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if opendialog1.Execute then
memo1.Lines.LoadFromFile(opendialog1.FileName);
end;

....
HookAPI('Comdlg32.dll', 'GetOpenFileNameA',@myGetOpenFileNameA, @GetOpenFilenameANextHook);
.....
madshi
Site Admin
Posts: 10766
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Hmmmm... Looks alright to me. Perhaps you need to change some other elements of the structure? Not sure...
Post Reply