Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Day

I've spent the day trying to find a solution. Maybe you can help.
I'm trying to execute a simple cmd line code in Visual Basic.
The code runs without problem as a bat file run as Administrator however I'm trying to code it as a shell command in Visual basic 2012 without having to back reference it to a bat file.

The Command line is
netsh wlan set hostednetwork mode=allow ssid=MySSID key=12345678 keyUsage=persistent

Ive tried

VB
Shell("netsh wlan set hostednetwork mode=allow ssid=MySSID key=12345678 keyUsage=persistent")
    Shell("netsh wlan start hostednetwork")


but the cmd box simply flashes and no virtual network is created.

The strange thing is that if I execute and start the hostednetwork using by bat file I can run the following VB code and it stops the network just fine

VB
Shell("netsh wlan stop hostednetwork")


Any Ideas or some pointers in the right direction would be most useful
Posted

Hello,

Here[^] is good discussion thred which might help you.

Regards,
 
Share this answer
 
Thanks Prasad
The Solution above does indeed have a similar result as using the shell command.
I have since found that my command windows are requesting admin rights.

This Referance
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/ac95030f-1279-4927-9eb9-cd2d85990571/[^]
VB
Sub RunAdmin(ByVal fileToExecute As String)
       Dim process As System.Diagnostics.Process = Nothing
       Dim processStartInfo As System.Diagnostics.ProcessStartInfo
       processStartInfo = New System.Diagnostics.ProcessStartInfo
       processStartInfo.FileName = fileToExecute
       processStartInfo.Verb = "runas"
       processStartInfo.Arguments = ""
       processStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
       processStartInfo.UseShellExecute = True
       Try
           process = System.Diagnostics.Process.Start(processStartInfo)
       Catch ex As Exception
           MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
       Finally

           If Not (process Is Nothing) Then
               process.Dispose()
           End If

       End Try

   End Sub


kaymaf

Gives a workable code on getting the command prompt to run as an administrator. Now Im just trying to figure where to input the netsh commands into this script
 
Share this answer
 
Thanks again for your help Prasad
I had previously found this answer but ignored it. I found it useful in finding my final solution.
All I have to do now is link in my input boxes.

Here is the final Solution which calls the user to run the cmd window as administrator (thanks to Kaymaf) and then runs the netsh commands.

change the /k to a /c to hide the cmd prompt window

VB
    Private Sub Temp_Click(sender As Object, e As EventArgs) Handles Temp.Click
        Dim process As System.Diagnostics.Process = Nothing
        Dim processStartInfo As System.Diagnostics.ProcessStartInfo
        processStartInfo = New System.Diagnostics.ProcessStartInfo
        processStartInfo.FileName = "cmd.exe"
        processStartInfo.Verb = "runas"
        processStartInfo.Arguments = ""
        
'Enter netsh commands here'
        processStartInfo.Arguments = "/k netsh wlan set hostednetwork mode = allow  ssid=MyNetworkID key=12345678 keyUsage=persistent"
        processStartInfo.Arguments = "/k netsh wlan start hostednetwork"
'End netsh commands'
    
    processStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
        processStartInfo.UseShellExecute = True
        Try
            process = System.Diagnostics.Process.Start(processStartInfo)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally

            If Not (process Is Nothing) Then
                process.Dispose()
            End If

        End Try
    End Sub
End Class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900