如何取得服务的运行程序名和运行路径?(100分)

X

xddl

Unregistered / Unconfirmed
GUEST, unregistred user!
windows2000的服务程序,在其属性面版中有“可执行文件路径”的信息,可是在
服务程序中怎么得到该路径?
 
是在服务控制程序中,还是在服务程序本身中,后者应该很容易,和普通程序一样。
 
主要想法是这样的,在服务程序中,想做一个LOG文件,把一些运行信息写进去,LOG文件
的文件名与服务程序的名字一样,扩展名为LOG,可是象普通应用程序一样使用Appliction
对象的ExeName属性不行,也没有找到合适的办法,Windows API也没有相关的调用。
 
怎么没有人回答?是否没有办法?
 
注册表中有相关服务的全路径和服务名.你regedit 搜索一下就晓得乐.
 
Not tested in Service,but i think it can work well in service project too.

procedure TForm1.Button1Click(Sender: TObject);
Var Names:array[0..1024] of char;
begin
getModuleFileName(GetModuleHandle(nil),@Names[0],1204);
showMessage(Names);
end;
 
function GetServicePath: string;
var Sc_MHandle, Sc_SHandle: SC_HANDLE;
s : array[0..MAX_PATH] of char;
iSize:DWORD;
Query_Config: PQueryServiceConfigA;
begin
Result:='';
Sc_MHandle := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if Sc_MHandle = 0 then
begin
Self.LogMessage('Error Open ScManager!');
exit;
end;

Sc_SHandle := OpenService(Sc_MHandle, Pchar('服务名'), SERVICE_QUERY_CONFIG);

if Sc_SHandle = 0 then
begin
CloseServiceHandle(Sc_MHandle);
Self.LogMessage('Error Open Service!!');

exit;
end;
getMem(Query_Config,4096);
try
if not QueryServiceConfig(Sc_SHandle, Query_Config, 4096, iSize) then
Abort;

StrCopy(s,Query_Config^.lpBinaryPathName);

Result := Extractfilepath(Trim(s));

finally
FreeMem(Query_Config,4096);
CloseServiceHandle(Sc_SHandle);
CloseServiceHandle(Sc_MHandle);
end;
end;

测试通过,记得use winsvc单元
 
不用这么麻烦吧?
LogFile:=ExtractFilePath(Application.ExeName)+LogFileName;
我就是这么做的。
我也正在做服务程序,可是碰到一点问题,网上这方面资料好少啊。
如果有同道中人不妨一起探讨探讨?
windfantasy@cableplus.com.cn
QQ:17791871 加我时写明:DELPHI服务程序。
 
顶部