locked
Process.Start with different credential on .lnk file RRS feed

  • Question

  • Hi.

    My application is essentially a menu that has to execute programs.
    Optionally user can specify different credential (a sort of runas function).

    Everything works very well except for a certain elements: the windows shortcut files (.lnk).

    If I try to execute a .lnk with this simple syntax:
    System.Diagnostics.Process.Start(bi.Path);
    where bi.Path is full path of shortcut the behavior is correct: linked program executes without problems with logged user credential.

    But when I try to execute it with System.Diagnostics.Process.Start overload, specifying username and password or when I use the syntax below, an error is thown ("The specified executable is not a valid Win32 application").

    This is my last attempt:
    Process p = new Process();
    p.StartInfo.FileName = bi.Path;
    //p.StartInfo.Verb="open";
    p.StartInfo.UserName = userName;
    p.StartInfo.Password = password;
    p.StartInfo.UseShellExecute = false;
    p.Start();

    Anyone could help?


    Tuesday, February 10, 2009 4:15 PM

Answers

  • Hi David.

    You are right. The problem could be solved extracting lnk file target.

    But as I already said, my problem was on particular shortcuts (like the Office ones) that seems to have a different structure compared to 'normal shortcuts' (normal shortcut are easy to parse).

    I finally understand what these strange shortcuts are: Windows Installer Advertised shortcuts.
    These kind of files don't contain the usual target file string so the technique to parse them are different.

    When I understand where the problem was, I easily find out a great solution to parse this kind of shortcut.

    This is the link if someone needs this information:

    http://www.geektieguy.com/2007/11/19/how-to-parse-special-lnk-files-aka-msi-shortcuts-aka-windows-installer-advertised-shortcuts-using-c/

    Thank you very much for your patience!!!


    • Marked as answer by epik Friday, February 13, 2009 1:12 PM
    Friday, February 13, 2009 1:12 PM

All replies

  • Use the code from this link to get the properties of the shortcut, and then run it's associated executable directly.
    David Morton - http://blog.davemorton.net/
    Tuesday, February 10, 2009 4:27 PM
    Moderator
  • Hi David.

    I already extract path from .lnk files (with other code than that you point out... no reference to COM object for my app!!!) but the problem still remain for certain kind of shortcut such as Word, Excel and all Office shorcuts, that have no information of destination path in.

    This kind of shortcuts execute normally in first case (current credential) so I would like to find out a way to execute them even with different credential without extracting contained path.
    Tuesday, February 10, 2009 5:15 PM
  • Unfortunately, you can't use the overload of Process.Start that allows you to enter the username and password with a file that isn't an executable file.  The MSDN documentation states so:

    "The file name must represent an executable file in the Start overloads that have userName, password, and domain parameters."

    That means executable, as opposed to a file that can be opened by an executable.  You could, however, use something to add impersonation on your thread or on another thread, which would essentially act the same way.  You would need to begin a thread impersonating another user, and then call the Process.Start from that thread.  Check out the following link for a class that can help you get started with this.

    http://www.codeproject.com/KB/cs/zetaimpersonator.aspx
    David Morton - http://blog.davemorton.net/
    Tuesday, February 10, 2009 6:58 PM
    Moderator
  • Hi David.

    It's weird. Impersonation works well but when I try to start my new process I notice that application is still executing with main account.

    This is an example:

    using (new Impersonator("secondaryAccount", Environment.MachineName, "password"))
    {
      string[] files = Directory.GetFiles(@"c:\Temp\TestImpersonation\" );
      System.Diagnostics.Process.Start(@"c:\Windows\Notepad.exe"); // If I replace executable path with .lnk path the problem remains
    }

    Files in folder c:\Temp\TestImpersonation are correctly enumerated even if main executing account has got no access in that folder.
    But unfortunately new process starts with main account credential (I can see that in task manager) and not with secondaryAccount one.

    May I argue that this is an impossible task?

    Wednesday, February 11, 2009 8:42 AM
  • Anyone can help me?
    Thursday, February 12, 2009 7:34 AM
  • If you have the full path to the filename, you can use the following class to find the executable file that opens the file.  Just pass the filename into the DefaultExecutable method, and the executable name will be returned, then use this executable name to run the file under Process.Start while passing in the username and the password, and your filename as the start argument.

    public class Shell

    {

        [DllImport("Shell32.dll")]

        private static extern uint FindExecutable(string filename, string directory, StringBuilder executable);

     

        public static string DefaultExecutable(string filename)

        {

            StringBuilder builder = new StringBuilder(260);

            FindExecutable(filename, null, builder);

            return builder.ToString();

        }

    }


    David Morton - http://blog.davemorton.net/
    Thursday, February 12, 2009 7:49 PM
    Moderator
  • Sorry David.

    Your code extracts the executable from an associated file (associated by its extension).
    I.e. if I pass to your function this full path: "c:\test.txt" function returns "c:\windows\notepad.exe".

    I don't need this.

    I need a system to execute a linked program having shortcut file (.lnk) with different credential.

    I can't extract the executable file path from .lnk file because some strange kind of shortcut (such as Office ones) don't have path in it.

    I can't use Impersonator class because it doesn't affect Process.Start method.

    I can't use Process.Start with StartInfo attributes UserName and Password because it works only if applied directly on the executable file and not on a shortcut.

    Thank you for trying.

    Friday, February 13, 2009 8:40 AM
  • epik,

    Stand back and think about this for a second.  Think about the logic flow, and you'll get this. What you're describing isn't anything that a little if/else can't solve.  Hint, you don't execute the .lnk file, you execute what the .lnk file is trying to execute.  If the .lnk file contains a path to a non-executable file, then you need to find the executable and run it passing in the non-executable file as an argument.  If the .lnk file contains an excutable, however, you simply need to run the executable with process.

    Maybe the following pseudocode will help. 

    string filename = // file to run. 

    if  (the file extension is a shortcut)
    {
        set filename to the link within the shortcut.  
    }

    if (extension isn't .com, .bat or .exe)
    {
        FindExecutable and run the executable passing in the filename as an argument.

    else
    {
        Run the filename directly using process.
    }


    David Morton - http://blog.davemorton.net/
    Friday, February 13, 2009 12:18 PM
    Moderator
  • Hi David.

    You are right. The problem could be solved extracting lnk file target.

    But as I already said, my problem was on particular shortcuts (like the Office ones) that seems to have a different structure compared to 'normal shortcuts' (normal shortcut are easy to parse).

    I finally understand what these strange shortcuts are: Windows Installer Advertised shortcuts.
    These kind of files don't contain the usual target file string so the technique to parse them are different.

    When I understand where the problem was, I easily find out a great solution to parse this kind of shortcut.

    This is the link if someone needs this information:

    http://www.geektieguy.com/2007/11/19/how-to-parse-special-lnk-files-aka-msi-shortcuts-aka-windows-installer-advertised-shortcuts-using-c/

    Thank you very much for your patience!!!


    • Marked as answer by epik Friday, February 13, 2009 1:12 PM
    Friday, February 13, 2009 1:12 PM
  • Glad to hear you got it working. Thanks for sharing the link. :)
    David Morton - http://blog.davemorton.net/
    Friday, February 13, 2009 3:10 PM
    Moderator