积极答复者
如何使用Windows 服务设置桌面背景图片

问题
答案
-
谢谢提醒,现在问题已经解决,解决的方式是
1、允许服务与桌面应用交互
2、引用的第三方的DLL(Cjwdev.WindowsApi)
主要代码如下:
1、允许服务与桌面应用交互
private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
//允许服务和用户界面交互
ConnectionOptions coOptions = new ConnectionOptions();
coOptions.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope mgmtScope = new System.Management.ManagementScope(@"root\CIMV2", coOptions);
mgmtScope.Connect();
ManagementObject wmiService;
wmiService = new ManagementObject("Win32_Service.Name='" + this.serviceInstaller1.ServiceName + "'");
ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
InParam["DesktopInteract"] = true;
ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);
}2、引用第三方动态库(Cjwdev.WindowsApi.dll),在服务启动后调用我的应用程序,更改桌面代码的逻辑放置在我的应用程序中。
protected override void OnStart(string[] args)
{//我的应用程序路径
string appStartPath = @"C:\\SV\\ChangeImageWinFormUI.exe";
IntPtr userTokenHandle = IntPtr.Zero;
ApiDefinitions.WTSQueryUserToken(ApiDefinitions.WTSGetActiveConsoleSessionId(), ref userTokenHandle);
ApiDefinitions.PROCESS_INFORMATION procInfo = new ApiDefinitions.PROCESS_INFORMATION();
ApiDefinitions.STARTUPINFO startInfo = new ApiDefinitions.STARTUPINFO();
startInfo.cb = (uint)Marshal.SizeOf(startInfo);
ApiDefinitions.CreateProcessAsUser(userTokenHandle, appStartPath, "", IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref startInfo, out procInfo);
if (userTokenHandle != IntPtr.Zero)
ApiDefinitions.CloseHandle(userTokenHandle);
int _currentAquariusProcessId = (int)procInfo.dwProcessId;}
- 已标记为答案 xbxc99 2016年5月17日 1:30
全部回复
-
谢谢,问题已经解决了,不过除了这一项之外还用到了一个第三方的动态库 Cjwdev.WindowsApi.dll
通过下面的代码,设置服务安装完之后,勾选允许服务于桌面交互
private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
//允许服务和用户界面交互
ConnectionOptions coOptions = new ConnectionOptions();
coOptions.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope mgmtScope = new System.Management.ManagementScope(@"root\CIMV2", coOptions);
mgmtScope.Connect();
ManagementObject wmiService;
wmiService = new ManagementObject("Win32_Service.Name='" + this.serviceInstaller1.ServiceName + "'");
ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
InParam["DesktopInteract"] = true;
ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);
} -
谢谢提醒,现在问题已经解决,解决的方式是
1、允许服务与桌面应用交互
2、引用的第三方的DLL(Cjwdev.WindowsApi)
主要代码如下:
1、允许服务与桌面应用交互
private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
//允许服务和用户界面交互
ConnectionOptions coOptions = new ConnectionOptions();
coOptions.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope mgmtScope = new System.Management.ManagementScope(@"root\CIMV2", coOptions);
mgmtScope.Connect();
ManagementObject wmiService;
wmiService = new ManagementObject("Win32_Service.Name='" + this.serviceInstaller1.ServiceName + "'");
ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
InParam["DesktopInteract"] = true;
ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);
}2、引用第三方动态库(Cjwdev.WindowsApi.dll),在服务启动后调用我的应用程序,更改桌面代码的逻辑放置在我的应用程序中。
protected override void OnStart(string[] args)
{//我的应用程序路径
string appStartPath = @"C:\\SV\\ChangeImageWinFormUI.exe";
IntPtr userTokenHandle = IntPtr.Zero;
ApiDefinitions.WTSQueryUserToken(ApiDefinitions.WTSGetActiveConsoleSessionId(), ref userTokenHandle);
ApiDefinitions.PROCESS_INFORMATION procInfo = new ApiDefinitions.PROCESS_INFORMATION();
ApiDefinitions.STARTUPINFO startInfo = new ApiDefinitions.STARTUPINFO();
startInfo.cb = (uint)Marshal.SizeOf(startInfo);
ApiDefinitions.CreateProcessAsUser(userTokenHandle, appStartPath, "", IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref startInfo, out procInfo);
if (userTokenHandle != IntPtr.Zero)
ApiDefinitions.CloseHandle(userTokenHandle);
int _currentAquariusProcessId = (int)procInfo.dwProcessId;}
- 已标记为答案 xbxc99 2016年5月17日 1:30