Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I create scheduled tasks in Delph for both XP and Vista, that are capable of running interactive programs

Posted on 2009-05-19
2
Medium Priority
?
680 Views
Last Modified: 2012-05-07
We are getting stuck with scheduled tasks. While we have implemented the solution from
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_22063360.html

It does not work in Vista

If we use 'schtasks' to schedule a task under the current user in Vista, it asks for their password

Is there a way that can work on Vista and XP/
0
Comment
Question by:rmallach
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 18

Expert Comment

by:Johnjces
ID: 24428348
Vista has increased security called User Access Control, UAC. You can turn UAC off in Vista and it will not ask for the users administrator password. I am assuming that you have a good Vista manifest compiled into your application. A vista manifest has some added stuff such as level="asInvoker" or level="asAdmin".

If using the asAdmin moniker, it will ask for the admin password at application startup as I recall, then that's it. Using asInvoker it will ask for an admin password each and everytime your application does "something" that the UAC needs an administrator to perform.

Anyway, from the tidbit of info provided, it seems that Vista UAC is your challenege here.

If unfamiliar with Vista manifest and how to compile and create, just search EE. Lot's of stuff on it.

John
0
 
LVL 3

Accepted Solution

by:
fjocke earned 2000 total points
ID: 24646162
The code below can be used to launch a process as system user.

CreateSystemProcess('schtasks.exe');

You can try and see if that works out for you.
//coded by lord_viper
//special thanx for magic_h2001
function DebugPrivilege(ToEnable:Boolean):Boolean;
var
 OldTokenPrivileges, TokenPrivileges: TTokenPrivileges;
 ReturnLength: DWORD;
 hToken: THandle;
 Luid: Int64;
begin
 Result:=False; hToken:=0;
 if not OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES,hToken) then Exit;
 try
  if not LookupPrivilegeValue(nil,'SeDebugPrivilege',Luid) then Exit;
  TokenPrivileges.Privileges[0].luid:=Luid;
  TokenPrivileges.PrivilegeCount:=1;
  TokenPrivileges.Privileges[0].Attributes:=0;
  AdjustTokenPrivileges(hToken,False,TokenPrivileges,SizeOf(TTokenPrivileges),OldTokenPrivileges,ReturnLength);
  OldTokenPrivileges.Privileges[0].luid:=Luid;
  OldTokenPrivileges.PrivilegeCount:=1;
  if ToEnable then OldTokenPrivileges.Privileges[0].Attributes:=TokenPrivileges.Privileges[0].Attributes or SE_PRIVILEGE_ENABLED
  else OldTokenPrivileges.Privileges[0].Attributes:=TokenPrivileges.Privileges[0].Attributes and (not SE_PRIVILEGE_ENABLED);
  Result:=AdjustTokenPrivileges(hToken,False,OldTokenPrivileges,ReturnLength,PTokenPrivileges(nil)^,ReturnLength);
 finally
  CloseHandle(hToken);
 end;
end;
 
function GetProcessId(ProcName: string): DWORD;
var
  hSnap: THandle;
  ProcInfo: TProcessEntry32;
begin
 Result:=0;
 hSnap:= CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
 if (hSnap=0) or (hSnap=INVALID_HANDLE_VALUE) then Exit;
 ProcInfo.dwSize := SizeOf(ProcessEntry32);
 if Process32First(hSnap, ProcInfo) then
 repeat
  if UpperCase(ProcName)=UpperCase(ProcInfo.szExeFile) then
    Result:=ProcInfo.th32ProcessID;
 until not Process32Next(hSnap, ProcInfo);
 CloseHandle(hSnap);
end;
 
function CreateSystemProcess(szProcessName: LPTSTR): BOOL;
var
hProcess: THANDLE;
hToken, hNewToken: THANDLE;
dwPid: DWORD;
pOldDAcl: PACL;
pNewDAcl: PACL;
bDAcl: BOOL;
bDefDAcl: BOOL;
dwRet: DWORD;
pSacl: PACL;
pSidOwner: PSID;
pSidPrimary: PSID;
dwAclSize: DWORD;
dwSaclSize: DWORD;
dwSidOwnLen: DWORD;
dwSidPrimLen: DWORD;
dwSDLen: DWORD;
ea: EXPLICIT_ACCESS;
pOrigSd: PSECURITY_DESCRIPTOR;
pNewSd: PSECURITY_DESCRIPTOR;
si: STARTUPINFO;
pi: PROCESS_INFORMATION;
bError: BOOL;
Label Cleanup;
begin
pOldDAcl:= nil;
pNewDAcl:= nil;
pSacl:= nil;
pSidOwner:= nil;
pSidPrimary:= nil;
dwAclSize:= 0;
dwSaclSize:= 0;
dwSidOwnLen:= 0;
dwSidPrimLen:= 0;
hToken:= 0;
hNewToken:= 0;
hProcess:= 0;
pi.hProcess:= 0;
pi.hThread:= 0;
pOrigSd:= nil;
pNewSd:= nil;
if not DebugPrivilege(True) then
    begin
      bError := TRUE;
      Goto Cleanup;
    end;
dwPid := GetProcessId('WINLOGON.EXE');
If dwPid = High(Cardinal) Then
    begin
      bError := TRUE;
      Goto Cleanup;
    end;
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION,FALSE,dwPid);
If hProcess = 0 Then
    begin
      bError := TRUE;
      Goto Cleanup;
    end;
If not OpenProcessToken(hProcess,READ_CONTROL or WRITE_DAC,hToken) Then
    begin
      bError := TRUE;
      Goto Cleanup;
    end;
ZeroMemory(@ea, Sizeof(EXPLICIT_ACCESS));
BuildExplicitAccessWithName(@ea,'Everyone',TOKEN_ALL_ACCESS,GRANT_ACCESS,0);
If not GetKernelObjectSecurity(hToken,DACL_SECURITY_INFORMATION,pOrigSd,0,dwSDLen) Then
    begin
      If GetLastError = ERROR_INSUFFICIENT_BUFFER Then
        begin
          pOrigSd := HeapAlloc(GetProcessHeap(),$00000008,dwSDLen);
          If pOrigSd = nil Then
            begin
              bError := TRUE;
              Goto Cleanup;
            end;
          If not GetKernelObjectSecurity(hToken,DACL_SECURITY_INFORMATION,pOrigSd,dwSDLen,dwSDLen) Then
            begin
              bError := TRUE;
              Goto Cleanup;
            end;
        end
      Else
        begin
          bError := TRUE;
          Goto Cleanup;
        end;
    end;
If not GetSecurityDescriptorDacl(pOrigSd,bDAcl,pOldDAcl,bDefDAcl) Then
    begin
      bError := TRUE;
      goto Cleanup;
    end;
dwRet := SetEntriesInAcl(1,@ea,pOldDAcl,pNewDAcl);
If dwRet <> ERROR_SUCCESS Then
    begin
      pNewDAcl := nil;
      bError := TRUE;
      goto Cleanup;
    end;
If not MakeAbsoluteSD(pOrigSd,pNewSd,dwSDLen,pOldDAcl^,dwAclSize,pSacl^,dwSaclSize,pSidOwner,dwSidOwnLen,pSidPrimary,dwSidPrimLen) Then
    begin
      If GetLastError = ERROR_INSUFFICIENT_BUFFER Then
        begin
          pOldDAcl := HeapAlloc(GetProcessHeap(),$00000008,dwAclSize);
          pSacl := HeapAlloc(GetProcessHeap(),$00000008,dwSaclSize);
          pSidOwner := HeapAlloc(GetProcessHeap(),$00000008,dwSidOwnLen);
          pSidPrimary := HeapAlloc(GetProcessHeap(),$00000008,dwSidPrimLen);
          pNewSd := HeapAlloc(GetProcessHeap(),$00000008,dwSDLen);
          If (pOldDAcl = nil) or
            (pSacl = nil) or
            (pSidOwner = nil) or
            (pSidPrimary = nil) or
            (pNewSd = nil) Then
            begin
              bError := TRUE;
              goto Cleanup;
            end;
          If not MakeAbsoluteSD(pOrigSd,pNewSd,dwSDLen,pOldDAcl^,dwAclSize,pSacl^,dwSaclSize,pSidOwner,dwSidOwnLen,pSidPrimary,dwSidPrimLen) Then
            begin
              bError := TRUE;
              goto Cleanup;
            end;
        end
      Else
        begin
          bError := TRUE;
          goto Cleanup;
        end;
    end;
If not SetSecurityDescriptorDacl(pNewSd,bDAcl,pNewDAcl,bDefDAcl) Then
    begin
      bError := TRUE;
      goto Cleanup;
    end;
If not SetKernelObjectSecurity(hToken,DACL_SECURITY_INFORMATION,pNewSd) Then
    begin
      bError := TRUE;
      goto Cleanup;
    end;
If not OpenProcessToken(hProcess,TOKEN_ALL_ACCESS,hToken) Then
    begin
      bError := TRUE;
      goto Cleanup;
    end;
If not DuplicateTokenEx(hToken,TOKEN_ALL_ACCESS,nil,SecurityImpersonation,TokenPrimary,hNewToken) Then
    begin
      bError := TRUE;
      goto Cleanup;
    end;
ZeroMemory(@si,Sizeof(STARTUPINFO));
si.cb := Sizeof(STARTUPINFO);
ImpersonateLoggedOnUser(hNewToken);
If not CreateProcessAsUser(hNewToken,nil,szProcessName,nil,nil,FALSE,0,nil,nil,si,pi) Then
    begin
      bError := TRUE;
      goto Cleanup;
    end;
bError := FALSE;
Cleanup:
If pOrigSd <> nil Then HeapFree(GetProcessHeap(),0,pOrigSd);
If pNewSd <> nil Then HeapFree(GetProcessHeap(),0,pNewSd);
If pSidPrimary <> nil Then HeapFree(GetProcessHeap(),0,pSidPrimary);
If pSidOwner <> nil Then HeapFree(GetProcessHeap(),0,pSidOwner);
If pSacl <> nil Then HeapFree(GetProcessHeap(),0,pSacl);
If pOldDAcl <> nil Then HeapFree(GetProcessHeap(),0,pOldDAcl);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
CloseHandle(hToken);
CloseHandle(hNewToken);
CloseHandle(hProcess);
Result:= not bError;
end;

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question