none
CreateProcessWithLogonW RRS feed

  • Pergunta

  • I have been trying to create a 'simple' Visual Basic.net program which will allow me to run another program under a different user... basically simulate the functionality of RUNAS.

    I have tried using a CreateProcessWithLogonW command

                retcode = CreateProcessWithLogonW(g_strUserName, g_strDomain, g_strPassword, _
                    LOGON_WITH_PROFILE, 0&, "notepad.exe", "c:\", 0&, 0&, si, pi)

    And


            Dim myProcess As System.Diagnostics.ProcessStartInfo
            myProcess = New System.Diagnostics.ProcessStartInfo()
            Dim password As Security.SecureString
            password = New Security.SecureString


            g_strUserName = "CinAccess"
            g_strPassword = "test"


            Dim c As Char
            For Each c In g_strPassword
                password.AppendChar(c)
            Next c


            myProcess.Password = password

            myProcess.UserName = g_strUserName

            myProcess.LoadUserProfile = True
            myProcess.UseShellExecute = False

            myProcess.WorkingDirectory = path
            myProcess.RedirectStandardOutput = True

            Try
                Process.Start("\notepad.exe", g_strUserName, password, g_strDomain)
            Catch ex As Exception

                MsgBox(ex.Message, MsgBoxStyle.Critical, "WARNING!")

            End Try

    Both of these ways of doing things have given me the following error:

    ---------------------------
    WARNING!
    ---------------------------
    The directory name is invalid
    ---------------------------
    OK   
    ---------------------------

    I have spent quite a bit of time looking on the internet and nothing suggested as worked. If anyone has any suggestions then I would really appreciate it.

    Thank you in advance

    Richard
    quinta-feira, 5 de outubro de 2006 10:07

Todas as Respostas

  • remote the backslash from notepad.exe :-) it should just be notepad.exe
    quinta-feira, 5 de outubro de 2006 10:15
    Moderador
  • Sorry, that seems to have been a typo, as this did not make a difference
    quinta-feira, 5 de outubro de 2006 10:16
  • one thing I just noticed is that you aren not giving Process the ProcessStartInfo.....so its not going to be using the configured Process you wish to start with.

    Where is WorkingDirectory set to? this shouldnt really matter since notepad is a Windows Shell command....

    Be sure that the WorkingDirectory DOES exist, I think this could be one reason for the error - infact it is the reason I believe. Make sure also that the user does have permission to access that directory but I think a SecurityException would be thrown instead.

    I've tried doing it in my code from yours and seems to work ok....

    Here is the modified code, whether it will make a difference, im not sure but hope so!



    Dim myProcess As new System.Diagnostics.ProcessStartInfo("Notepad.exe")

            Dim password As Security.SecureString
            password = New Security.SecureString


            g_strUserName = "CinAccess"
            g_strPassword = "test"


            Dim c As Char
            For Each c In g_strPassword
                password.AppendChar(c)
            Next c


            myProcess.Password = password

            myProcess.UserName = g_strUserName

            myProcess.LoadUserProfile = True
            myProcess.UseShellExecute = False

            myProcess.WorkingDirectory = path
            myProcess.RedirectStandardOutput = True

            Try
                Process.Start(myProcess) ', g_strUserName, password, g_strDomain)
            Catch ex As Exception

                MsgBox(ex.Message, MsgBoxStyle.Critical, "WARNING!")

            End Try

     

    quinta-feira, 5 de outubro de 2006 10:29
    Moderador
  • Hi ahmedilyas thank you very much for your help.

    I am still getting the same problem,

    ---------------------------
    WARNING!
    ---------------------------
    The directory name is invalid
    ---------------------------
    OK  
    ---------------------------

    The path is currrently set to:

    path = System.IO.Path.GetDirectoryName("%SystemRoot%\system32\")

    Thanks again for any help
    quinta-feira, 5 de outubro de 2006 10:39
  • Try doing:

    path = Environment.GetFolderPath(Environment.SpecialFolder.System)

    in stead of the whole "%systemroot%"

    as this will get the correct string path of the System32 folder

     

    does this work?

    quinta-feira, 5 de outubro de 2006 10:45
    Moderador
  • No... this gives me the following error:

    ---------------------------
    WARNING!
    ---------------------------
    The handle is invalid
    ---------------------------
    OK  
    ---------------------------


    Thanks again for any help
    quinta-feira, 5 de outubro de 2006 10:48
  • something wrong somewhere...lol. I tried the code and used to doing the Process() stuff. Are you sure your not doing anything else somewhere from this happening?

    What about if you just do only:

    Dim theProcess as new System.Diagnostics.ProcessStartInfo("Notepad.exe")

    System.Diagnostics.Process.Start(theProcess)

     

    you may wish to comment out/take out bits and bobs and start from the basics working upwards adding in the other fancy stuff and see where it hits the problem

    I think its to do with the useraccount you are trying to run the process under

    It could be to do with the RedirectStandardOutput as some accounts may have limitation in if they can redirect the outputs depending on the type of account you are executing under

    quinta-feira, 5 de outubro de 2006 10:55
    Moderador
  • Thanks for you help on this.

    If I comment out the following to lines:

            myProcess.Password = password
            myProcess.UserName = g_strUserName

    I.e. running the process as someone other than the user that is logged on , I get the directory invalid error.

    I think it could be a permission thing, but not sure. As I would like to solve any problem programmatically.

    Thanks again
    quinta-feira, 5 de outubro de 2006 11:02
  • interesting! :-)

    Can you run the process on its own without any fancy stuff as suggested in my previous response? Just a simple PSI running notepad? (Which will use your own account by default)

    Then try adding in the account credentials and see what happens

    then set the path and see what happens

    then set the redirectoutput and see what happens....

     

    Tried giving the useraccount in question Admin priviledges? (just for now for testing) - what happens?

    Could I also ask why do you have to set the working directory to the System folder?

    quinta-feira, 5 de outubro de 2006 11:06
    Moderador
  • Running the process without any fancy stuff works fine.

    The path does not make a difference (it would appear), nor does redirectoutput.

    It just seems to be the adding a different user and password creates the error:

    ---------------------------
    WARNING!
    ---------------------------
    The directory name is invalid
    ---------------------------
    OK  
    ---------------------------

    If I make the user (CinAccess) an admin user of the local  machine it works fine.
    quinta-feira, 5 de outubro de 2006 11:24
  • This is for anyone who has a similar problem since this is an old thread.

     

    I had this same problem and I couldn't figure out how to get it working. It turns out that the error is a result of the user not having permissions to access the directory where the exe is running from. I tested my exe in VS and it failed with "directory is invalid", I then moved the exe to a neutral directory c:\whatever and it ran fine as the other user.

     

    Therefore, it is a permission problem and changing the user to admin will fix it. However, there are other solutions.

     

    quarta-feira, 7 de novembro de 2007 19:01
  •  http://support.microsoft.com/kb/832434

    Here's the real answer, Runas cannot read a mapped drive, the start in command line for the DOS prompt is generally %HomeDrive%%HomePath% which is a map, the way I've worked around this (my project is widely deployable and attempting to change the start in for so many machines is not advised) error is to open a shell, sendkey "cd c:\~" this changes the working directory to a non mapped directory which runas can read, and then run my actual runas program i.e , program 1 is specifically designed to run program 2 from a dos shell, then a terminate command ends the dos shell. Once opened in this manner program 2 is able to run as if it were in the DOS prompt that opened it.    
    quinta-feira, 31 de julho de 2008 19:28
  • This is indeed a permissions issue (nothing to do with the directory being invalid or not there) to test i'd recommend giving the user running the command permission to the directory containing the .exe.  I've not tried Franklin's idea but it seems sound enough. 
    quinta-feira, 16 de janeiro de 2020 17:07