我的問(wèn)題是 PostMessage Windows API 工作不正常,因?yàn)樗趶目刂婆_(tái)應(yīng)用程序運(yùn)行時(shí)工作。工作代碼:我有 2 個(gè)應(yīng)用程序 [1] 是控制臺(tái)應(yīng)用程序 [2] Windows 窗體應(yīng)用程序。要求是我想向所有正在運(yùn)行的應(yīng)用程序?qū)嵗l(fā)送消息??刂婆_(tái)應(yīng)用程序代碼: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) } } }}上面的代碼按預(yù)期工作。我的問(wèn)題從這里開(kāi)始。我想從 Windows 服務(wù)而不是控制臺(tái)應(yīng)用程序運(yùn)行相同的代碼。需要立即指導(dǎo)。似乎當(dāng)我從 Windows 服務(wù)運(yùn)行此代碼時(shí),它沒(méi)有掌握進(jìn)程或服務(wù)在不同的帳戶中運(yùn)行,因此沒(méi)有傳遞消息。
1 回答

胡說(shuō)叔叔
TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊
很可能您在會(huì)話 0 中以本地系統(tǒng)帳戶的身份運(yùn)行您的服務(wù),并且出于充分的理由,它相當(dāng)孤立。例如,您無(wú)權(quán)訪問(wèn)其他桌面/會(huì)話。
您必須實(shí)現(xiàn)不同的 IPC 方法,例如管道或內(nèi)存映射文件。
- 1 回答
- 0 關(guān)注
- 240 瀏覽
添加回答
舉報(bào)
0/150
提交
取消