安装程序我也改了,不过很不好意思,很乱,自己看吧:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Buttons, ComCtrls, JwaWS2spi,JwaWinsock2;
type
TForm1 = class(TForm)
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
Panel2: TPanel;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
Panel1: TPanel;
t1: TLabeledEdit;
p1: TLabeledEdit;
m1: TLabeledEdit;
Panel3: TPanel;
BitBtn3: TBitBtn;
BitBtn4: TBitBtn;
Panel4: TPanel;
spiname: TLabeledEdit;
BitBtn5: TBitBtn;
Image1: TImage;
Image2: TImage;
Image3: TImage;
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
procedure BitBtn4Click(Sender: TObject);
procedure BitBtn5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
m_sPathName:string;
const
REG_INSTALL_PATH_ITEM='PathName';
REG_INSTALL_KEY='SYSTEM/CurrentControlSet/Services/WinSock2/SockEyeS';
REG_PROTOCOL_CATALOG_KEY='SYSTEM/CurrentControlSet/Services/WinSock2/Parameters/Protocol_Catalog9/Catalog_Entries';
REG_PROTOCOL_CATALOG_ITEM='PackedCatalogItem';
myreg_key='SOFTWARE/SockEyeS';
MAX_PATH=260;
MAX_PROTOCOL_CATALOG_LENTH=sizeof(WSAPROTOCOL_INFOW) + MAX_PATH;
implementation
{$R *.dfm}
function readreg(sKey:string;var pBuffer:string;dwBufSize:dword;key:hkey;sSubKey:string;ulType:dword):boolean;
var
sTemp
char;
hSubKey: hkey;
Datatype:dword;
begin
result:=false;
if RegOpenKeyEx(key,pchar(sSubkey),0,KEY_ALL_ACCESS,hSubKey)<>0 then
begin
exit;
end;
try
getmem(sTemp,dwBufSize);
if (RegQueryValueEx(hSubKey,pchar(sKey),nil,@Datatype,pbyte(sTemp),@dwBufSize)=0)and(DataType = ulType) then
begin
pBuffer:=sTemp;
result:=true;
end;
finally
RegCloseKey(hSubKey);
freemem(sTemp);
end;
end;
function savereg(sKey:string;pBuffer:string;dwBufSize:dword;key:hkey;sSubKey:string;ulType:dword):boolean;
var
hSubKey: hkey;
begin
result:=false;
if RegOpenKeyEx(key,pchar(sSubKey),0,KEY_ALL_ACCESS,hSubKey)<>0 then
begin
if RegCreateKey(key,pchar(sSubKey),hSubKey)<>0 then
exit;
end;
try
if RegSetValueEx(hSubKey,pchar(sKey),0,ulType,pbyte(pchar(pBuffer)),dwBufSize)=0 then
begin
result:=true;
end;
finally
RegCloseKey(hSubKey);
end;
end;
function deletereg(key:hkey;sSubKey:string;sItem:string):boolean;
var
hSubKey:hkey;
begin
result:=false;
if (key=0)or(sSubKey='') then
exit;
if sItem='' then
begin
if RegDeleteKey(key,pchar(sSubKey))=0 then
begin
result:=true;
exit;
end
else
exit;
end;
if RegOpenKeyEx(key,pchar(sSubkey),0,KEY_ALL_ACCESS,hSubKey)<>0 then
begin
exit;
end;
try
if RegDeleteValue(hSubKey, pchar(sItem))=0 then
result:=true;
finally
RegCloseKey(hSubKey);
end;
end;
function GetTarget(var Targets:string):boolean;
var
sTemp
char;
hSubKey: hkey;
Datatype:integer;
ulDateLenth: DWORD;
begin
result:=false;
targets:='';
if RegOpenKeyEx(HKEY_LOCAL_MACHINE,pchar(myreg_key),0,KEY_ALL_ACCESS,hSubKey)<>0 then
begin
exit;
end;
ulDateLenth:=MAX_PATH;
getmem(sTemp,MAX_PATH);
try
if RegQueryValueEx(hSubKey,pchar('Target'),nil,@Datatype,pbyte(sTemp),@ulDateLenth)=0 then
begin
targets:=stemp;
result:=true;
end;
RegCloseKey(hSubKey);
finally
freemem(sTemp);
end;
end;
function SetTarget(Targets:string):boolean;
var
sTemp
char;
hSubKey: hkey;
Datatype:integer;
len: DWORD;
begin
result:=false;
if RegOpenKeyEx(HKEY_LOCAL_MACHINE,pchar(myreg_key),0,KEY_ALL_ACCESS,hSubKey)<>0 then
begin
if RegCreateKey(HKEY_LOCAL_MACHINE,pchar(myreg_key),hSubKey)<>0 then
exit;
end;
len:=length(Targets)+1;
getmem(sTemp,len);
strcopy(stemp,pchar(targets));
Datatype:=REG_SZ;
try
if RegSetValueEx(hSubKey,pchar('Target'),0,Datatype,pbyte(sTemp),len)=0 then
begin
result:=true;
end;
RegCloseKey(hSubKey);
finally
freemem(sTemp);
end;
end;
function SetPlugin(Plugin:string):boolean;
var
sTemp
char;
hSubKey: hkey;
Datatype:integer;
len: DWORD;
begin
result:=false;
if RegOpenKeyEx(HKEY_LOCAL_MACHINE,pchar(myreg_key),0,KEY_ALL_ACCESS,hSubKey)<>0 then
begin
if RegCreateKey(HKEY_LOCAL_MACHINE,pchar(myreg_key),hSubKey)<>0 then
exit;
end;
len:=length(plugin)+1;
getmem(sTemp,len);
strcopy(stemp,pchar(plugin));
Datatype:=REG_SZ;
try
if RegSetValueEx(hSubKey,pchar('Plugin'),0,Datatype,pbyte(sTemp),len)=0 then
begin
result:=true;
end;
RegCloseKey(hSubKey);
finally
freemem(sTemp);
end;
end;
function GetPlugin(var Plugin:string):boolean;
var
sTemp
char;
hSubKey: hkey;
Datatype:integer;
ulDateLenth: DWORD;
begin
result:=false;
plugin:='';
if RegOpenKeyEx(HKEY_LOCAL_MACHINE,pchar(myreg_key),0,KEY_ALL_ACCESS,hSubKey)<>0 then
begin
exit;
end;
ulDateLenth:=MAX_PATH;
getmem(sTemp,MAX_PATH);
try
if RegQueryValueEx(hSubKey,pchar('Plugin'),nil,@Datatype,pbyte(sTemp),@ulDateLenth)=0 then
begin
Plugin:=stemp;
result:=true;
end;
RegCloseKey(hSubKey);
finally
freemem(sTemp);
end;
end;
function SetMir(Mir:string):boolean;
var
sTemp
char;
hSubKey: hkey;
Datatype:integer;
len: DWORD;
begin
result:=false;
if RegOpenKeyEx(HKEY_LOCAL_MACHINE,pchar(myreg_key),0,KEY_ALL_ACCESS,hSubKey)<>0 then
begin
if RegCreateKey(HKEY_LOCAL_MACHINE,pchar(myreg_key),hSubKey)<>0 then
exit;
end;
len:=length(mir)+1;
getmem(sTemp,len);
strcopy(stemp,pchar(mir));
Datatype:=REG_SZ;
try
if RegSetValueEx(hSubKey,pchar('Mir'),0,Datatype,pbyte(sTemp),len)=0 then
begin
result:=true;
end;
RegCloseKey(hSubKey);
finally
freemem(sTemp);
end;
end;
function GetMir(var Mir:string):boolean;
var
sTemp
char;
hSubKey: hkey;
Datatype:integer;
ulDateLenth: DWORD;
begin
result:=false;
Mir:='';
if RegOpenKeyEx(HKEY_LOCAL_MACHINE,pchar(myreg_key),0,KEY_ALL_ACCESS,hSubKey)<>0 then
begin
exit;
end;
ulDateLenth:=MAX_PATH;
getmem(sTemp,MAX_PATH);
try
if RegQueryValueEx(hSubKey,pchar('Mir'),nil,@Datatype,pbyte(sTemp),@ulDateLenth)=0 then
begin
Mir:=stemp;
result:=true;
end;
RegCloseKey(hSubKey);
finally
freemem(sTemp);
end;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
tmp:string;
begin
gettarget(tmp);
t1.Text:=tmp;
getplugin(tmp);
p1.Text:=tmp;
getmir(tmp);
m1.Text:=tmp;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
var
tmp:string;
begin
tmp:=t1.Text;
settarget(tmp);
tmp:=p1.Text;
setplugin(tmp);
tmp:=m1.Text;
setmir(tmp);
end;
function isinstalled(spiPathName:string):boolean;
var
tmp:string;
begin
result:=false;
try
if ReadReg( REG_INSTALL_PATH_ITEM,
Tmp,
MAX_PATH,
HKEY_LOCAL_MACHINE,
REG_INSTALL_KEY, REG_SZ
) then
begin
if tmp<>'' then
result:=true;
end;
finally
end;
end;
function SaveHookKey(key:hkey;sSubKey
char;IsRemove:boolean):boolean;
var
hSubKey:hkey;
ItemValue,
sItem,
sProvider:string;
ItemSize:integer;
Datatype:dword;
mProtocolInfo
WsaProtocolInfoW;
begin
result:=false;
ItemSize:=MAX_PROTOCOL_CATALOG_LENTH;
if RegOpenKeyEx(key, sSubKey, 0, KEY_ALL_ACCESS, hSubKey)<>0 then
begin
exit;
end;
try
setlength(ItemValue,MAX_PROTOCOL_CATALOG_LENTH);
if(RegQueryValueEx(hSubKey, REG_PROTOCOL_CATALOG_ITEM
, nil,@Datatype, pbyte(pchar(ItemValue)), @ItemSize)<>0)or
(ItemSize <> MAX_PROTOCOL_CATALOG_LENTH) then
begin
exit;
end;
mProtocolInfo:=PWsaProtocolInfoW(pchar(ItemValue)+MAX_PATH);
if (mProtocolInfo.ProtocolChain.ChainLen = 1)and
(mProtocolInfo.iAddressFamily = AF_INET) then
begin
sItem:=inttostr(mProtocolInfo.dwCatalogEntryId);
if not isRemove then
begin
if not SaveReg(sItem, pchar(ItemValue), strlen(pchar(ItemValue)),
HKEY_LOCAL_MACHINE, REG_INSTALL_KEY, REG_SZ) then
begin
exit;
end;
strcopy(pchar(ItemValue), pchar(m_sPathName));
if RegSetValueEx(hSubKey, REG_PROTOCOL_CATALOG_ITEM
, 0, REG_BINARY, pchar(ItemValue), ItemSize)<>0 then
exit;
end
else
begin
if not ReadReg(sItem, sProvider, MAX_PATH,
HKEY_LOCAL_MACHINE, REG_INSTALL_KEY, REG_SZ) then
exit;
strcopy(pchar(ItemValue), pchar(sProvider));
if RegSetValueEx(hSubKey, REG_PROTOCOL_CATALOG_ITEM
, 0, REG_BINARY, pchar(ItemValue), ItemSize)<>0 then
exit;
end;
end;
finally
RegCloseKey(hSubKey);
end;
result:=true;
end;
function EnumHookKey(IsRemove:boolean):boolean;
var
skey:hkey;
ssubkey
char;
dwIndex:integer;
begin
result:=false;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE
, REG_PROTOCOL_CATALOG_KEY, 0, KEY_READ, skey) <>0) then
begin
exit;
end;
try
dwIndex:=0;
getmem(ssubkey,MAX_PATH);
while(RegEnumKey(skey, dwIndex, sSubKey, MAX_PATH) =0)do
begin
if not SaveHookKey(skey, sSubKey, IsRemove) then
begin
exit;
end;
inc(dwIndex);
end;
result:=true;
finally
freemem(sSubKey);
RegCloseKey(sKey);
end;
end;
function InstallProvider(spiPathName:string):integer;
begin
result:=123;
if isinstalled(spiPathName) then
begin
result:=1;
exit;
end;
m_sPathName:=spiPathName;
if not EnumHookKey(FALSE) then
begin
result:=2;
exit;
end;
if not SaveReg(REG_INSTALL_PATH_ITEM, spiPathName, length(spiPathName),
HKEY_LOCAL_MACHINE, REG_INSTALL_KEY, REG_SZ) then
begin
result:=3;
exit;
end;
result:=0;
end;
function RemoveProvider():integer;
begin
result:=123;
if not isInstalled('test') then
begin
result:=1;
exit;
end;
if not EnumHookKey(TRUE) then
begin
result:=2;
exit;
end;
if not deletereg(HKEY_LOCAL_MACHINE,REG_INSTALL_KEY,'') then
begin
result:=3;
exit;
end;
result:=0;
end;
procedure TForm1.BitBtn3Click(Sender: TObject);
var
err:integer;
begin
if spiname.Text<>'' then
err:=InstallProvider(spiname.Text);
case err of
0:
begin
showmessage('Install success!');
end;
1:
begin
showmessage('Already installed!');
end;
2:
begin
showmessage('Install error 2!');
end;
3:
begin
showmessage('Install error 3!');
end;
123:
begin
showmessage('Unknow error!');
end;
end;
end;
procedure TForm1.BitBtn4Click(Sender: TObject);
var
err:integer;
begin
err:=RemoveProvider();
case err of
0:
begin
showmessage('Uninstall success!');
end;
1:
begin
showmessage('Not install!');
end;
2:
begin
showmessage('UNinstall error 2!');
end;
3:
begin
showmessage('Uninstall error 3!');
end;
123:
begin
showmessage('Unknow error!');
end;
end;
end;
procedure TForm1.BitBtn5Click(Sender: TObject);
begin
if isInstalled('test') then
begin
showmessage('Already installed!');
end
else
begin
showmessage('Not install!');
end;
end;
end.