CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    [RESOLVED] Run Key Copies EXE to System32

    I am attempting to make an application run on startup for every user on the local machine (pretty easy, I know) however if I place an entry into the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run key, it starts the application fine but "copies" the executable and runs it in the Windows\system32 folder which causes problems with licensing and the like. I am using the full path to the executable, so its not relative to the system32 folder...
    Whats going on here? Why doesn't it run it from the executable folder where it is located?
    Last edited by RaleTheBlade; June 23rd, 2009 at 01:18 PM.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Run Key Copies EXE to System32

    What do you mean it 'copies'? Physically copies? Or the current directory within the program has been set to \system32? If the latter, and you are using GetCurrentDirectory within your code, I would suggess that you don't rely on GetCurrentDirectory; instead use GetModuleFileName to locate the full path of your executable.

  3. #3
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: Run Key Copies EXE to System32

    Well... so far your right Arjay. The working directory gets changed whenever you use a registry entry in the HKLM\...\Run key to start the application and the application is acting like its running in Windows\system32. Now, how to go about fixing this. Is there anything you can do to prevent the directory from being changed? Im only seeing two options out of this one:

    1) Change the directory back to the executable directory when the application is started by using Directory.SetCurrentDirectory()

    2) Change all of my paths that reference items in the .exe directory to have absolute paths.

    Any other ideas? Thanks for the help
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Run Key Copies EXE to System32

    Sorry about the previous Win32 answers - I thought we were in a different forum.

    The GetDirectory approach doesn't work too well because this path can be changed at any time. It can be changed by a 3rd party component or by the system, so it just isn't reliable. The other way it's changed is at program startup as you have found out.

    What you want to use is the Assembly.GetEntryAssembly method. This will always point to the current [physical] location of the process exe.

    Don't use absolute paths. Sure, it may work okay today, if you need to move the exe, or run it on another machine, or... too fragile.

  5. #5
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: Run Key Copies EXE to System32

    I understand And by absolute path I meant doing something like:

    Code:
    // This is just an example, not real code
    string exePath = Assembly.GetEntryAssembly().Location;
    string exeFolder = Path.GetDirectoryName(exePath);
    string exeConfigPath = exeFolder + "\\myapp.config";
    But that requires reflection. I know that Directory.GetCurrentDirectory() is unreliable so I tend to avoid its use. However, this whole startup thing with the registry key was unexpected so now its time to fix it. Ill just have to reset the current working directory on startup and go through and find all of my relative paths (I know... big no no) and fix them as well.

    Thanks for the help

    EDIT: Oh, and i like Win32... if I had it my way I would do everything in Win32. Gives you so much more power and I know what the heck is going on at a much lower level. I always say "Its my memory! Let ME clean it up!", hahaha
    Last edited by RaleTheBlade; June 23rd, 2009 at 08:26 PM.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Run Key Copies EXE to System32

    Look into Click Once deployment. You can have an app that doesn't get installed on the client, which runs ONLY when the source is available
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Run Key Copies EXE to System32

    Quote Originally Posted by RaleTheBlade View Post
    But that requires reflection.
    Not really. It requires functionality that is located within the reflection assembly. Why is reflection an issue?

  8. #8
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: Run Key Copies EXE to System32

    Quote Originally Posted by dglienna View Post
    Look into Click Once deployment. You can have an app that doesn't get installed on the client, which runs ONLY when the source is available
    We have looked into ClickOnce but we needed an installer with more options for configuration at install and uninstall time so we went ahead and used the Windows Installer.

    Quote Originally Posted by Arjay
    Not really. It requires functionality that is located within the reflection assembly. Why is reflection an issue?
    I just tend to avoid it if I can... you know, performance hit and the like. But its not really an issue in this case so Im going to go ahead and use it
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Run Key Copies EXE to System32

    Quote Originally Posted by RaleTheBlade View Post
    I just tend to avoid it if I can... you know, performance hit and the like. But its not really an issue in this case so Im going to go ahead and use it
    Yeah, I doubt that particular function is doing any real reflecting. At any rate, if you are concerned about performance, just call the method once at the start of your program and save the result.

    Something like:

    Code:
    public static string GetAppFolder( )
    {
      static string appFolder = String.Empty;
    
      if( String.IsNullOrEmpty( appFolder ) )
      {
        appFolder = Path.GetDirectoryName( Assembly.GetEntryAssembly( ).Location );
      }
    
      return appFolder;
    }

  10. #10
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: Run Key Copies EXE to System32

    Quote Originally Posted by Arjay View Post
    Yeah, I doubt that particular function is doing any real reflecting. At any rate, if you are concerned about performance, just call the method once at the start of your program and save the result.

    Something like:

    Code:
    public static string GetAppFolder( )
    {
      static string appFolder = String.Empty;
    
      if( String.IsNullOrEmpty( appFolder ) )
      {
        appFolder = Path.GetDirectoryName( Assembly.GetEntryAssembly( ).Location );
      }
    
      return appFolder;
    }
    Sorry Arjay but I think your getting your C++ and C# lines crossed again I don't believe that declaring a static local variable in C# is legal, although I do know where you are going with the idea. I will just get the executable directory at startup, save it "globally" somehow, and use it when I need to. I believe this problem is toast, Im marking this thread resolved Thanks for the help
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Run Key Copies EXE to System32

    Quote Originally Posted by RaleTheBlade View Post
    Sorry Arjay but I think your getting your C++ and C# lines crossed again
    Yep, so just make the variable a private member of the class that contains the method.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured