.NET(C#)实现桌面背景切换(控制台应用程序,windows服务版的未实现成功)
2021-05-19 20:28
标签:cat aging str fit stop 系统 pcr live protected AdvancedBackgroundJimmy.Program.cs ProjectInstaller.cs AdvancedBackgroundServiceJimmy.cs 完成. .NET(C#)实现桌面背景切换(控制台应用程序,windows服务版的未实现成功) 标签:cat aging str fit stop 系统 pcr live protected 原文地址:https://www.cnblogs.com/JimmySeraph/p/11696303.htmlusing AdvancedBackground;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.ServiceProcess;
using System.Threading.Tasks;
using System.Timers;
namespace AdvancedBackgroundJimmy
{
[System.Runtime.Remoting.Contexts.Synchronization]
static class Program
{
const int SPI_GETDESKWALLPAPER = 0x73;
const int MAX_PATH = 260;
const int SPI_SETDESKWALLPAPER = 0x14;
const int SPIF_UPDATEINIFILE = 0x01;
const int SPIF_SENDININICHANGE = 0x02;
static string photoFileDirPath = @"g:/Photos/Camera/";
static DirectoryInfo photoDir = new DirectoryInfo(photoFileDirPath);
static FileInfo[] photoFileArr = photoDir.GetFiles();
static int PHOTO_PER_WAIT = 3000;
static string log_path = @"g:/TestLog/AdvancedBackgroundJimmy/log.txt";
static FileInfo logFileInfo = new FileInfo(log_path);
static string log = null;
///
using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Management;
namespace AdvancedBackgroundJimmy
{
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
protected override void OnCommitted(System.Collections.IDictionary savedState)
{
base.OnCommitted(savedState);
//将服务更改为允许桌面交互模式
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);
}
}
}
using AdvancedBackground;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace AdvancedBackgroundJimmy
{
partial class AdvancedBackgroundServiceJimmy : ServiceBase
{
static String log_path = @"g:/TestLog/AdvancedBackgroundJimmy/log.txt";
static FileInfo logFileInfo = new FileInfo(log_path);
static String log = null;
static Thread thread = null;
[DllImport("user32.dll")]
static extern int GetDesktopWindow();
[DllImport("user32.dll")]
static extern IntPtr GetProcessWindowStation();
[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentThreadId();
[DllImport("user32.dll")]
static extern IntPtr GetThreadDesktop(IntPtr dwThread);
[DllImport("user32.dll")]
static extern IntPtr OpenWindowStation(string a, bool b, int c);
[DllImport("user32.dll")]
static extern IntPtr OpenDesktop(string lpszDesktop, uint dwFlags,
bool fInherit, uint dwDesiredAccess);
[DllImport("user32.dll")]
static extern IntPtr CloseDesktop(IntPtr p);
[DllImport("rpcrt4.dll", SetLastError = true)]
static extern IntPtr RpcImpersonateClient(int i);
[DllImport("rpcrt4.dll", SetLastError = true)]
static extern IntPtr RpcRevertToSelf();
[DllImport("user32.dll")]
static extern IntPtr SetThreadDesktop(IntPtr a);
[DllImport("user32.dll")]
static extern IntPtr SetProcessWindowStation(IntPtr a);
[DllImport("user32.dll")]
static extern IntPtr CloseWindowStation(IntPtr a);
public AdvancedBackgroundServiceJimmy()
{
InitializeComponent();
if (!logFileInfo.Exists)
{
logFileInfo.CreateText();
}
}
protected override void OnStart(string[] args)
{
if (!logFileInfo.Exists)
{
logFileInfo.CreateText();
}
log = "进入OnStart方法!" + System.Environment.NewLine;
File.AppendAllText(log_path, log);
ThreadStart threadStart = new ThreadStart(delegate()
{
try
{
if (!logFileInfo.Exists)
{
logFileInfo.CreateText();
}
GetDesktopWindow();
IntPtr hwinstaSave = GetProcessWindowStation();
IntPtr dwThreadId = GetCurrentThreadId();
IntPtr hdeskSave = GetThreadDesktop(dwThreadId);
IntPtr hwinstaUser = OpenWindowStation("WinSta0", false, 33554432);
if (hwinstaUser == IntPtr.Zero)
{
RpcRevertToSelf();
return;
}
SetProcessWindowStation(hwinstaUser);
IntPtr hdeskUser = OpenDesktop("Default", 0, false, 33554432);
RpcRevertToSelf();
if (hdeskUser == IntPtr.Zero)
{
SetProcessWindowStation(hwinstaSave);
CloseWindowStation(hwinstaUser);
return;
}
SetThreadDesktop(hdeskUser);
IntPtr dwGuiThreadId = dwThreadId;
AdvancedBackgroundJimmy.Program.AutoPhoto();
dwGuiThreadId = IntPtr.Zero;
SetThreadDesktop(hdeskSave);
SetProcessWindowStation(hwinstaSave);
CloseDesktop(hdeskUser);
CloseWindowStation(hwinstaUser);
}
catch (Exception e)
{
log = e.Message + System.Environment.NewLine;
File.AppendAllText(log_path, log);
}
});
thread = new Thread(threadStart);
thread.IsBackground = true;
thread.Start();
//AdvancedBackgroundJimmy.Program.AutoPhoto();
}
protected override void OnStop()
{
if(thread != null)
{
if (thread.IsAlive)
{
thread.Abort();
thread = null;
}
}
}
}
}
文章标题:.NET(C#)实现桌面背景切换(控制台应用程序,windows服务版的未实现成功)
文章链接:http://soscw.com/index.php/essay/87752.html