library HookFunc;

uses
  SysUtils, Types, Windows, WinSock, Classes;

type
  TCAPIINFO = record
  module_name,func, c_func_name:PChar;
  end;
{$R *.res}
var
  c_api_info:array[1..5] of TCAPIINFO;

function cSetCurrentDirectoryA(dir:PChar):DWORD;stdcall;
var
  ret:LongBool;
  //err:integer;
begin
  MessageBox(0, dir, 'cSetCurrentDirectory...', MB_OK);
  ret :=SetCurrentDirectoryA(dir);
  //err =GetLastError();
  //your handle
  //SetLastError(err);
  result :=DWORD(ret);
end;

function cSetCurrentDirectoryW(dir:PWChar):DWORD;stdcall;
var
  ret:LongBool;
  //err:integer;
begin
  MessageBoxW(0, dir, 'cSetCurrentDirectoryW...', MB_OK);
  ret :=SetCurrentDirectoryW(dir);
  //err =GetLastError();
   //your handle
  //SetLastError(err);
  result :=DWORD(ret);
end;

// in Win32£ĴOpenFile instead of CreateFile£Ĵactually open files call CreateFile
function cOpenFile(fname:PChar; pofs:POFSTRUCT; style:UINT):DWORD;stdcall;
var
  ret:HFILE;
  //err:integer;
begin
  MessageBox(0, fname, 'cOpenFile...', MB_OK);
  ret :=OpenFile(fname, pofs^, style);
  //err =GetLastError();
  //your handle
  //SetLastError(err);
  Result := ret;
end;

function cCreateFileA(fname:PChar;dwDesiredAccess:DWORD;dwShareMod:DWORD;lpSecurityAttributes:PSecurityAttributes;dwCreationDisposition:DWORD;dwFlagsAndAttributes:DWORD;hTemplateFile:HFILE):DWORD;stdcall;
var
  ret:HFILE;
  //err:integer;
begin
  MessageBox(0, fname, 'cCreateFileA...', MB_OK);
  ret :=CreateFileA(fname, dwDesiredAccess, dwShareMod, lpSecurityAttributes,
		dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
  //err =GetLastError();
  //your handle
  //SetLastError(err);
  Result := ret;
end;

Function GetCAPIINFO():Pointer;stdCall;
begin
	GetCAPIINFO :=Addr(c_api_info);
end;

exports
  GetCAPIINFO,
  cSetCurrentDirectoryA,
  cSetCurrentDirectoryW,
  cOpenFile,
  cCreateFileA;

begin
  c_api_info[1].module_name :='KERNEL32.DLL';
  c_api_info[1].func :='SetCurrentDirectoryA(a)';
  c_api_info[1].c_func_name :='cSetCurrentDirectoryA';
  c_api_info[2].module_name :='KERNEL32.DLL';
  c_api_info[2].func :='OpenFile(a,b,c)';
  c_api_info[2].c_func_name :='cOpenFile';
  c_api_info[3].module_name :='KERNEL32.DLL';
  c_api_info[3].func :='OpenFile(a,b,c)';
  c_api_info[3].c_func_name :='cOpenFile';
  c_api_info[4].module_name :='KERNEL32.DLL';
  c_api_info[4].func :='CreateFileA(1,2,3,4,5,6,7)';
  c_api_info[4].c_func_name :='cCreateFileA';
  c_api_info[5].module_name :=nil;
  c_api_info[5].func :=nil;
  c_api_info[5].c_func_name :=nil;
end.


