在NT服务中,怎么样得到服务程序所在的路径?
解决方案 »
- System.Text.Encoding.GetEncoding("GB2312").GetString换delphi怎么写呢
- 在COM对象中 如何获取客户端信息?
- 有关字体大小的问题,谁能帮我解决一下,谢谢?
- Delphi & C++Builder一调试就死机,Visual Studio不会!!!
- 为何在频繁调用Sql server数据库的存储过程后,内存占用一直在增大,没有释放那?如何释放?
- 关于使用ado编程序,从oracle数据库到数据问题batchmove
- 用skinEngine的奇怪问题!
- 还是ADO连接数据库的问题
- 关于用DELPHI做程序启动的问题
- 求救,这是怎么回事。11
- 多图层PSD文件特定图层的读取替换问题
- 大家对天天免费加班有何看法?很郁闷发觉今天是星期六……
方法2:使用GetModuleFileName()函数
{
LPQUERY_SERVICE_CONFIG lpcnfg;
DWORD Size=0;
HANDLE SvcMgr;
int iLen=0;
HANDLE Svc;
AnsiString Result="";
SvcMgr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if(!SvcMgr)
RaiseLastOSError();
try
{
Svc=OpenService(SvcMgr,ServiceName.c_str(),SERVICE_ALL_ACCESS);
if(!Svc)RaiseLastOSError();
try
{ lpcnfg= (LPQUERY_SERVICE_CONFIG) LocalAlloc(LPTR, 4096);
if(!lpcnfg)return Result;
memset(lpcnfg,0,4096);
DWORD qResult=QueryServiceConfig(Svc,lpcnfg,4096,&Size);
if(!qResult)
return Result;
switch(qResult)
{
case ERROR_ACCESS_DENIED:
return "ERROR_ACCESS_DENIED";
case ERROR_INVALID_HANDLE:
return "ERROR_INVALID_HANDLE";
case ERROR_INSUFFICIENT_BUFFER:
LocalFree(lpcnfg);
LocalAlloc(LPTR,Size);
memset(lpcnfg,0,Size);
qResult=QueryServiceConfig(Svc,lpcnfg,Size,&Size);
}
iLen=strlen(lpcnfg->lpBinaryPathName);
Result.SetLength(iLen);
memcpy(Result.c_str(),lpcnfg->lpBinaryPathName,iLen); LocalFree(lpcnfg); }
__finally
{
CloseServiceHandle(Svc);
}
}
__finally
{
CloseServiceHandle(SvcMgr);
}
return Result;
}
SvcMgr,winsvc;
function GetServicePath(const ServiceName:string):string;
var
lpcnfg:PQueryServiceConfig;
nSize:DWORD;
SvcMgr:THandle;
iLen:Integer;
Svc:THANDLE;
qResult:boolean;
begin
Result:='';
SvcMgr := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if SvcMgr=0 then
RaiseLastOSError;
try
Svc:=OpenService(SvcMgr,PChar(ServiceName),SERVICE_ALL_ACCESS);
if Svc=0 then RaiseLastOSError;
try lpcnfg:= PQueryServiceConfig( LocalAlloc(LPTR, 4096));
if not Assigned(lpcnfg) then exit;
ZeroMemory(lpcnfg,4096);
nSize:=4096;
qResult:=QueryServiceConfig(Svc,lpcnfg,4096,nSize);
if not qResult then exit;
iLen:=Length(lpcnfg.lpBinaryPathName);
SetLength(Result,iLen);
CopyMemory(PChar(Result),PChar(lpcnfg.lpBinaryPathName),iLen); LocalFree(HLOCAL(lpcnfg)); finally
CloseServiceHandle(Svc);
end; finally
CloseServiceHandle(SvcMgr);
end;
end;