Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 08-25-2010, 12:37 PM
VictorVG VictorVG is offline
Junior Member
 
Join Date: Aug 2010
Posts: 2
Cool HKEY_LOCALMACHINE...Run - App gets started, but does nothing,simply stays as if paus

Hello,
I have a problem with an app which works fine when the user runs it, but doesn't work when run from "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Curr entVersion\Run".
You can see the app in the "Task manager" doing nothing. The app doesn't have any associated form or window, but as I said before, it works fine if the user runs it manually.
I tried making a little test app. And surprisingly I get the same problem. Only works if you start the app manually. Please, see below:

Code:
program Test;
uses Windows, SysUtils;
{$R *.RES}
var
  aux: integer;
  hf: THandle;
  t: string;
  written: Cardinal;
begin
  hf := CreateFile('TestLog.txt', GENERIC_WRITE, 0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  try
    //You can see the process during 1 minute in "Task manager"
    for aux := 1 to 60 do begin
      Sleep(1000); 
      t := DateTimeToStr(Now) + #13#10;
      WriteFile(hf, t[1], Length(t), written, nil);
    end;
  finally
    CloseHandle(hf);
  end;
end.
If you compile and add an entry to the Windows registry, you could check, that it runs, appears about 1 minute in the "Task manager" and dies. But no file was written to disk.
Instead, if you run the app manually, it writes the file perfectly.
I tested changing the "exe" file by another dummy app with a Form and it works!!! Also tried moving to HKCU, but also it didn't work.

Any idea on what is happening??

Thanks in advance,
VictorVG
Reply With Quote
  #2  
Old 08-25-2010, 02:18 PM
mdixon7610 mdixon7610 is offline
Senior Member
 
Join Date: Sep 2003
Posts: 141
Default

The problem is probably that you're not specifying a full path and filename, only a filename. When you do that the current directory is used, which may not be where the exe is.

For example, if a shortcut is created, that folder will be the "Start In" property of the shortcut. I'm not sure what it is when started from the Run key.

I always specify a full path and filename, as third party controls and things like the Windows common dialog can change the current folder.

I always do something like this:

Code:
sFilename := AppPath + 'testfile.txt'
Code:
Function AppPath():String;
begin
    //Get Path of app exe
    Result := ExtractFilePath(application.ExeName);
    //Add trailing backslash if needed
    if Result[length(Result)] <> '\' then Result:= Result+ '\';
  end;
end;
But even then, if the app is on Vista or Windows 7, the app probably isn't going to have write access to the Program Files folders, so it will end up in the virtual store.

When you say manually, do you mean double-clicking the exe, or running it from a shortcut?
Reply With Quote
  #3  
Old 08-25-2010, 08:38 PM
VictorVG VictorVG is offline
Junior Member
 
Join Date: Aug 2010
Posts: 2
Default

You are right mdixon7610! Thanks for your help! Problem fixed!!
It was just around the path. I am not specifying the full path.
So, when I run the app double-clicking the exe, I don't need to use full path, as the working directory is the same than the app. But, when run from the registry the working path becomes "C:\Documents and Settings\User\Application Data\...", so, the app is running and writting to this folder.
I have tested my app specifying the full path for all the files and it works fine!!!


Thanks again,
VictorVG
Reply With Quote
Reply

Tags
registry, windows

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 04:35 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.