//取得本地IP地址(精简版) //注:使用函数前需要 WSAStartup($202, wsdata); function GetLocalIP(): String; var HostName: array[0..255] of Char; HostEnt: PHostEnt; begin Result := ”; if gethostname(HostName, 255) = 0 then begin HostEnt := gethostbyname(HostName); Result := StrPas(inet_ntoa(PInAddr(PInAddr(HostEnt^.h_addr_list)^)^)); end; end;
// 首先加入.RC文件,写上 MyDLL DAT testDLL.dll // 然后程序里 ExtractRes(‘DAT’,'MyDLL’,'123DLL.dll’); procedure ExtractRes(ResType, ResName, ResNewName:String); var Res:TResourceStream; begin Res:=TResourceStream.Create(Hinstance, Resname, Pchar(ResType)); try Res.SavetoFile(‘.\’+ResNewName); // 释放到当前目录 finally Res.Free; end; end;
6.延时
// 模仿VB里DoEvents的延时 procedure Delay(const uDelay: DWORD); var n: DWORD; begin n := GetTickCount; while ( (GetTickCount – n) <= uDelay ) do Application.ProcessMessages; end;
7.标准C中的 itoa() 函数Delphi版,将Int型变量转化为(radix)进制输出
//二进制 itoa(i, 2); //八进制 itoa(i, 8); //十六进制 itoa(i, 16); function itoa(aData, radix: Integer): String; var t: Integer; begin Result := ”; repeat t := aData mod radix; if t < 10 then Result := InttoStr(t)+Result else Result := InttoHex(t, 1)+Result; aData := aData div radix; until (aData = 0); end;
{ Automatically start a service after using /Install or /Uniinstall switch In the service unit add these statements before the ‘end.’ statement. To automatically start or stop the service during install or uninstall Tested only on Win2k and WinXP Change the ‘myservice’ in both WinExec statements to your own service name. } // 在Service的初始化和结束部分加入如下代码: initialization if (ParamCount >= 1) and (CompareText(‘/uninstall’,ParamStr(1))=0) then begin // 如果是卸载,先停止服务。注意修改 myservice 为你的服务名 WinExec(‘cmd.exe /c net stop myservice‘, sw_hide); sleep(3000); end; finalization if (ParamCount >= 1) and (CompareText(‘/Install’,ParamStr(1))=0) then // 用 net start 执行服务 WinExec(‘cmd.exe /c net start myservice‘,sw_hide); end.
10.给自己编写的服务程序添加描述
没有描述的服务看起来要多可疑有多可疑,仿照微软的写法给你的服务加个描述吧:
procedure TMyService1.ServiceAfterInstall(Sender: TService); var MyReg: TRegistry; begin MyReg := TRegistry.Create; try with MyReg do begin RootKey := HKEY_LOCAL_MACHINE; { Service1是服务的名字,注意修改成你自己的 } if Openkey(‘SYSTEM\CurrentControlSet\Services\Service1′, true) then WriteString(‘Description’, ‘你自己的服务描述…’); CloseKey; end; finally MyReg.Free; end; end;