我的问题是 PostMessage Windows API 工作不正常,因为它在从控制台应用程序运行时工作。工作代码:我有 2 个应用程序 [1] 是控制台应用程序 [2] Windows 窗体应用程序。要求是我想向所有正在运行的应用程序实例发送消息。控制台应用程序代码:class Program{ #region Dll Imports public const int HWND_BROADCAST = 0xFFFF; [DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32")] public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam); [DllImport("user32")] public static extern int RegisterWindowMessage(string message); #endregion Dll Imports public static readonly int WM_ACTIVATEAPP = RegisterWindowMessage("CLOSE"); static void Main(string[] args) { //we tried to create a mutex, but there's already one (createdNew = false - another app created it before) //so there's another instance of this application running Process currentProcess = Process.GetCurrentProcess(); //get the process that has the same name as the current one but a different ID foreach (Process process in Process.GetProcessesByName("ClientApp1")) { if (process.Id != currentProcess.Id) { IntPtr handle = process.MainWindowHandle; //if the handle is non-zero then the main window is visible (but maybe somewhere in the background, that's the reason the user started a new instance) //so just bring the window to front //if (handle != IntPtr.Zero) } } }}上面的代码按预期工作。我的问题从这里开始。我想从 Windows 服务而不是控制台应用程序运行相同的代码。需要立即指导。似乎当我从 Windows 服务运行此代码时,它没有掌握进程或服务在不同的帐户中运行,因此没有传递消息。
1 回答
胡说叔叔
TA贡献1804条经验 获得超8个赞
很可能您在会话 0 中以本地系统帐户的身份运行您的服务,并且出于充分的理由,它相当孤立。例如,您无权访问其他桌面/会话。
您必须实现不同的 IPC 方法,例如管道或内存映射文件。
- 1 回答
- 0 关注
- 217 浏览
添加回答
举报
0/150
提交
取消