Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Run application on Windows 10 start


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 5 - Last Post: Jan 4, 2016 10:04 AM Last Post By: Remy Lebeau (Te...
Martin Masci

Posts: 29
Registered: 4/3/04
Run application on Windows 10 start  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 30, 2015 7:42 AM
My application doesn't run on Windows 10 start.

On my application the user can set start with windows. I edit the registry to do it ("hkey_local_machine\software\microsoft\windows\currentversion\run" key).

This works with XP and with Seven but not with Windows 10 (I didn't have test Windows 8).

Where is the problem?

Note: my application is not run with administrator right.

Thanks
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Run application on Windows 10 start  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 30, 2015 10:16 AM   in response to: Martin Masci in response to: Martin Masci
Martin wrote:

This works with XP and with Seven but not with Windows 10 (I didn't
have test Windows 8).

Please show your actual code. When I try to write to the Run key on Win7
without running as an admin, I get an access denied error. Which makes sense,
because only admins can write to HKLM.

Note: my application is not run with administrator right.

That is your problem. To fix it, you can either:

1. add a UAC manifest to your app that has the requestedExecutionLevel field
set to asInvoker. This will disable any Registry Virtualization/Redirection.
If your app is run by a user who does not have write access to HKLM, the
write should fail rather than be silently redirected to a user-writable key.

2. when UAC is enabled, have your app run a second copy of itself that is
run elevated (via ShellExecute/Ex() with the "runas" verb), or write a COM
object that is instantiated using the COM Elevation Moniker. Let that elevated
code write to HKLM and then exit without doing anything else. This way,
you don't have to elevate your main app, but you can still write to HKLM
after obtaining admin rights to do so.

--
Remy Lebeau (TeamB)
Martin Masci

Posts: 29
Registered: 4/3/04
Re: Run application on Windows 10 start  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 30, 2015 10:25 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Please show your actual code. When I try to write to the Run key on Win7
without running as an admin, I get an access denied error. Which makes sense,
because only admins can write to HKLM.

I have make a mistake on first post the right key edit by my code is HKEY_CURRENT_USER.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Run application on Windows 10 start  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 30, 2015 10:43 AM   in response to: Martin Masci in response to: Martin Masci
Martin wrote:

I have make a mistake on first post the right key edit by my code is
HKEY_CURRENT_USER.

Considering that the user always has write access to HKCU, it doesn't make
sense for the app to not run at startup if you are actually writing to the
HKCU Run key correctly. Did you verify that your string value is actually
being written to the correct Key? Microsoft has not changed the location
of the Run key, so either you are not actually writing your app info to the
correct key, or you are writing the app info itself incorrectly so Windows
cannot interpret it at startup. Which is again why I ask that you please
show your actual code that is writing to the Registry. When I test using
Win10, without admin rights, it works fine for me, so it has to be a problem
in your code.

--
Remy Lebeau (TeamB)
Martin Masci

Posts: 29
Registered: 4/3/04
Re: Run application on Windows 10 start  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 2, 2016 3:32 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
This is the code:

RunOnWinStart('MyApp', AppPath + 'myapp.exe', False);

procedure RunOnWinStart(const ApTitle, ApPathFile: string; RunOnce: Boolean);
var
Reg: TRegistry;
TheKey: string;
begin
Reg := TRegistry.Create;
Reg.RootKey := HKEY_CURRENT_USER;
TheKey := 'Software\Microsoft\Windows\CurrentVersion\Run';
if RunOnce then TheKey := TheKey + 'Once';

Reg.OpenKey(TheKey, True);
Reg.WriteString(ApTitle, ApPathFile);
Reg.CloseKey;
Reg.Free;
end;

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Run application on Windows 10 start  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 4, 2016 10:04 AM   in response to: Martin Masci in response to: Martin Masci
Martin wrote::

This is the code:

You are not checking the return value of TRegistry.OpenKey() before calling
TRegistry.WriteString(). Also, the TRegistry.Access property defaults to
KEY_ALL_ACCESS, which is asking for too many permissions than you really
need. In this case, all you need is KEY_SET_VALUE permission.

Try this instead:

RunOnWinStart('MyApp', AppPath + 'myapp.exe', False);

procedure RunOnWinStart(const ApTitle, ApPathFile: string; RunOnce: Boolean);
var
Reg: TRegistry;
TheKey: string;
begin
TheKey := 'Software\Microsoft\Windows\CurrentVersion\Run';
if RunOnce then TheKey := TheKey + 'Once';
Reg := TRegistry.Create(KEY_SET_VALUE);
try
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.OpenKey(TheKey, True) then
begin
Reg.WriteString(ApTitle, ApPathFile); // <-- raises an exception if
fails!
Reg.CloseKey;
end else
begin
// key NOT opened/created, use Reg.LastError to find out why...
end;
finally
Reg.Free;
end;
end;
{code}

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02