Cannot normally end a module using shell32.dll

jlhuber

New member
Joined
Jun 19, 2004
Messages
3
Programming Experience
10+
Help!

I am trying to programatically enable and disable LAN connections. The code works just fine, BUT it hangs. All of the code is below. It is stripped down to perform only one function (turn off the LAN connection). How can I get this to end normally? I am interested because I ultimately want the "main" routine to be a function returning a code.

I've tried set objects to "nothing". I've tried moving the code to a thread and killing the thread. I've tried making it a separate class. I've even tried to put it in a Windows form and run it as an application. Nothing seems to keep it from hanging.

HELP!

Module Module1
Public Sub Main()
process1()
' the task will not end here, just hangs
Exit Sub
End Sub
Private Sub process1()
Const ssfCONTROLS = 3
Dim ConnectionName As String = "Local Area Connection"
Dim DisableVerb As String = "Disa&ble"
Dim ShellApp As New Shell32.Shell
Dim ControlPanel As Shell32.Folder = ShellApp.NameSpace(ssfCONTROLS)
Dim FolderItem As Shell32.FolderItem
Dim NetworkFolder As Shell32.Folder
Dim LANConnection As Shell32.FolderItem
For Each FolderItem In ControlPanel.Items()
If FolderItem.Name = "Network and Dial-up Connections" Then
NetworkFolder = FolderItem.GetFolder
Exit For
End If
Next
If NetworkFolder Is Nothing Then
Exit Sub
End If
For Each FolderItem In NetworkFolder.Items()
If FolderItem.Name = ConnectionName Then
LANConnection = FolderItem
Exit For
End If
Next
If LANConnection Is Nothing Then
Exit Sub
End If
Dim DisableVerbItem, Verb As Shell32.FolderItemVerb
For Each Verb In LANConnection.Verbs
If Verb.Name = DisableVerb Then
DisableVerbItem = Verb
End If
Next
DisableVerbItem.DoIt()
End Sub
End Module
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,779
Location
Sydney, Australia
Programming Experience
10+
Please use the
VB.NET:
 tags in the editor to make your code more readable.  If your code hangs then I would think it must be waiting for some method to complete.  What line of code does it end up waiting on?
 

jlhuber

New member
Joined
Jun 19, 2004
Messages
3
Programming Experience
10+
Reply to jmcilhinney

VB.NET:
Module Module1 
Public Sub Main() 
   process1() 
   ' the task will not end here, just hangs 
   Exit Sub	 '<<<<<<<<<<<<< the program hangs here 
End Sub 
Private Sub process1() 
   Const ssfCONTROLS = 3 
   Dim ConnectionName As String = "Local Area Connection" 
   Dim DisableVerb As String = "Disa&ble" 
   Dim ShellApp As New Shell32.Shell 
   Dim ControlPanel As Shell32.Folder = ShellApp.NameSpace(ssfCONTROLS) 
   Dim FolderItem As Shell32.FolderItem 
   Dim NetworkFolder As Shell32.Folder 
   Dim LANConnection As Shell32.FolderItem 
   For Each FolderItem In ControlPanel.Items() 
	  If FolderItem.Name = "Network and Dial-up Connections" Then 
		 NetworkFolder = FolderItem.GetFolder 
		 Exit For 
	  End If 
   Next 
   If NetworkFolder Is Nothing Then 
	  Exit Sub 
   End If 
   For Each FolderItem In NetworkFolder.Items() 
	  If FolderItem.Name = ConnectionName Then 
		 LANConnection = FolderItem 
		 Exit For 
	  End If 
   Next 
   If LANConnection Is Nothing Then 
	  Exit Sub 
   End If 
   Dim DisableVerbItem, Verb As Shell32.FolderItemVerb 
   For Each Verb In LANConnection.Verbs 
	  If Verb.Name = DisableVerb Then 
		 DisableVerbItem = Verb 
	  End If 
   Next 
   DisableVerbItem.DoIt() 
End Sub 
End Module
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,779
Location
Sydney, Australia
Programming Experience
10+
There are two things to note here.

Firstly, there is absolutely no reason to have an Exit statement as the last line of a method. Once it passes that line it exits anyway so it serves no purpose. Only use an Exit statement if you want to exit early, like if some condition has been met and you want to exit without executing the remainder of the method.

Secondly, I very much doubt that your code actually hangs on an Exit statement. I think it far more likely that execution enters the process1 (I'd suggest changing that name to something more descriptive, and method names generally start with an upper case letter) method and gets stuck on one of the operations in there. I'd suggest you put a breakpoint at the top of the process1 procedure and step through it line by line using F10. Once you find the offending line, if it is one of your own methods you can use F11 to step into that method and trace its execution and so on.
 
Top Bottom