Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB Shell
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
 
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
 
Shell("netsh wlan stop hostednetwork")
 
Any Ideas or some pointers in the right direction would be most useful
Posted 17-Mar-13 22:19pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello,
 
Here[^] is good discussion thred which might help you.
 
Regards,
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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/[^]
 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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
 
    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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 295
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 18 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100