PDA

View Full Version : سوال: اجرا کردن یک برنامه به وسیله یک ویندوز سرویس



ferankyy
چهارشنبه 15 آذر 1391, 10:25 صبح
با سلام

من یه ویندوز سرویس نوشتم که میخوام وقتی Start شد یه برنامه رو اجرا کنه . چون در ویندوز 7 سرویس ها در session دیگری در حال اجرا هستند وقتی یه برنامه رو اجرا میکنن داخل همون session اجرا میشه . ولی من میخوام که داخل session دسکتاپ اجرا بشه .

Amir_GhF
چهارشنبه 15 آذر 1391, 16:07 عصر
سلام من هم همین مشکل رو دارم تو SESSION 1 ویندوز 7. برنامه به راحتی تو ایکس پی کار میکنه. ی کلاس هست که باید اول و اخر پروسست اجراش کنی. من انجام دادم ولی باز نشد.اگه راهی ÷یدا کردی بم بگو... زیاد گشتم ولی خیلی گیج کننده بود.using Desktop1; ---------------------------------------------------------------------- Desktop2 test = new Desktop2(); test.BeginInteraction(); myproject(); test.EndInteraction();---------------------------------------------------------------using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.InteropServices;namespace Desktop1{ internal class Desktop2 { private IntPtr m_hCurWinsta = IntPtr.Zero; private IntPtr m_hCurDesktop = IntPtr.Zero; private IntPtr m_hWinsta = IntPtr.Zero; private IntPtr m_hDesk = IntPtr.Zero; /// /// associate the current thread to the default desktop /// /// internal bool BeginInteraction() { EndInteraction(); m_hCurWinsta = User32DLL.GetProcessWindowStation(); if (m_hCurWinsta == IntPtr.Zero) return false; m_hCurDesktop = User32DLL.GetDesktopWindow(); if (m_hCurDesktop == IntPtr.Zero) return false; m_hWinsta = User32DLL.OpenWindowStation("winsta0", false, WindowStationAccessRight.WINSTA_ACCESSCLIPBOARD | WindowStationAccessRight.WINSTA_ACCESSGLOBALATOMS | WindowStationAccessRight.WINSTA_CREATEDESKTOP | WindowStationAccessRight.WINSTA_ENUMDESKTOPS | WindowStationAccessRight.WINSTA_ENUMERATE | WindowStationAccessRight.WINSTA_EXITWINDOWS | WindowStationAccessRight.WINSTA_READATTRIBUTES | WindowStationAccessRight.WINSTA_READSCREEN | WindowStationAccessRight.WINSTA_WRITEATTRIBUTES ); if (m_hWinsta == IntPtr.Zero) return false; User32DLL.SetProcessWindowStation(m_hWinsta); m_hDesk = User32DLL.OpenDesktop("default", OpenDesktopFlag.DF_NONE, false, DesktopAccessRight.DESKTOP_CREATEMENU | DesktopAccessRight.DESKTOP_CREATEWINDOW | DesktopAccessRight.DESKTOP_ENUMERATE | DesktopAccessRight.DESKTOP_HOOKCONTROL | DesktopAccessRight.DESKTOP_JOURNALPLAYBACK | DesktopAccessRight.DESKTOP_JOURNALRECORD | DesktopAccessRight.DESKTOP_READOBJECTS | DesktopAccessRight.DESKTOP_SWITCHDESKTOP | DesktopAccessRight.DESKTOP_WRITEOBJECTS ); if (m_hDesk == IntPtr.Zero) return false; User32DLL.SetThreadDesktop(m_hDesk); return true; } /// /// restore /// internal void EndInteraction() { if (m_hCurWinsta != IntPtr.Zero) User32DLL.SetProcessWindowStation(m_hCurWinsta); if (m_hCurDesktop != IntPtr.Zero) User32DLL.SetThreadDesktop(m_hCurDesktop); if (m_hWinsta != IntPtr.Zero) User32DLL.CloseWindowStation(m_hWinsta); if (m_hDesk != IntPtr.Zero) User32DLL.CloseDesktop(m_hDesk); } } public static class User32DLL { /// /// The GetDesktopWindow function returns a handle to the desktop window. /// The desktop window covers the entire screen. /// The desktop window is the area on top of which other windows are painted. /// /// The return value is a handle to the desktop window. [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern IntPtr GetDesktopWindow(); /// /// Retrieves a handle to the current window station for the calling process. /// /// If the function succeeds, /// the return value is a handle to the window station. /// If the function fails, the return value is NULL. [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern IntPtr GetProcessWindowStation(); /// /// Retrieves a handle to the desktop assigned to the specified thread. /// /// [in] Handle to the thread /// for which to return the desktop handle. /// If the function succeeds, the return value is a handle to the /// desktop associated with the specified thread. /// If the function fails, the return value is NULL. [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern IntPtr GetThreadDesktop(uint dwThread); /// /// Opens the specified window station. /// /// Pointer to a null-terminated /// string specifying the name of the window station /// to be opened. Window station names are case-insensitive. /// This window station must belong to the current session. /// /// [in] If this value /// is TRUE, processes created by this process /// will inherit the handle. Otherwise, /// the processes do not inherit this handle. /// /// [in] Access to the window station /// If the function succeeds, the return value /// is the handle to the specified window station. /// If the function fails, the return value is NULL. [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern IntPtr OpenWindowStation(string lpszWinSta , bool fInherit , WindowStationAccessRight dwDesiredAccess ); /// /// Assigns the specified window station to the calling process. /// This enables the process to access objects in the window /// station such as desktops, the clipboard, and global atoms. /// All subsequent operations on the window station /// use the access rights granted to hWinSta. /// /// [in] Handle to the window /// station to be assigned to the calling process /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero. [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern IntPtr SetProcessWindowStation(IntPtr hWinSta); /// /// Closes an open window station handle. /// /// [in] Handle /// to the window station to be closed. /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero. [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern IntPtr CloseWindowStation(IntPtr hWinSta); /// /// Opens the specified desktop object. /// /// [in] Pointer to null-terminated string /// specifying the name of the desktop to be opened. /// Desktop names are case-insensitive. /// This desktop must belong to the current window station. /// [in] This parameter can /// be zero or DF_ALLOWOTHERACCOUNTHOOK=0x0001 /// [in] If this value is TRUE, processes created by /// this process will inherit the handle. /// Otherwise, the processes do not inherit this handle. /// [in] Access /// to the desktop. For a list of access rights /// If the function succeeds, the return value is a handle to the opened desktop. /// When you are finished using the handle, call the CloseDesktop function to close it. /// If the function fails, the return value is NULL. /// [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern IntPtr OpenDesktop(string lpszDesktop , OpenDesktopFlag dwFlags , bool fInherit , DesktopAccessRight dwDesiredAccess ); /// /// Closes an open handle to a desktop object. /// /// [in] Handle to the desktop to be closed. /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero. [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern IntPtr CloseDesktop(IntPtr hDesktop); /// /// Assigns the specified desktop to the calling thread. /// All subsequent operations on the desktop use the access rights granted to the desktop. /// /// [in] Handle to the desktop /// to be assigned to the calling thread. /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero. [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern bool SetThreadDesktop(IntPtr hDesktop); } /// /// REF MSDN:Window Station Security and Access Rights /// ms-help://MS.MSDN.vAug06.en/dllproc/base/window_station_security_and_access_rights.htm /// [FlagsAttribute] public enum WindowStationAccessRight : uint { /// All possible access rights for the window station. WINSTA_ALL_ACCESS = 0x37F, /// Required to use the clipboard. WINSTA_ACCESSCLIPBOARD = 0x0004, /// Required to manipulate global atoms. WINSTA_ACCESSGLOBALATOMS = 0x0020, /// Required to create new desktop /// objects on the window station. WINSTA_CREATEDESKTOP = 0x0008, /// Required to enumerate existing desktop objects. WINSTA_ENUMDESKTOPS = 0x0001, /// Required for the window station to be enumerated. WINSTA_ENUMERATE = 0x0100, /// Required to successfully call the ExitWindows or ExitWindowsEx function. /// Window stations can be shared by users and this access type can prevent other users /// of a window station from logging off the window station owner. WINSTA_EXITWINDOWS = 0x0040, /// Required to read the attributes of a window station object. /// This attribute includes color settings /// and other global window station properties. WINSTA_READATTRIBUTES = 0x0002, /// Required to access screen contents. WINSTA_READSCREEN = 0x0200, /// Required to modify the attributes of /// a window station object. /// The attributes include color settings /// and other global window station properties. WINSTA_WRITEATTRIBUTES = 0x0010, } /// /// OpenDesktop 2nd param /// public enum OpenDesktopFlag : uint { /// /// Default value /// DF_NONE = 0x0000, /// /// Allows processes running in other accounts on the desktop /// to set hooks in this process. /// DF_ALLOWOTHERACCOUNTHOOK = 0x0001, } /// /// REF MSDN:Desktop Security and Access Rights /// ms-help://MS.MSDN.vAug06.en/dllproc/base/desktop_security_and_access_rights.htm /// [FlagsAttribute] public enum DesktopAccessRight : uint { /// Required to create a menu on the desktop. DESKTOP_CREATEMENU = 0x0004, /// Required to create a window on the desktop. DESKTOP_CREATEWINDOW = 0x0002, /// Required for the desktop to be enumerated. DESKTOP_ENUMERATE = 0x0040, /// Required to establish any of the window hooks. DESKTOP_HOOKCONTROL = 0x0008, /// Required to perform journal playback on a desktop. DESKTOP_JOURNALPLAYBACK = 0x0020, /// Required to perform journal recording on a desktop. DESKTOP_JOURNALRECORD = 0x0010, /// Required to read objects on the desktop. DESKTOP_READOBJECTS = 0x0001, /// Required to activate the desktop /// using the SwitchDesktop function. DESKTOP_SWITCHDESKTOP = 0x0100, /// Required to write objects on the desktop. DESKTOP_WRITEOBJECTS = 0x0080, }}

black_binary
دوشنبه 14 بهمن 1392, 13:20 عصر
سلام
کسی پاسخی برای این تایپیک نداره.......؟

ferankyy
سه شنبه 15 بهمن 1392, 11:56 صبح
من پیدا کردم برات میزارم

hamid_hr
سه شنبه 15 بهمن 1392, 12:49 عصر
برو توی service manage
روی سرویس راست کلیک و properties
تب log on
تیک allow service ti intracet with desktop رو فعال کن
برا xp,7 درست میشه ولی برا win server 2003 درست نشد

ordebehesht
سه شنبه 15 بهمن 1392, 18:10 عصر
سلام من هم همین مشکل رو دارم تو SESSION 1 ویندوز 7. برنامه به راحتی تو ایکس پی کار میکنه. ی کلاس هست که باید اول و اخر پروسست اجراش کنی. من انجام دادم ولی باز نشد.اگه راهی ÷یدا کردی بم بگو... زیاد گشتم ولی خیلی گیج کننده بود.using Desktop1; ---------------------------------------------------------------------- Desktop2 test = new Desktop2(); test.BeginInteraction(); myproject(); test.EndInteraction();---------------------------------------------------------------using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.InteropServices;namespace Desktop1{ internal class Desktop2 { private IntPtr m_hCurWinsta = IntPtr.Zero; private IntPtr m_hCurDesktop = IntPtr.Zero; private IntPtr m_hWinsta = IntPtr.Zero; private IntPtr m_hDesk = IntPtr.Zero; /// /// associate the current thread to the default desktop /// /// internal bool BeginInteraction() { EndInteraction(); m_hCurWinsta = User32DLL.GetProcessWindowStation(); if (m_hCurWinsta == IntPtr.Zero) return false; m_hCurDesktop = User32DLL.GetDesktopWindow(); if (m_hCurDesktop == IntPtr.Zero) return false; m_hWinsta = User32DLL.OpenWindowStation("winsta0", false, WindowStationAccessRight.WINSTA_ACCESSCLIPBOARD | WindowStationAccessRight.WINSTA_ACCESSGLOBALATOMS | WindowStationAccessRight.WINSTA_CREATEDESKTOP | WindowStationAccessRight.WINSTA_ENUMDESKTOPS | WindowStationAccessRight.WINSTA_ENUMERATE | WindowStationAccessRight.WINSTA_EXITWINDOWS | WindowStationAccessRight.WINSTA_READATTRIBUTES | WindowStationAccessRight.WINSTA_READSCREEN | WindowStationAccessRight.WINSTA_WRITEATTRIBUTES ); if (m_hWinsta == IntPtr.Zero) return false; User32DLL.SetProcessWindowStation(m_hWinsta); m_hDesk = User32DLL.OpenDesktop("default", OpenDesktopFlag.DF_NONE, false, DesktopAccessRight.DESKTOP_CREATEMENU | DesktopAccessRight.DESKTOP_CREATEWINDOW | DesktopAccessRight.DESKTOP_ENUMERATE | DesktopAccessRight.DESKTOP_HOOKCONTROL | DesktopAccessRight.DESKTOP_JOURNALPLAYBACK | DesktopAccessRight.DESKTOP_JOURNALRECORD | DesktopAccessRight.DESKTOP_READOBJECTS | DesktopAccessRight.DESKTOP_SWITCHDESKTOP | DesktopAccessRight.DESKTOP_WRITEOBJECTS ); if (m_hDesk == IntPtr.Zero) return false; User32DLL.SetThreadDesktop(m_hDesk); return true; } /// /// restore /// internal void EndInteraction() { if (m_hCurWinsta != IntPtr.Zero) User32DLL.SetProcessWindowStation(m_hCurWinsta); if (m_hCurDesktop != IntPtr.Zero) User32DLL.SetThreadDesktop(m_hCurDesktop); if (m_hWinsta != IntPtr.Zero) User32DLL.CloseWindowStation(m_hWinsta); if (m_hDesk != IntPtr.Zero) User32DLL.CloseDesktop(m_hDesk); } } public static class User32DLL { /// /// The GetDesktopWindow function returns a handle to the desktop window. /// The desktop window covers the entire screen. /// The desktop window is the area on top of which other windows are painted. /// /// The return value is a handle to the desktop window. [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern IntPtr GetDesktopWindow(); /// /// Retrieves a handle to the current window station for the calling process. /// /// If the function succeeds, /// the return value is a handle to the window station. /// If the function fails, the return value is NULL. [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern IntPtr GetProcessWindowStation(); /// /// Retrieves a handle to the desktop assigned to the specified thread. /// /// [in] Handle to the thread /// for which to return the desktop handle. /// If the function succeeds, the return value is a handle to the /// desktop associated with the specified thread. /// If the function fails, the return value is NULL. [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern IntPtr GetThreadDesktop(uint dwThread); /// /// Opens the specified window station. /// /// Pointer to a null-terminated /// string specifying the name of the window station /// to be opened. Window station names are case-insensitive. /// This window station must belong to the current session. /// /// [in] If this value /// is TRUE, processes created by this process /// will inherit the handle. Otherwise, /// the processes do not inherit this handle. /// /// [in] Access to the window station /// If the function succeeds, the return value /// is the handle to the specified window station. /// If the function fails, the return value is NULL. [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern IntPtr OpenWindowStation(string lpszWinSta , bool fInherit , WindowStationAccessRight dwDesiredAccess ); /// /// Assigns the specified window station to the calling process. /// This enables the process to access objects in the window /// station such as desktops, the clipboard, and global atoms. /// All subsequent operations on the window station /// use the access rights granted to hWinSta. /// /// [in] Handle to the window /// station to be assigned to the calling process /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero. [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern IntPtr SetProcessWindowStation(IntPtr hWinSta); /// /// Closes an open window station handle. /// /// [in] Handle /// to the window station to be closed. /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero. [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern IntPtr CloseWindowStation(IntPtr hWinSta); /// /// Opens the specified desktop object. /// /// [in] Pointer to null-terminated string /// specifying the name of the desktop to be opened. /// Desktop names are case-insensitive. /// This desktop must belong to the current window station. /// [in] This parameter can /// be zero or DF_ALLOWOTHERACCOUNTHOOK=0x0001 /// [in] If this value is TRUE, processes created by /// this process will inherit the handle. /// Otherwise, the processes do not inherit this handle. /// [in] Access /// to the desktop. For a list of access rights /// If the function succeeds, the return value is a handle to the opened desktop. /// When you are finished using the handle, call the CloseDesktop function to close it. /// If the function fails, the return value is NULL. /// [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern IntPtr OpenDesktop(string lpszDesktop , OpenDesktopFlag dwFlags , bool fInherit , DesktopAccessRight dwDesiredAccess ); /// /// Closes an open handle to a desktop object. /// /// [in] Handle to the desktop to be closed. /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero. [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern IntPtr CloseDesktop(IntPtr hDesktop); /// /// Assigns the specified desktop to the calling thread. /// All subsequent operations on the desktop use the access rights granted to the desktop. /// /// [in] Handle to the desktop /// to be assigned to the calling thread. /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero. [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern bool SetThreadDesktop(IntPtr hDesktop); } /// /// REF MSDN:Window Station Security and Access Rights /// ms-help://MS.MSDN.vAug06.en/dllproc/base/window_station_security_and_access_rights.htm /// [FlagsAttribute] public enum WindowStationAccessRight : uint { /// All possible access rights for the window station. WINSTA_ALL_ACCESS = 0x37F, /// Required to use the clipboard. WINSTA_ACCESSCLIPBOARD = 0x0004, /// Required to manipulate global atoms. WINSTA_ACCESSGLOBALATOMS = 0x0020, /// Required to create new desktop /// objects on the window station. WINSTA_CREATEDESKTOP = 0x0008, /// Required to enumerate existing desktop objects. WINSTA_ENUMDESKTOPS = 0x0001, /// Required for the window station to be enumerated. WINSTA_ENUMERATE = 0x0100, /// Required to successfully call the ExitWindows or ExitWindowsEx function. /// Window stations can be shared by users and this access type can prevent other users /// of a window station from logging off the window station owner. WINSTA_EXITWINDOWS = 0x0040, /// Required to read the attributes of a window station object. /// This attribute includes color settings /// and other global window station properties. WINSTA_READATTRIBUTES = 0x0002, /// Required to access screen contents. WINSTA_READSCREEN = 0x0200, /// Required to modify the attributes of /// a window station object. /// The attributes include color settings /// and other global window station properties. WINSTA_WRITEATTRIBUTES = 0x0010, } /// /// OpenDesktop 2nd param /// public enum OpenDesktopFlag : uint { /// /// Default value /// DF_NONE = 0x0000, /// /// Allows processes running in other accounts on the desktop /// to set hooks in this process. /// DF_ALLOWOTHERACCOUNTHOOK = 0x0001, } /// /// REF MSDN:Desktop Security and Access Rights /// ms-help://MS.MSDN.vAug06.en/dllproc/base/desktop_security_and_access_rights.htm /// [FlagsAttribute] public enum DesktopAccessRight : uint { /// Required to create a menu on the desktop. DESKTOP_CREATEMENU = 0x0004, /// Required to create a window on the desktop. DESKTOP_CREATEWINDOW = 0x0002, /// Required for the desktop to be enumerated. DESKTOP_ENUMERATE = 0x0040, /// Required to establish any of the window hooks. DESKTOP_HOOKCONTROL = 0x0008, /// Required to perform journal playback on a desktop. DESKTOP_JOURNALPLAYBACK = 0x0020, /// Required to perform journal recording on a desktop. DESKTOP_JOURNALRECORD = 0x0010, /// Required to read objects on the desktop. DESKTOP_READOBJECTS = 0x0001, /// Required to activate the desktop /// using the SwitchDesktop function. DESKTOP_SWITCHDESKTOP = 0x0100, /// Required to write objects on the desktop. DESKTOP_WRITEOBJECTS = 0x0080, }}


دوست من هنگام ایجاد تایپیک از تگ های بالایی استفاده کنید تا متن شما خاوانا بشود

http://barnamenevis.org/images/editor/cpp.gif http://barnamenevis.org/images/editor/csharp.gif http://barnamenevis.org/images/editor/css.gif http://barnamenevis.org/images/editor/pas.gif http://barnamenevis.org/images/editor/java.gif http://barnamenevis.org/images/editor/js.gif http://barnamenevis.org/images/editor/sql.gif http://barnamenevis.org/images/editor/vb.gif http://barnamenevis.org/images/editor/xml.gif ....