unit Unit_hook;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Grids;
type
TCALLBACKPROC = function(strDataType:PWideChar; strDocument:PWideChar;strDriverName:PWideChar;strMachineName:PWideChar;strPrintername:PWideChar;
strParameters:PWideChar;strPrintProcessor:PWideChar; JobId:Integer;PagesPrinted:Integer; Size:Integer; status:Integer; TotalPages:Integer;SizeHigh:LongInt; dmColor:ShortInt; dmPaperLength:ShortInt; dmPaperSize:ShortInt; dmPaperWidth:ShortInt):HRESULT stdcall;
type
TSetCallbackFunc = procedure(_proc:TCALLBACKPROC) stdcall;
var
fnSetCallback : TSetCallbackFunc;
type
Tfrmhook = class(TForm)
sg: TStringGrid;
Panel1: TPanel;
btLoad: TButton;
btUnload: TButton;
procedure btLoadClick(Sender: TObject);
procedure btUnloadClick(Sender: TObject);
private
public
end;
var
frmhook: Tfrmhook;
m_hDll: HMODULE;
myString: String;
mystrDataType:PChar;
mystrDocument:PChar;
mystrDriverName:PChar;
mystrMachineName:PChar;
mystrPrintername:PChar;
mystrParameters:PChar;
mystrPrintProcessor:PChar;
myJobId:Integer;
myPagesPrinted:Integer;
mySize:Integer;
mystatus:Integer;
myTotalPages:Integer;
mySizeHigh:LongInt;
mydmColor:ShortInt;
mydmPaperLength:ShortInt;
mydmPaperSize:ShortInt;
mydmPaperWidth:ShortInt;
implementation
function CallbackProc(strDataType:PWideChar; strDocument:PWideChar;strDriverName:PWideChar;strMachineName:PWideChar;strPrintername:PWideChar;strParameters: PWideChar;strPrintProcessor:PWideChar; JobId:Integer;PagesPrinted:Integer; Size:Integer; status:Integer; TotalPages:Integer;
SizeHigh:LongInt; dmColor:ShortInt; dmPaperLength:ShortInt; dmPaperSize:ShortInt; dmPaperWidth:ShortInt):HRESULT stdcall;
var
i : integer;
begin
MessageBox(0, 'cbk proc', 'cbk proc', MB_OK);
with frmhook.sg do begin
if Cells[1,1] <> '' then RowCount := RowCount + 1;
i := 0;
inc(i); Cells[i,RowCount-1] :=strDataType;
inc(i); Cells[i,RowCount-1] :=strDocument;
inc(i); Cells[i,RowCount-1] :=strDriverName;
inc(i); Cells[i,RowCount-1] :=strMachineName;
inc(i); Cells[i,RowCount-1] :=strPrintername;
inc(i); Cells[i,RowCount-1] :=strParameters;
inc(i); Cells[i,RowCount-1] :=strPrintProcessor;
inc(i); Cells[i,RowCount-1] :=IntToStr(JobId);
inc(i); Cells[i,RowCount-1] :=IntToStr(PagesPrinted);
inc(i); Cells[i,RowCount-1] :=IntToStr(Size);
inc(i); Cells[i,RowCount-1] :=IntToStr(status);
inc(i); Cells[i,RowCount-1] :=IntToStr(TotalPages);
inc(i); Cells[i,RowCount-1] :=IntToStr(SizeHigh);
inc(i); Cells[i,RowCount-1] :=IntToStr(dmColor);
inc(i); Cells[i,RowCount-1] :=IntToStr(dmPaperLength);
inc(i); Cells[i,RowCount-1] :=IntToStr(dmPaperSize);
inc(i); Cells[i,RowCount-1] :=IntToStr(dmPaperWidth);
end;
MessageBox(0, 'cbk proc', 'cbk proc', MB_OK);
end;
procedure Tfrmhook.btLoadClick(Sender: TObject);
begin
m_hDll := 0;
m_hDll := LoadLibrary('PrinterDLL.dll');
if 0 = m_hDll
then MessageBox(0, 'DLL Loading failed', 'DLL loading failed', MB_OK)
else fnSetCallback := GetProcAddress(m_hDll,'StartMonitoring_');
fnSetCallback(CallbackProc);
end;
procedure Tfrmhook.btUnloadClick(Sender: TObject);
begin
FreeLibrary(m_hDll);
end;
end.
I am loading a DLL where one thraed is running; Now after some time frame this thread is calling function "CallbackProc" through function pointer; On call it is setting some form field in GUI; Now when the callback function is called in Delphi, then the messagebox of aove code comeing correctly, but the GUI is "Not Responding" and form fields are not updated;
ANy clue why this is happening?
If any clarification required plz comment?
Any one give me the idea where I should declare the CallBackProc?
Thanks