We help IT Professionals succeed at work.

Process.Start code not working when trying to launch as a specified user

149 Views
Last Modified: 2018-11-05
I found this code that should run a program as a different user, but it errors and doesn't run the program.  Is there something I'm not doing correctly?
Imports System.Security
Imports System.ComponentModel

Module Module1

    Sub Main()
        Dim username As String = "MyUser"
        Dim password As SecureString = ConvertToSecureString("MyPassword")
        Dim domain As String = "MyDomain"
        Dim filename As String = "c:\windows\notepad.exe"
        Try
            System.Diagnostics.Process.Start(filename, username, password, domain)
        Catch ex As Win32Exception
            System.Windows.Forms.MessageBox.Show("Something went wrong!", "Error")
        End Try
    End Sub
    Function ConvertToSecureString(ByVal str As String)
        Dim password As New SecureString
        For Each c As Char In str.ToCharArray
            password.AppendChar(c)
        Next
        Return password
    End Function
End Module

Open in new window

Comment
Watch Question

Senior Software Developer
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
BrianSystems Administrator

Author

Commented:
That's awesome!  Thanks Russ!  But I am getting an error for "MyPassword".ToSecureString.  The error is 'ToSecureString' is not a member of 'String'.  Maybe I need another Imports?
Russ SuterSenior Software Developer
CERTIFIED EXPERT

Commented:
As I said, my VB is a little rusty. ToSecureString is a method. Perhaps it needs the parentheses () at the end?
Russ SuterSenior Software Developer
CERTIFIED EXPERT

Commented:
DOH! It might have been helpful if I'd have included the Extension class declaration that houses the "ToSecureString()" method.
Public Class LocalExtensions
    
    Public Shared Function ToSecureString(this ByVal password As String) As SecureString
        Dim secure As SecureString = New SecureString
        For Each c As Char In password
            secure.AppendChar(c)
        Next
        Return secure
    End Function
End Class

Open in new window

I'm not 100% certain if extension classes are allowed in VB.NET so here's another version that should work just fine.
    Public Shared Function ToSecureString(ByVal password As String) As SecureString
        Dim secure As SecureString = New SecureString
        For Each c As Char In password
            secure.AppendChar(c)
        Next
        Return secure
    End Function

Open in new window

You'd just call that as a regular function like this:
startInfo.Password = ToSecureString("MyPassword")

Open in new window

BrianSystems Administrator

Author

Commented:
Thanks Russ for giving such clear and precise information!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions