Hooking Shell32.Shell32ExecuteA

c++ / delphi package - dll injection and api hooking
Post Reply
Mazinger
Posts: 33
Joined: Wed Jan 26, 2005 6:26 am

Hooking Shell32.Shell32ExecuteA

Post by Mazinger »

Hi forum,

I'm trying to hook the Shell32ExecuteA function, and inside, change the value for "parameters" variable. My code is like this:

Code: Select all

Function ShellExecuteA_API(hWnd: HWND; Operation, FileName, Parameters, Directory: PAnsiChar; ShowCmd: Integer): HINST; stdcall;
Var nParameters:PAnsiChar;
Begin

  GetMem(nParameters,255);
  ZeroMemory(nParameters,255);
  nParameters:=PAnsiChar('NEWPARAM');

  Result:=ShellExecuteA_Next(hWnd,Operation,FileName,nParameters,Directory,ShowCmd);

  FreeMem(nParameters);


End;
but when this function is called I receive an Access violation error.

Any idea about how to do this?

Thanks in advance.
iconic
Site Admin
Posts: 1065
Joined: Wed Jun 08, 2005 5:08 am

Post by iconic »

Don't have time to test this myself but if FileName points to an executable application, only then can you pass parameters (refer to MSDN or Win32 API Help File). You need to make sure that this is not a document, not sure if you'd receive an AV or just have the OS ignore parameters because when it's not an executable it mentions how parameters should be set to Nil. I'd do a sanity check on this first. If you do pass a parameter string to the executable, it needs to be quoted in your string

i.e> lpCstr := PAnsiChar('"-some_param"');


--Iconic
Mazinger
Posts: 33
Joined: Wed Jan 26, 2005 6:26 am

Post by Mazinger »

Hi, thanks for your answer.

I've solved the problem with AV by changing GetMem for localAlloc ...
madshi
Site Admin
Posts: 10764
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Your code is really bad. First you're allocating memory, then instead of using the allocated memory, you're changing the buffer pointer to a contant string. Finally, you're freeing the buffer pointer, which is the constant string. Basically your code has a memory leak and then tries to free a constant string. VERY bad code.

I think you need to learn about pointer programming before you even start thinking about doing API hooking... :?
Post Reply