hi I found some c++ code online and am trying to convert it to work in delphi. its been 3 yrs since I last programmed c++ and I have less experience in delphi. I've done most if not all of the foot work already and just it to compile properly. If someone can help me properly set up the Begin..End..Else blocks in delphi and get the DLL functioning I will award them 4000 points. I'll grade an A and then open another empty question and award you another 2000 points. Since this question is worth 500 pts (4 * 500 = 2000) *2 = 4000 pts. It took me about an hour with the help of a friend and here's what I've come up with. My conditional blocks are off (it seems) and I believe thats the reason it will not function properly due to a halt in code execution.
//------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
{ The DLL Code }
library dDLL;
uses
SysUtils,
windows, psapi;
var
x: integer = 0;
stream: integer;
count: integer;
bHooked: longbool = false;
CBT: hHook = 0;
CBT1: hHook = 0;
HINSTANCE: hInst;
szModName: array[0..1023]of char;
szModName1: array[0..1023]of char;
nul: array[0..1023]of char;
win: array[0..MAX_PATH]of char;
function StrStr(source, target: Pchar):integer;
var
p: PChar;
s1, s2: array[0..$FF]of Char;
begin
StrPCopy(s1, source);
StrPCopy(s2, target);
p := StrPos(s1, s2);
StrStr := (p - s1) +1;
end;
function GetWinDir: Pchar;
begin
zeromemory(@win, sizeof(win));
GetWindowsDirectory(win, MAX_PATH);
result := StrCat(@win, '\explorer.exe');
end;
function CBTProc(ncode: integer; wPARAM: WPARAM; lPARAM: LPARAM):LongInt;stdcall
var
hProc: Thandle;
hMods: Array [0..1023]of HMODULE;
n,
dwProcessId,
lpExitCode,
dwSize,
dwType,
dwDisp: DWORD;
Regentry: HKEY;
host1: PChar;
host: array[0..1023]of char;
rezerva: Pchar;
begin
if (nCode = HCBT_ACTIVATE)
or
(nCode = HCBT_SYSCOMMAND)
or
(nCode = HCBT_QS)
or
(nCode = HCBT_CREATEWND) then
begin
GetWindowThreadProcessId(H
WND(wParam
), @dwProcessID);
hProc := OpenProcess(PROCESS_ALL_AC
CESS, FALSE, dwProcessId);
if EnumProcessModules(hproc, @hMods, sizeof(hMods), n) then
begin
if ( n > 0) then
GetModuleFileNameEx(hProc,
hMods[0], szModName, sizeof(szModName));
end;
GetExitCodeProcess(hProc, lpExitCode);
if not (host1 = strRscan(szModName,'\')) then
strcopy(host, szModName)
else
strcopy(host,host1 +1);
RegOpenKeyEx(HKEY_LOCAL_MA
CHINE, 'SOFTWARE\Gapula\PEND', 0, KEY_QUERY_VALUE, Regentry);
RegQueryValueEx(Regentry,h
ost, nil, @dwType, Pbyte(rezerva), @dwSize);
if RegQueryValueEx(Regentry,h
ost , nil, @dwType, Pbyte(rezerva), @dwSize) <> ERROR_SUCCESS
then
begin
RegOpenKeyEx(HKEY_LOCAL_MA
CHINE, 'SOFTWARE\Gapula\OK', 0, KEY_QUERY_VALUE, Regentry);
RegQueryValueEx(Regentry,h
ost , nil, @dwType, Pbyte(rezerva), @dwSize);
if RegQueryValueEx(Regentry,h
ost ,nil, @dwType, Pbyte(rezerva), @dwSize) <> ERROR_SUCCESS
then
begin
RegCloseKey(Regentry);
RegOpenKeyEx(HKEY_LOCAL_MA
CHINE, 'SOFTWARE\Gapula\RESTR', 0, KEY_QUERY_VALUE or KEY_ALL_ACCESS, Regentry);
RegQueryValueEx(Regentry,h
ost , nil, @dwType, Pbyte(rezerva), @dwSize);
end;
if RegQueryValueEx(Regentry,h
ost , nil, @dwType, Pbyte(rezerva), @dwSize) <> ERROR_SUCCESS
then
RegCreateKeyEx(HKEY_LOCAL_
MACHINE, 'SOFTWARE\Gapula\PEND', 0, '', REG_OPTION_NON_VOLATILE,KE
Y_WRITE, nil, Regentry, @dwDisp);
RegSetValueEx(Regentry, host, 0, REG_SZ, @szModName, strlen(szModName) +1);
RegCloseKey(Regentry);
strcat(szModName, 'is trying to start, do you allow that ?'+#13+
'Please recall that if you say yes this action will be happening every time this program starts'+#13+
'This goes for NO as well so be careful what you wish for');
if (MessageBox(0,szModName,'G
abby',MB_I
CONQUESTIO
N or
MB_SYSTEMMODAL or
MB_APPLMODAL or
MB_TASKMODAL or
MB_SETFOREGROUND or
MB_TOPMOST or
MB_YESNO) = IDNO)
then
begin
RegCreateKeyEx(HKEY_LOCAL_
MACHINE, 'SOFTWARE\Gapula\RESTR', 0, '', REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, Regentry, @dwDisp);
RegSetValueEx(Regentry, host, 0, REG_SZ, @szModName, strlen(szModName)+1);
RegCloseKey(Regentry);
TerminateProcess(hProc, lpExitCode);
end
else
begin
RegCreateKeyEx(HKEY_LOCAL_
MACHINE, 'SOFTWARE\Gapula\OK', 0, '', REG_OPTION_NON_VOLATILE,KE
Y_WRITE, 0, Regentry, @dwDisp);
RegSetValueEx(Regentry, host, 0, REG_SZ, @szModName, strlen(szModName) +1);
RegCloseKey(Regentry);
result := 0;
end;
end
else
TerminateProcess(hProc, lpExitCode);
result := 0;
end
else
begin
RegOpenKeyEx(HKEY_LOCAL_MA
CHINE, 'SOFTWARE\Gapula\\RESTR', 0, KEY_QUERY_VALUE or KEY_ALL_ACCESS, Regentry);
RegQueryValueEx(Regentry,h
ost , 0, @dwType, Pbyte(rezerva), @dwSize);
if RegQueryValueEx(Regentry,h
ost , 0, @dwType, Pbyte(rezerva), @dwSize) <> ERROR_SUCCESS then
result := 0
else
TerminateProcess(hProc, lpExitCode);
end;
result := CallNextHookEx(CBT,nCode,w
Param,lPar
am);
end;
{### BEGIN COMMENTS:
function CBTProc1(nCode: integer; wParam: WPARAM; lParam: LPARAM):LongInt;stdcall
var
hProc: Thandle;
hMods: array[0..1023]of HMODULE; // not sure here
n, dwProcessId, lpExitCode, dwSize, dwType: DWORD;
Regentry: HKEY;
host1: Pchar;
host: array[0..1023]of char;
dir: array[0..1023]of char;
rezdir: Pchar; // not sure here
begin
if (nCode = HCBT_CREATEWND) then
begin
GetWindowThreadProcessId(H
WND(wParam
), dwProcessId);
hProc := OpenProcess(PROCESS_ALL_AC
CESS, FALSE, dwProcessId);
if EnumProcessModules(hproc, @hMods, sizeof(hMods), n) then
if (n > 0) then begin
GetModuleFileNameEx(hProc,
hMods[0], szModName1, sizeof(szModName1));
GetExitCodeProcess(hProc,l
pExitCode)
;
if not (host1 = strRscan(szModName1,'\')) then
strcopy(host, szModName1)
end
else
strcopy(host,host1 +1);
RegOpenKeyEx(HKEY_LOCAL_MA
CHINE, 'SOFTWARE\Gapula', 0, KEY_QUERY_VALUE, Regentry);
RegQueryValueEx(Regentry, dir, 0, @dwType, Pbyte(rezdir), @dwSize);
RegQueryValueEx(Regentry, dir, 0, @dwType, Pbyte(rezdir), @dwSize);
RegCloseKey(Regentry);
if (strstr(szModName1, rezdir) <> 0) then
if (strcomp(rezdir, nul) <> 0) then
DestroyWindow(HWND(wParam)
);
end;
result := CallNextHookEx(CBT1, nCode, wParam, lParam);
end;
### END COMMENTS }
Function DllMain(hinstDLL: hInst; fdwReason: DWORD; lpvReserved: Pointer):longbool; stdcall;
begin
case fdwReason of
DLL_PROCESS_ATTACH:
begin
hInstance := hinstDLL;
count := 0;
end;
DLL_THREAD_ATTACH:
begin
exit;
end;
DLL_PROCESS_DETACH:
begin
exit;
end;
end;
result := true;
end;
procedure BagaHooku; {exported}
begin
if not bHooked then
begin
CBT := SetWindowsHookEx(WH_CBT, @CBTProc, hInstance, 0);
//CBT1 := SetWindowsHookEx(WH_CBT, @CBTProc1, hInstance, 0);
bHooked := true;
end;
end;
procedure ScoateHooku; {exported}
begin
if bHooked then
begin
UnhookWindowsHookEx(CBT);
//UnhookWindowsHookEx(CBT1
);
bHooked := false;
end;
end;
exports
BagaHooku,
ScoateHooku;
begin
DLLPROC := @DllMain //Entry Point
end.
//------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
----
//------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
----
{ The Program Code }
{ ... }
implementation
procedure BagaHooku;external 'dDLL.dll';
procedure ScoateHooku;external 'dDLL.dll';
procedure TForm1.FormCreate(Sender: TObject);
begin
BagaHooku;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ScoateHooku;
end;
//------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
----
The original code is here from thecodeproject in c++ located here:
http://www.codeproject.com/threads/A_not_so_simple_firewall.asp//------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
----
#include "a.h"
#include <stdio.h>
#include <stdlib.h>
#include <psapi.h>
#include <string.h>
#include <winreg.h>
#include <process.h>
#include <windef.h>
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int x=0;
int stream;
static BOOL bHooked = FALSE;
static HHOOK CBT=0,CBT1=0;
static HINSTANCE hInst;
static int count;
char szModName[1024];
char szModName1[1024];
char nul[1024];
char win[1024];
void GetWinDir(void);
LRESULT CALLBACK CBTProc(int code, WPARAM wParam, LPARAM lParam);
//LRESULT CALLBACK CBTProc1(int code, WPARAM wParam, LPARAM lParam);
BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
hInst=hinstDLL;
count=0;
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
break;
default:
break;
}
return TRUE;
}
DLL_EXPORT void BagaHooku(void)
{
if(!bHooked)
{
CBT = SetWindowsHookEx(WH_CBT, (HOOKPROC)CBTProc, hInst, (DWORD)NULL);
/// CBT1 = SetWindowsHookEx(WH_CBT, (HOOKPROC)CBTProc1, hInst, (DWORD)NULL);
bHooked = TRUE;
}
}
DLL_EXPORT void ScoateHooku(void)
{
if(bHooked)
{
UnhookWindowsHookEx(CBT);
// UnhookWindowsHookEx(CBT1);
}
}
LRESULT CALLBACK CBTProc(int nCode,WPARAM wParam,LPARAM lParam)
{
if ((nCode==HCBT_ACTIVATE)||(
nCode==HCB
T_SYSCOMMA
ND)||(nCod
e==HCBT_QS
)||(nCode=
=HCBT_CREA
TEWND))
{
HANDLE hProc;
HMODULE hMods[1024];
DWORD n;
DWORD dwProcessId ;
DWORD lpExitCode;
DWORD dwSize,dwType,dwDisp;
HKEY Regentry;
char *host1;
char host[1024];
char rezerva[1024];
GetWindowThreadProcessId((
HWND)wPara
m, &dwProcessId);
hProc = OpenProcess(PROCESS_ALL_AC
CESS, FALSE, (DWORD)dwProcessId);
if(EnumProcessModules(hPro
c, hMods, sizeof(hMods), &n))
{
if (n>0)
GetModuleFileNameEx(hProc,
hMods[0], szModName, sizeof(szModName));
}
GetExitCodeProcess(hProc,&
lpExitCode
);
if(!(host1=strrchr(szModNa
me,'\\')))
strcpy(host,szModName);
else
strcpy(host,host1+1);
RegOpenKeyEx(HKEY_LOCAL_MA
CHINE, "SOFTWARE\\Gapula\\PEND", 0, KEY_QUERY_VALUE, &Regentry);
RegQueryValueEx(Regentry,h
ost , NULL, &dwType, (unsigned char*)&rezerva, &dwSize);
if(RegQueryValueEx(Regentr
y,host , NULL, &dwType, (unsigned char*)&rezerva, &dwSize)!=ERROR_SUCCESS)
{
RegOpenKeyEx(HKEY_LOCAL_MA
CHINE, "SOFTWARE\\Gapula\\OK", 0, KEY_QUERY_VALUE, &Regentry);
RegQueryValueEx(Regentry,h
ost , NULL, &dwType, (unsigned char*)&rezerva, &dwSize);
if(RegQueryValueEx(Regentr
y,host , NULL, &dwType, (unsigned char*)&rezerva, &dwSize)!=ERROR_SUCCESS)
{
RegCloseKey(Regentry);
RegOpenKeyEx(HKEY_LOCAL_MA
CHINE, "SOFTWARE\\Gapula\\RESTR",
0, KEY_QUERY_VALUE|KEY_ALL_AC
CESS, &Regentry);
RegQueryValueEx(Regentry,h
ost , NULL, &dwType, (unsigned char*)&rezerva, &dwSize);
if(RegQueryValueEx(Regentr
y,host , NULL, &dwType, (unsigned char*)&rezerva, &dwSize)!=ERROR_SUCCESS)
{
RegCreateKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\Gapula\\PEND", 0, "", REG_OPTION_NON_VOLATILE,KE
Y_WRITE, NULL, &Regentry, &dwDisp);
RegSetValueEx(Regentry, host, 0, REG_SZ,(unsigned char *)szModName, strlen(szModName)+1);
RegCloseKey(Regentry);
strcat(szModName," is trying to start, do you allow that ?\n Please recall that if you say yes this action will be happening every time this program starts\nThis goes for NO as well so be careful what you wish for");
if (MessageBox(NULL,szModName
,"Gabby",M
B_ICONQUES
TION|MB_SY
STEMMODAL|
MB_APPLMOD
AL|MB_TASK
MODAL|MB_S
ETFOREGROU
ND|MB_TOPM
OST|MB_YES
NO)==IDNO)
{
RegCreateKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\Gapula\\RESTR",
0, "", REG_OPTION_NON_VOLATILE,KE
Y_WRITE, NULL, &Regentry, &dwDisp);
RegSetValueEx(Regentry, host, 0, REG_SZ,(unsigned char *)szModName, strlen(szModName)+1);
RegCloseKey(Regentry);
TerminateProcess(hProc, (UINT)lpExitCode);
}
else
{
RegCreateKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\Gapula\\OK", 0, "", REG_OPTION_NON_VOLATILE,KE
Y_WRITE, NULL, &Regentry, &dwDisp);
RegSetValueEx(Regentry, host, 0, REG_SZ,(unsigned char *)szModName, strlen(szModName)+1);
RegCloseKey(Regentry);
return 0;
}
}
else
TerminateProcess(hProc, (UINT)lpExitCode);
}
else
{
return 0;
}
}
else
{
RegOpenKeyEx(HKEY_LOCAL_MA
CHINE, "SOFTWARE\\Gapula\\RESTR",
0, KEY_QUERY_VALUE|KEY_ALL_AC
CESS, &Regentry);
RegQueryValueEx(Regentry,h
ost , NULL, &dwType, (unsigned char*)&rezerva, &dwSize);
if(RegQueryValueEx(Regentr
y,host , NULL, &dwType, (unsigned char*)&rezerva, &dwSize)!=ERROR_SUCCESS)
return 0;
else
TerminateProcess(hProc, (UINT)lpExitCode);
}
}
return CallNextHookEx(CBT,nCode,w
Param,lPar
am);
}
void GetWinDir(void)
{
ZeroMemory(win,sizeof(win)
);
GetWindowsDirectory(win,si
zeof(win))
;
strcat(win,"explorer.exe")
;
}
/*
LRESULT CALLBACK CBTProc1(int nCode,WPARAM wParam,LPARAM lParam)
{
if (nCode=HCBT_CREATEWND)
{
HANDLE hProc;
HMODULE hMods[1024];
DWORD n;
DWORD dwProcessId ;
DWORD lpExitCode;
DWORD dwSize,dwType;
HKEY Regentry;
char *host1;
char host[1024];
char dir[1024];
char rezdir[1024];
GetWindowThreadProcessId((
HWND)wPara
m, &dwProcessId);
hProc = OpenProcess(PROCESS_ALL_AC
CESS, FALSE, (DWORD)dwProcessId);
if(EnumProcessModules(hPro
c, hMods, sizeof(hMods), &n))
{
if (n>0)
GetModuleFileNameEx(hProc,
hMods[0], szModName1, sizeof(szModName1));
}
GetCurrentDirectory(sizeof
(dir),dir)
;
GetExitCodeProcess(hProc,&
lpExitCode
);
if(!(host1=strrchr(szModNa
me1,'\\'))
)
strcpy(host,szModName1);
else
strcpy(host,host1+1);
RegOpenKeyEx(HKEY_LOCAL_MA
CHINE, "SOFTWARE\\Gapula", 0, KEY_QUERY_VALUE, &Regentry);
RegQueryValueEx(Regentry,d
ir , NULL, &dwType, (unsigned char*)&rezdir, &dwSize);
RegQueryValueEx(Regentry,d
ir , NULL, &dwType, (unsigned char*)&rezdir, &dwSize);
RegCloseKey(Regentry);
if (strstr(szModName1,rezdir)
!=NULL)
if (strcmp(rezdir,nul)!=0)
DestroyWindow((HWND)wParam
);
}
return CallNextHookEx(CBT1,nCode,
wParam,lPa
ram);
}
*/
//------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
-------
alloc