線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:6554
推到 Plurk!
推到 Facebook!

CreateProcessAsUser无法正常运行于Service程序

尚未結案
alanktop
一般會員


發表:7
回覆:21
積分:5
註冊:2005-03-01

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-03-13 09:54:30 IP:219.133.xxx.xxx 未訂閱
Windows服务程序以默认的System帐号注册后,无法访问网络资源(请参考 http://dev.csdn.net/article/20/20555.shtm ),于是必须使用CreateProcessWithLogonW将登陆身份转换为Administrator然后创建进程。CreateProcessWithLogonW在Windows2000/XP/XPSP1下能正常工作,但是在WindowsXPSP2/Windows2003下却不再有效。 (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createprocesswithlogonw.asp : Windows XP SP2 and Windows Server 2003: You cannot call CreateProcessWithLogonW from a process that is running under the LocalSystem account, because the function uses the logon SID in the caller token, and the token for the LocalSystem account does not contain this SID. As an alternative, use the CreateProcessAsUser and LogonUser functions.) 于是我将CreateProcessWithLogonW更改为LogonUser、CreateProcessAsUser,程序可以运行,但是创建的进程虽然跑起来了,但是似乎屏幕上总是出现了透明的窗体,无法完全正常显示。 (类似的问题: http://www.delphipages.com/threads/thread.cfm?ID=113860&G=113860) 微软的解释: (http://support.microsoft.com/kb/165194 : Sometimes the process may start, but fail to draw its GUI correctly. The best method to resolve these and other potential access related problems is to grant the user full access to both the targeted windowstation and desktop. For example, if you want the process that is started by the CreateProcessAsUser function to be interactive, specify the following windowstation and desktop combination: "winsta0\default" ) 我在程序代码里加入了"winsta0\default"的定义后,程序反而不能运行了。 LogonUser(PChar(username), PChar(GetTheComputerName), PChar(password), LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, FToken); ImpersonateLoggedOnUser(FToken); DuplicateTokenEx(FToken, MAXIMUM_ALLOWED, nil, SecurityAnonymous,TokenPrimary, DupToken); FillChar(StartInfo, Sizeof(StartInfo), 0); StartInfo.cb := SizeOf(StartInfo); StartInfo.lpDesktop := PChar('winsta0\\default'); StartInfo.wShowWindow := 1; CreateProcessAsUser(DupToken, nil, PChar(needtorun), nil, nil, True, 0, nil, nil, StartInfo, Info); 请高手赐教,感谢! 發表人 - alanktop 於 2005/03/13 09:58:23 發表人 - alanktop 於 2005/03/13 10:01:28
alanktop
一般會員


發表:7
回覆:21
積分:5
註冊:2005-03-01

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-03-19 23:14:06 IP:218.17.xxx.xxx 未訂閱
注意不要用Delphi7来编译该程序,虽能通过编译,但是得到的执行文件安装为系统服务后,立刻会有错误产生,原因还未能找出。
kuo348
一般會員


發表:2
回覆:11
積分:12
註冊:2002-09-18

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-09-30 15:03:12 IP:60.248.xxx.xxx 未訂閱
我也曾遇過這樣的問題,不過我通常把Service停掉在重新啟動則無此情形, 發生頻率不固定.我也不曉得,因為我在Serveice需要使用網路印表機,System無法使用所以我的做法是將Service改由Current User來啟動,缺點是無法與桌面互動 PS我使用的是BCB6
Kingron
中階會員


發表:1
回覆:51
積分:60
註冊:2005-09-14

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-10-01 15:51:21 IP:61.145.xxx.xxx 未訂閱
你的那个Desktop不一定存在,你应该先建立一个Desktop。 看你的样子是在做木马吧?
------
超级猛料:http://kingron.delphibbs.com
alanktop
一般會員


發表:7
回覆:21
積分:5
註冊:2005-03-01

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-10-10 00:10:50 IP:218.18.xxx.xxx 未訂閱
虽然这个程序有点木马的意思,不过我的原意不是这样。我是为公司的终端用户做一个程序,用于强制分发公司安全策略中必须运行的程序。所以得做成服务模式并供远程调用。 可是怎样实现以System账号建立一个Desktop并在当前用户环境中显示呢?具体代码如何写?这正是我关心的。 是否利用Desktop这个特征我们可以写出多桌面应用程序,即在当前屏幕开辟某一区域大小的桌面,却以另一用户身份登陆?这种方式有别于Windows终端的概念。终端是本机操纵另以机器,这个是一个屏幕上显示几个用户登陆的窗口,各自独立。
kuo348
一般會員


發表:2
回覆:11
積分:12
註冊:2002-09-18

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-10-17 09:37:09 IP:60.248.xxx.xxx 未訂閱
我改編了包老大的Service Application Sample ,CreateProcessAsUse則是從MSDN找到的Code Usernaeme 是TEdit元件 Password  是TMaskEdit元件
//------------------------------------------------------------------------------    #include 
#pragma hdrstop
#include 
#include "Unit2.h"    NOTIFYICONDATA IconData;
extern bool gbCanClose;
#define DESKTOP_ALL (DESKTOP_READOBJECTS | DESKTOP_CREATEWINDOW | DESKTOP_CREATEMENU | DESKTOP_HOOKCONTROL | DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK | DESKTOP_ENUMERATE | DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP | STANDARD_RIGHTS_REQUIRED)    #define WINSTA_ALL (WINSTA_ENUMDESKTOPS | WINSTA_READATTRIBUTES | WINSTA_ACCESSCLIPBOARD | WINSTA_CREATEDESKTOP | WINSTA_WRITEATTRIBUTES | WINSTA_ACCESSGLOBALATOMS | WINSTA_EXITWINDOWS | WINSTA_ENUMERATE | WINSTA_READSCREEN | STANDARD_RIGHTS_REQUIRED)    #define GENERIC_ACCESS (GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL)
BOOL AddAceToDesktop(HDESK hdesk, PSID psid);
VOID FreeLogonSID (PSID *ppsid);
BOOL GetLogonSID (HANDLE hToken, PSID *ppsid);
BOOL AddAceToWindowStation(HWINSTA hwinsta, PSID psid);
BOOL StartInteractiveClientProcess (
    LPTSTR lpszUsername,    // client to log on
    LPTSTR lpszDomain,      // domain of client's account
    LPTSTR lpszPassword,    // client's password
    LPTSTR lpFile,
    LPTSTR lpCommandLine    // command line to execute
); 
VOID FreeLogonSID (PSID *ppsid)
{
    HeapFree(GetProcessHeap(), 0, (LPVOID)*ppsid);
}
 BOOL GetLogonSID (HANDLE hToken, PSID *ppsid)
{
   BOOL bSuccess = FALSE;
   DWORD dwIndex;
   DWORD dwLength = 0;
   PTOKEN_GROUPS ptg = NULL;    // Verify the parameter passed in is not NULL.
    if (NULL == ppsid)
        goto Cleanup;    // Get required buffer size and allocate the TOKEN_GROUPS buffer.       if (!GetTokenInformation(
         hToken,         // handle to the access token
         TokenGroups,    // get information about the token's groups 
         (LPVOID) ptg,   // pointer to TOKEN_GROUPS buffer
         0,              // size of buffer
         &dwLength       // receives required buffer size
      )) 
   {
      if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) 
         goto Cleanup;          ptg = (PTOKEN_GROUPS)HeapAlloc(GetProcessHeap(),
         HEAP_ZERO_MEMORY, dwLength);          if (ptg == NULL)
         goto Cleanup;
   }    // Get the token group information from the access token.       if (!GetTokenInformation(
         hToken,         // handle to the access token
         TokenGroups,    // get information about the token's groups 
         (LPVOID) ptg,   // pointer to TOKEN_GROUPS buffer
         dwLength,       // size of buffer
         &dwLength       // receives required buffer size
         )) 
   {
      goto Cleanup;
   }    // Loop through the groups to find the logon SID.       for (dwIndex = 0; dwIndex < ptg->GroupCount; dwIndex  ) 
      if ((ptg->Groups[dwIndex].Attributes & SE_GROUP_LOGON_ID)
             ==  SE_GROUP_LOGON_ID) 
      {
      // Found the logon SID; make a copy of it.             dwLength = GetLengthSid(ptg->Groups[dwIndex].Sid);
         *ppsid = (PSID) HeapAlloc(GetProcessHeap(),
                     HEAP_ZERO_MEMORY, dwLength);
         if (*ppsid == NULL)
             goto Cleanup;
         if (!CopySid(dwLength, *ppsid, ptg->Groups[dwIndex].Sid)) 
         {
             HeapFree(GetProcessHeap(), 0, (LPVOID)*ppsid);
             goto Cleanup;
         }
         break;
      }       bSuccess = TRUE;    Cleanup:     // Free the buffer for the token groups.       if (ptg != NULL)
      HeapFree(GetProcessHeap(), 0, (LPVOID)ptg);       return bSuccess;
}
BOOL StartInteractiveClientProcess (
    LPTSTR lpszUsername,    // client to log on
    LPTSTR lpszDomain,      // domain of client's account
    LPTSTR lpszPassword,    // client's password
    LPTSTR lpFile,
    LPTSTR lpCommandLine    // command line to execute
)
{
   HANDLE      hToken;
   HDESK       hdesk = NULL;
   HWINSTA     hwinsta = NULL, hwinstaSave = NULL;
   PROCESS_INFORMATION pi;
   PSID pSid = NULL;
   STARTUPINFO si;
   BOOL bResult = FALSE;    // Log the client on to the local computer.       if (!LogonUser(
           lpszUsername,
           lpszDomain,
           lpszPassword,
           LOGON32_LOGON_INTERACTIVE,
           LOGON32_PROVIDER_DEFAULT,
           &hToken) )
   {
      goto Cleanup;
   }    // Save a handle to the caller's current window station.       if ( (hwinstaSave = GetProcessWindowStation() ) == NULL)
      goto Cleanup;    // Get a handle to the interactive window station.       hwinsta = OpenWindowStation(
       "winsta0",                   // the interactive window station 
       FALSE,                       // handle is not inheritable
       READ_CONTROL | WRITE_DAC);   // rights to read/write the DACL       if (hwinsta == NULL) 
      goto Cleanup;    // To get the correct default desktop, set the caller's 
// window station to the interactive window station.       if (!SetProcessWindowStation(hwinsta))
      goto Cleanup;    // Get a handle to the interactive desktop.       hdesk = OpenDesktop(
      "default",     // the interactive window station 
      0,             // no interaction with other desktop processes
      FALSE,         // handle is not inheritable
      READ_CONTROL | // request the rights to read and write the DACL
      WRITE_DAC | 
      DESKTOP_WRITEOBJECTS | 
      DESKTOP_READOBJECTS);    // Restore the caller's window station.       if (!SetProcessWindowStation(hwinstaSave)) 
      goto Cleanup;       if (hdesk == NULL) 
      goto Cleanup;    // Get the SID for the client's logon session.       if (!GetLogonSID(hToken, &pSid))
      goto Cleanup;    // Allow logon SID full access to interactive window station.       if (! AddAceToWindowStation(hwinsta, pSid) )
      goto Cleanup;    // Allow logon SID full access to interactive desktop.       if (! AddAceToDesktop(hdesk, pSid) ) 
      goto Cleanup;    // Impersonate client to ensure access to executable file.       if (! ImpersonateLoggedOnUser(hToken) ) 
      goto Cleanup;    // Initialize the STARTUPINFO structure.
// Specify that the process runs in the interactive desktop.       ZeroMemory(&si, sizeof(STARTUPINFO));
   si.cb= sizeof(STARTUPINFO);
   si.lpDesktop = TEXT("winsta0\\default");    // Launch the process in the client's logon session.       bResult = CreateProcessAsUser(
      hToken,            // client's access token
      lpFile,              // file to execute
      lpCommandLine ,     // command line
      NULL,              // pointer to process SECURITY_ATTRIBUTES
      NULL,              // pointer to thread SECURITY_ATTRIBUTES
      true,             // handles are not inheritable
      NULL,   // creation flags
      NULL,              // pointer to new environment block
      NULL,              // name of current directory 
      &si,               // pointer to STARTUPINFO structure
      &pi                // receives information about new process
   );    // End impersonation of client.       RevertToSelf();       if (bResult && pi.hProcess != INVALID_HANDLE_VALUE) 
   {
      WaitForSingleObject(pi.hProcess, INFINITE); 
      CloseHandle(pi.hProcess); 
   }        if (pi.hThread != INVALID_HANDLE_VALUE)
      CloseHandle(pi.hThread);      Cleanup:        if (hwinstaSave != NULL)
      SetProcessWindowStation (hwinstaSave);    // Free the buffer for the logon SID.       if (pSid)
      FreeLogonSID(&pSid);    // Close the handles to the interactive window station and desktop.       if (hwinsta)
      CloseWindowStation(hwinsta);       if (hdesk)
      CloseDesktop(hdesk);    // Close the handle to the client's access token.       if (hToken != INVALID_HANDLE_VALUE)
      CloseHandle(hToken);         return bResult;
}    BOOL AddAceToWindowStation(HWINSTA hwinsta, PSID psid)
{
   ACCESS_ALLOWED_ACE   *pace;
   ACL_SIZE_INFORMATION aclSizeInfo;
   BOOL                 bDaclExist;
   BOOL                 bDaclPresent;
   BOOL                 bSuccess = FALSE;
   DWORD                dwNewAclSize;
   DWORD                dwSidSize = 0;
   DWORD                dwSdSizeNeeded;
   PACL                 pacl;
   PACL                 pNewAcl;
   PSECURITY_DESCRIPTOR psd = NULL;
   PSECURITY_DESCRIPTOR psdNew = NULL;
   PVOID                pTempAce;
   SECURITY_INFORMATION si = DACL_SECURITY_INFORMATION;
   unsigned int         i;       __try
   {
      // Obtain the DACL for the window station.          if (!GetUserObjectSecurity(
             hwinsta,
             &si,
             psd,
             dwSidSize,
             &dwSdSizeNeeded)
      )
      if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
      {
         psd = (PSECURITY_DESCRIPTOR)HeapAlloc(
               GetProcessHeap(),
               HEAP_ZERO_MEMORY,
               dwSdSizeNeeded);             if (psd == NULL)
          {
           // __leave;
          } //             psdNew = (PSECURITY_DESCRIPTOR)HeapAlloc(
               GetProcessHeap(),
               HEAP_ZERO_MEMORY,
               dwSdSizeNeeded);             if (psdNew == NULL)
           {
           // __leave;
           }             dwSidSize = dwSdSizeNeeded;             if (!GetUserObjectSecurity(
               hwinsta,
               &si,
               psd,
               dwSidSize,
               &dwSdSizeNeeded)
         )
          {
           // __leave;
          }
      }
      else
       {
       //  __leave;
       }          // Create a new DACL.          if (!InitializeSecurityDescriptor(
            psdNew,
            SECURITY_DESCRIPTOR_REVISION)
      )
        {
        // __leave;
        }          // Get the DACL from the security descriptor.          if (!GetSecurityDescriptorDacl(
            psd,
            &bDaclPresent,
            &pacl,
            &bDaclExist)
      )
        {
        // __leave;
        }          // Initialize the ACL.          ZeroMemory(&aclSizeInfo, sizeof(ACL_SIZE_INFORMATION));
      aclSizeInfo.AclBytesInUse = sizeof(ACL);          // Call only if the DACL is not NULL.          if (pacl != NULL)
      {
         // get the file ACL size info
         if (!GetAclInformation(
               pacl,
               (LPVOID)&aclSizeInfo,
               sizeof(ACL_SIZE_INFORMATION),
               AclSizeInformation)
         )
          {
          //  __leave;
          }
      }          // Compute the size of the new ACL.          dwNewAclSize = aclSizeInfo.AclBytesInUse   (2*sizeof(ACCESS_ALLOWED_ACE))   
(2*GetLengthSid(psid)) - (2*sizeof(DWORD));          // Allocate memory for the new ACL.          pNewAcl = (PACL)HeapAlloc(
            GetProcessHeap(),
            HEAP_ZERO_MEMORY,
            dwNewAclSize);          if (pNewAcl == NULL)
         {
         //__leave;
         }          // Initialize the new DACL.          if (!InitializeAcl(pNewAcl, dwNewAclSize, ACL_REVISION))
        {
        // __leave;
        }          // If DACL is present, copy it to a new DACL.          if (bDaclPresent)
      {
         // Copy the ACEs to the new ACL.
         if (aclSizeInfo.AceCount)
         {
            for (i=0; i < aclSizeInfo.AceCount; i  )
            {
               // Get an ACE.
               if (!GetAce(pacl, i, &pTempAce))
                 {
                 // __leave;
                 }                   // Add the ACE to the new ACL.
               if (!AddAce(
                     pNewAcl,
                     ACL_REVISION,
                     MAXDWORD,
                     pTempAce,
                    ((PACE_HEADER)pTempAce)->AceSize)
               )
               {
               //__leave;
               }
            }
         }
      }          // Add the first ACE to the window station.          pace = (ACCESS_ALLOWED_ACE *)HeapAlloc(
            GetProcessHeap(),
            HEAP_ZERO_MEMORY,
            sizeof(ACCESS_ALLOWED_ACE)   GetLengthSid(psid) -
                  sizeof(DWORD));          if (pace == NULL)
       {
       //  __leave;
       }          pace->Header.AceType  = ACCESS_ALLOWED_ACE_TYPE;
      pace->Header.AceFlags = CONTAINER_INHERIT_ACE |
                   INHERIT_ONLY_ACE | OBJECT_INHERIT_ACE;
      pace->Header.AceSize  = sizeof(ACCESS_ALLOWED_ACE)  
                   GetLengthSid(psid) - sizeof(DWORD);
      pace->Mask            = GENERIC_ACCESS;          if (!CopySid(GetLengthSid(psid), &pace->SidStart, psid))
        {
         //__leave;
        }          if (!AddAce(
            pNewAcl,
            ACL_REVISION,
            MAXDWORD,
            (LPVOID)pace,
            pace->Header.AceSize)
      )            {
        // __leave;
        }          // Add the second ACE to the window station.          pace->Header.AceFlags = NO_PROPAGATE_INHERIT_ACE;
      pace->Mask            = WINSTA_ALL;          if (!AddAce(
            pNewAcl,
            ACL_REVISION,
            MAXDWORD,
            (LPVOID)pace,
            pace->Header.AceSize)
      )
      {
        // __leave;
      }          // Set a new DACL for the security descriptor.          if (!SetSecurityDescriptorDacl(
            psdNew,
            TRUE,
            pNewAcl,
            FALSE)
      )
        {
        // __leave;
        }          // Set the new security descriptor for the window station.          if (!SetUserObjectSecurity(hwinsta, &si, psdNew))
         {
           //__leave;
         }          // Indicate success.          bSuccess = TRUE;
   }
   __finally
   {
      // Free the allocated buffers.          if (pace != NULL)
         HeapFree(GetProcessHeap(), 0, (LPVOID)pace);          if (pNewAcl != NULL)
         HeapFree(GetProcessHeap(), 0, (LPVOID)pNewAcl);          if (psd != NULL)
         HeapFree(GetProcessHeap(), 0, (LPVOID)psd);          if (psdNew != NULL)
         HeapFree(GetProcessHeap(), 0, (LPVOID)psdNew);
   }       return bSuccess;    }    BOOL AddAceToDesktop(HDESK hdesk, PSID psid)
{
   ACL_SIZE_INFORMATION aclSizeInfo;
   BOOL                 bDaclExist;
   BOOL                 bDaclPresent;
   BOOL                 bSuccess = FALSE;
   DWORD                dwNewAclSize;
   DWORD                dwSidSize = 0;
   DWORD                dwSdSizeNeeded;
   PACL                 pacl;
   PACL                 pNewAcl;
   PSECURITY_DESCRIPTOR psd = NULL;
   PSECURITY_DESCRIPTOR psdNew = NULL;
   PVOID                pTempAce;
   SECURITY_INFORMATION si = DACL_SECURITY_INFORMATION;
   unsigned int         i;       __try
   {
      // Obtain the security descriptor for the desktop object.          if (!GetUserObjectSecurity(
            hdesk,
            &si,
            psd,
            dwSidSize,
            &dwSdSizeNeeded))
      {
         if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
         {
            psd = (PSECURITY_DESCRIPTOR)HeapAlloc(
                  GetProcessHeap(),
                  HEAP_ZERO_MEMORY,
                  dwSdSizeNeeded );                if (psd == NULL)
              {
              // __leave;
              }                psdNew = (PSECURITY_DESCRIPTOR)HeapAlloc(
                  GetProcessHeap(),
                  HEAP_ZERO_MEMORY,
                  dwSdSizeNeeded);                if (psdNew == NULL)
             {
              // __leave;
             }                dwSidSize = dwSdSizeNeeded;                if (!GetUserObjectSecurity(
                  hdesk,
                  &si,
                  psd,
                  dwSidSize,
                  &dwSdSizeNeeded)
            )
              {
              // __leave;
              }
         }
         else
          {
          //  __leave;
          }
      }          // Create a new security descriptor.          if (!InitializeSecurityDescriptor(
            psdNew,
            SECURITY_DESCRIPTOR_REVISION)
      )
        {
         //__leave;
        }          // Obtain the DACL from the security descriptor.          if (!GetSecurityDescriptorDacl(
            psd,
            &bDaclPresent,
            &pacl,
            &bDaclExist)
      )
        {
         //__leave;
        }          // Initialize.          ZeroMemory(&aclSizeInfo, sizeof(ACL_SIZE_INFORMATION));
      aclSizeInfo.AclBytesInUse = sizeof(ACL);          // Call only if NULL DACL.          if (pacl != NULL)
      {
         // Determine the size of the ACL information.             if (!GetAclInformation(
               pacl,
               (LPVOID)&aclSizeInfo,
               sizeof(ACL_SIZE_INFORMATION),
               AclSizeInformation)
         )
          {
          //  __leave;
          }
      }          // Compute the size of the new ACL.          dwNewAclSize = aclSizeInfo.AclBytesInUse  
            sizeof(ACCESS_ALLOWED_ACE)  
            GetLengthSid(psid) - sizeof(DWORD);          // Allocate buffer for the new ACL.          pNewAcl = (PACL)HeapAlloc(
            GetProcessHeap(),
            HEAP_ZERO_MEMORY,
            dwNewAclSize);          if (pNewAcl == NULL)
       {
        // __leave;
       }          // Initialize the new ACL.          if (!InitializeAcl(pNewAcl, dwNewAclSize, ACL_REVISION))
       {
        //  __leave;
       }          // If DACL is present, copy it to a new DACL.          if (bDaclPresent)
      {
         // Copy the ACEs to the new ACL.
         if (aclSizeInfo.AceCount)
         {
            for (i=0; i < aclSizeInfo.AceCount; i  )
            {
               // Get an ACE.
               if (!GetAce(pacl, i, &pTempAce))
                 {
                  // __leave;
                 }                   // Add the ACE to the new ACL.
               if (!AddAce(
                  pNewAcl,
                  ACL_REVISION,
                  MAXDWORD,
                  pTempAce,
                  ((PACE_HEADER)pTempAce)->AceSize)
               )
                 {
                  // __leave;
                  }
            }
         }
      }          // Add ACE to the DACL.          if (!AddAccessAllowedAce(
            pNewAcl,
            ACL_REVISION,
            DESKTOP_ALL,
            psid)
      )
        {
         // __leave;
         }          // Set new DACL to the new security descriptor.          if (!SetSecurityDescriptorDacl(
            psdNew,
            TRUE,
            pNewAcl,
            FALSE)
      )
        {
         // __leave;
        }          // Set the new security descriptor for the desktop object.          if (!SetUserObjectSecurity(hdesk, &si, psdNew))
       {
        // __leave;
       }          // Indicate success.          bSuccess = TRUE;
   }
   __finally
   {
      // Free buffers.          if (pNewAcl != NULL)
         HeapFree(GetProcessHeap(), 0, (LPVOID)pNewAcl);          if (psd != NULL)
         HeapFree(GetProcessHeap(), 0, (LPVOID)psd);          if (psdNew != NULL)
         HeapFree(GetProcessHeap(), 0, (LPVOID)psdNew);
   }       return bSuccess;
}    //------------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TPaoServForm *PaoServForm;
//------------------------------------------------------------------------------
__fastcall TPaoServForm::TPaoServForm(TComponent* Owner)
   : TForm(Owner)
{
}
//------------------------------------------------------------------------------
void __fastcall TPaoServForm::AddPaoServiceIcon(void)
{
   ZeroMemory(&IconData, sizeof(NOTIFYICONDATA));
   IconData.cbSize=sizeof(NOTIFYICONDATA);
   IconData.hWnd=Handle;
   IconData.uID=0;
   IconData.uFlags=NIF_MESSAGE | NIF_ICON | NIF_TIP;
   IconData.uCallbackMessage=WM_PAOSERVICEMESSAGE;
   IconData.hIcon=this->Icon->Handle;
   strcpy(IconData.szTip, "RaynorPao's Service (Demo)");
   if(Shell_NotifyIcon(NIM_ADD, &IconData))
   {
      Timer1->Enabled=false;
   }
}
//------------------------------------------------------------------------------
void __fastcall TPaoServForm::DelPaoServiceIcon(void)
{
   Shell_NotifyIcon(NIM_DELETE, &IconData);
}
//------------------------------------------------------------------------------
MESSAGE void __fastcall TPaoServForm::PaoServiceMessage(TMessage &Msg)
{
   if(Msg.LParam==WM_LBUTTONDBLCLK)
   {
      Show();
   }
}
//------------------------------------------------------------------------------
MESSAGE void __fastcall TPaoServForm::PaoQueryEndSession(TMessage &Msg)
{
   Msg.Result=1;
   gbCanClose=true;
   Close();
}
//------------------------------------------------------------------------------
void __fastcall TPaoServForm::btnOKClick(TObject *Sender)
{
   Close();
}
//------------------------------------------------------------------------------
void __fastcall TPaoServForm::FormCloseQuery(TObject *Sender,
      bool &CanClose)
{
   CanClose=gbCanClose;
   if(!CanClose)
   {
      Hide();
   }
}
//------------------------------------------------------------------------------
void __fastcall TPaoServForm::Timer1Timer(TObject *Sender)
{
   Timer1->Enabled = false;    }
//------------------------------------------------------------------------------
void __fastcall TPaoServForm::FormCreate(TObject *Sender)
{
   //Timer1->Enabled=true;
   //Timer1->Interval=1000;
   AddPaoServiceIcon();
  
}
//------------------------------------------------------------------------------
void __fastcall TPaoServForm::FormDestroy(TObject *Sender)
{
  // Timer1->Enabled=false;
   DelPaoServiceIcon();
}
//------------------------------------------------------------------------------    void __fastcall TPaoServForm::BitBtn1Click(TObject *Sender)
{
   StartInteractiveClientProcess(Username->Text.c_str(),
                                  NULL,Password->Text.c_str(),
                                  "Notepad.exe",
                                  NULL  );
}
//---------------------------------------------------------------------------    void __fastcall TPaoServForm::FormShow(TObject *Sender)
{
     TIniFile *ini = new TIniFile(ChangeFileExt(Application->ExeName,".ini"));
   Username->Text = ini->ReadString("Login","User","");
   Password->Text = ini->ReadString("Login","Password","");
   delete ini;    
}
 
系統時間:2024-05-07 4:27:20
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!