C# Windwos服务脱离.Net Framework

解决方案:

1.将C#windows服务改成无窗台的Winform

2.C#的Winform脱离.Net Framework(网上有很多资料,这不讲了)

2.用Delphi编写Windows服务,服务启动时启动C#的Winform程序

3.设置Windows服务允许服务与桌面交互

 

部分代码:
1.获取服务文件路径:

 

ContractedBlock.gif ExpandedBlockStart.gif Delphi 获取服务文件路径

   
    function
     TBootCssService122.GetServiceAppPath: String;
Var
Res : Longint;
CurSize : Longint;
begin
CurSize :
= 1024 ;
SetLength(Result, CurSize);
Res :
= GetModuleFilename(GetModuleHandle( nil ), PChar(Result), CurSize);
If (res
curSize) then
begin
CurSize :
= res + 10 ;
SetLength(Result, CurSize);
Res :
= GetModuleFilename(GetModuleHandle( nil ), PChar(Result), CurSize);
end ;
Setlength(Result, Res);
Result :
= ExtractFileDir(Result) + ' \ ' ;
end ;

2.启动C#程序


  
   WinExec(PChar(GetServiceAppPath
   +
   '
   VM\VMDoNet.exe 
   '
   
+ GetServiceAppPath + ' winform文件名 ' ), SW_SHOW);

3.停止C#程序

 

ContractedBlock.gif ExpandedBlockStart.gif Delphi 杀死进程

   
    function
     KillTask(ExeFileName: string): integer;
const
PROCESS_TERMINATE
= $ 0001 ;
var
ContinueLoop: BOOLean;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
Result :
= 0 ;
FSnapshotHandle :
= CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0 );
FProcessEntry32.dwSize :
= SizeOf(FProcessEntry32);
ContinueLoop :
= Process32First(FSnapshotHandle, FProcessEntry32);

while Integer(ContinueLoop) 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName))
or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName)))
then
Result :
= Integer(TerminateProcess(
OpenProcess(PROCESS_TERMINATE,
BOOL(
0 ),
FProcessEntry32.th32ProcessID),
0 ));
ContinueLoop :
= Process32Next(FSnapshotHandle, FProcessEntry32);
end ;
CloseHandle(FSnapshotHandle);
end ;

 


  
   procedure
    TBootCssService122.ServiceStop(Sender: TService;
var Stopped: Boolean);
begin
// 杀进程,停止程序
KillTask(
' C#程序 ' ); // 调用函数,杀你的程序abc.exe
Stopped :
= True;
end ;

 

 

转载于:https://www.cnblogs.com/ruanzb/archive/2010/08/06/1794353.html

最新回复(0)
/jishumWdv0itULVMrFnaosbkFagNwOEP3xR0mgHer9w_3D_3D4488858
8