views:

536

answers:

4

Hi, I am trying to add my program run in Windows 7 startup, but it doesn't work. My program has an embedded UAC manifest.

My current way is by adding a string value at HKCU..\Run.

I found a manual solution for Vista from http://social.technet.microsoft.com/Forums/en/w7itprosecurity/thread/81c3c1f2-0169-493a-8f87-d300ea708ecf

  1. Click Start, right click on Computer and choose “Manage”.
  2. Click “Task Scheduler” on the left panel.
  3. Click “Create Task” on the right panel.
  4. Type a name for the task.
  5. Check “Run with highest privileges”.
  6. Click Actions tab.
  7. Click “New…”.
  8. Browse to the program in the “Program/script” box. Click OK.
  9. On desktop, right click, choose New and click “Shortcut”.
  10. In the box type: schtasks.exe /run /tn TaskName where TaskName is the name of task you put in on the basics tab and click next.
  11. Type a name for the shortcut and click Finish.

Additionally, you need to run the saved scheduled task shortcut to run the program instead of running the application shortcut to ignore the IAC prompt. When startup the system will run the program via the original shortcut. Therefore you need to change the location to run the saved task. Please:

  1. Open Regedit.
  2. Find the entry of the startup item in Registry. It will be stored in one of the following branches.
    • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
    • HKEY_USERS.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Run
    • HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
  3. Double-click on the correct key, change the path to the saved scheduled task you created.

Is there any free code to add item with privileges option in scheduled task? I haven't found the free one in torry.net.

Thanks a lot.

+2  A: 

There is a COM component called TaskScheduler. Some documentation is at http://msdn.microsoft.com/en-us/library/aa384006(v=VS.85).aspx. An example in C# is included in the Windows 7 Training Kit.

Kate Gregory
+2  A: 

JCL provides a Delphi interface unit to control Microsoft task schedule service. Its name is JclTask.pas. They also provide a demo application for adding/removing/showing Windows tasks in "jcl\examples\windows\tasks" folder.

Also, if commercial components are OK with you, SiComponents provides VCL Scheduling Agent, which is a VCL wrapper for Windows Task Scheduler, and supports new interface provided in Windows Vista.

vcldeveloper
+1  A: 

Why not just add your program to the Task Scheduler? See the command line options for schtasks.exe at MSDN for options. Your command line might look something like this:

schtasks.exe /Create /RU {username} /RP {password} /SC ONLOGON /TN {task name} /TR {file to run} /RL HIGHEST.

The "/RL HIGHEST" is what lets the task run with the admin level privileges.

Jason Swager
Probably the easiest way, even the code not based on Delphi but I'll try in the early morning.
I guess I can't use schtasks.exe because my program have to fill username and password, if required.
Yes - SOME security is needed to be able to run at an elevated permissions level. If no security was required, I guess any program - virus, trojan, logger - could easily add a task to elevate itself. Something from the user is required; either a dialog acknowledgement or the input of extra information.
Jason Swager
A: 

Are you asking how to get your app to start(launch) at Windows 7 startup or are you asking how to change your apps privileges at Windows 7 startup?

I want to get my app to start at Windows 7 startup. My program has an embedded UAC manifest
It looks like some one took the liberty of changing the title of your question. I recently went through the exercise of getting the "start app at windows startup" to work on Vista after it had been working successfully on XP for a long time. On XP my app would write a key to the Registry but this was crashing Vista. So rethinking this, I now think the best way to go is not to write to the Registry but instead to programmaticly create a "Shortcut" and write it into the Startup "Directory". This works for all versions of windows. If your interested, I'll post the code.
IIRC windows startup blocks startup apps that require elevation because it would annoy the user with prompts.
Larry Osterman
I've tried to create shortcut and it doesn't work at Windows 7. Why this simple thing become sophicasted problem at Windows 7 :) . I guess I am gonna look JCL, even my prediction is JCL use "at.exe" instead of "schtasks.exe"
iira: See this blog post: http://blogs.msdn.com/uac/archive/2006/08/23/715265.aspx What you're trying to do is explicitly not supported because it would annoy users.
Larry Osterman
Thanks Larry. Btw, If I use COM to elevate my program. Is there any UAC confirmation prompts?