Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

please, how to add start-up behaviour ? 1

Status
Not open for further replies.

deadlykitten

Programmer
Jun 20, 2015
11
RO
hello, i have an application, and i put a shortcut to it inside win+startup folder, and all is ok.

Now i wish to change this approach, by coding it, and so i have used the code listed at the bottom of this post.
The code inputs a key inside HKLM, but there is a windows error when system starts :
start-up_error_PC_lsofxu.png


This error is similar on 3 different computers, running win xp or win 7.

Please, i would apreciate a bit of help :) kindly !

procedure SetAutoStart(AppName, AppTitle: string; bRegister: Boolean);
const RegKey = '\Software\Microsoft\Windows\CurrentVersion\Run'; // Run or RunOnce
var Registry: TRegistry;
begin
Registry := TRegistry.Create;
try Registry.RootKey := HKEY_LOCAL_MACHINE;
if Registry.OpenKey(RegKey, False)
then begin
if bRegister = False then Registry.DeleteValue(AppTitle)
else Registry.WriteString(AppTitle, AppName);
end;
finally Registry.Free;
end;
end;
 
The only think I see missing is the call to CloseKey. Perhaps that is causing the issue.
Code:
procedure SetAutoStart(AppName, AppTitle: string; bRegister: Boolean);
const 
  RegKey = '\Software\Microsoft\Windows\CurrentVersion\Run';
var 
  Registry: TRegistry;
begin
  Registry := TRegistry.Create;
  try
    Registry.RootKey := HKEY_LOCAL_MACHINE;
    if Registry.OpenKey(RegKey, False) then
    try
      if bRegister = False then 
        Registry.DeleteValue(AppTitle)
      else
        Registry.WriteString(AppTitle, AppName);
    finally
      Registry.CloseKey;
    end;
  finally
    Registry.Free;
  end;
end;

Of course, I don't see what values you are passing to this procedure, so it may also stem from the values you are passing, and there is also a possibility that your program needs some resource from your computer that is not available when it is starting up, which would definitely be causing an Access Violation Error.
 
hello majlumbo :) the program tries to read some data from a file, but it can not read it, even if the file exists.

i have used a 30seconds delay, and after windows started, i also started the application by clicking on it, and it worked. However, after the 30seconds timer, the application started automatically, and same error appeared.

In other words, even if the file is there, the application is not allowed to read it.

it may be something about privileges, etc, but i dont have experience with this kind of things :) kindly !
 
i created another application, that uses ShellExecute, so to run the main application. Same problem appears.

i use FileExist to verify if the file is there; FileExist calls this function "FindFirstFile(lpFileName: PChar; var lpFindFileData: TWIN32FindData): THandle; stdcall;", and this function is behaving differently

when i launch application :
... by clicking its own icon, handle = 1461824, or 1452896 , or 1462696 ( seems to be changing )
... through the application that uses ShellExecute, handle = 4294967295 ( this value does not change )
* although file is always there ...

is like the main application can not check for files, unless i launch it normally ... pfff
 
i used a simple memo.lines.loadfromfile, and it worked only when i launched the application

when i tried to start the application by "ShellExecute", or by "editing the HKLM\RUN", it did not work

there is a status, depending on launch conditions, that does not allow acces to files
 
it may be something about privileges
Where is the file located? You should probably put the file under %APPDATA% under some directory you create for your application.

As to your application's Handle changing. The handle would be expected to change every time you run it, if you get the save value, it is purely by happenstance.
 
hello majlumbo :) the file has a very simple content, and it is inside the application folder

path is currentdir\*.txt

i will share a very simple code pretty soon, so to show what is wrong
 
i should have used full paths

different ways of starting the application are causing it to have different default search directory paths

thank you majlumbo :) it was nice talking to you :)
 
Glad to help. At least it put you on the correct path to figure it out on your own.
 
nonono, i did not figure it out myself / just the usual : asking all over the forums :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top