CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2000
    Location
    South East England
    Posts
    86

    Question Load and run a program from Visual Basic.

    I am trying to run;
    Environ$("SystemRoot") + "\system32\inetcpl.cpl"
    (C:\WINDOWS\system32\inetcpl.cpl)
    and
    Environ$("SystemRoot") + "\system32\firewall.cpl"
    (C:\WINDOWS\system32\firewall.cpl)

    I can't use following code as the files end with .cpl and not .exe
    Code:
    P Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    
    Private Sub com_Button_Click()
      Call ShellExecute(hWnd, "open", Environ$("SystemRoot") + "\system32\inetcpl.cpl", "", vbNullString, vbNormalFocus)
    End Sub
    ShellExecute returns 31 and not 33.
    I can't even run a shortcut file to load/run inetcpl.cpl as a shortcut file ends with .lnk

    ShellExecute(hWnd, "edit"
    ShellExecute(hWnd, "explore"
    ShellExecute(hWnd, "run"
    ShellExecute(hWnd, "exec"
    Etc. also don't work.


    Help!
    Don't spend too much time on your computer.
    Sit on a chair instead!!
    It's a lot more comfortable and better for your hardware.

    :-/

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Load and run a program from Visual Basic.

    ShellExecute() opens the default program (only if it is set)

    Code:
    Option Explicit
    
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    
    Private Const SW_HIDE As Long = 0
    Private Const SW_SHOWNORMAL As Long = 1
    Private Const SW_SHOWMINIMIZE As Long = 2
    Private Const SW_SHOWMAXIMIZED As Long = 3
    Private Const SW_SHOWNOACTIVATE As Long = 4
    Private Const SW_SHOW As Long = 5
    Private Const SW_MINIMIZE As Long = 6
    Private Const SW_SHOWMINNOACTIVED As Long = 7
    Private Const SW_SHOWNA As Long = 8
    Private Const SW_RESTORE As Long = 9
    Private Const SW_SHOWDEFAULT As Long = 10
    
    'SW_HIDE 0
    'Hides the window and activates another window.
    'SW_MAXIMIZE 3
    'Maximizes the specified window.
    'SW_MINIMIZE 6
    'Minimizes the specified window and activates
    'the next top-level window in the Z order.
    'SW_RESTORE 9
    'Activates and displays the window. If the window
    'is minimized or maximized, Windows restores it
    ' to its original size and position. An application
    'should specify this flag when restoring a minimized window.
    'SW_SHOW 5
    'Activates the window and displays it in its current size and position.
    'SW_SHOWDEFAULT 10
    'Sets the show state based on the SW_ flag
    ' specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application. An application should call ShowWindow with this flag to set the initial show state of its main window.
    'SW_SHOWMAXIMIZED 3
    'Activates the window and displays it as a maximized window.
    'SW_SHOWMINIMIZE 2
    'Activates the window and displays it as a minimized window.
    'SW_SHOWMINNOACTIVED 7
    'Displays the window as a minimized window. The active window remains active.
    'SW_SHOWNA 8
    'Displays the window in its current state. The active window remains active.
    'SW_SHOWNOACTIVATE 4
    'Displays a window in its most recent size
    'and position. The active window remains active.
    'SW_SHOWNORMAL 1
    'Activates and displays a window. If the window is minimized or
    'maximized, Windows restores it to its original size and position.
    'An application should specify this flag when displaying the window
    'for the first time
    
    
    Private Sub Command1_Click()
      ShellExecute 0&, "OPEN", "www.msn.com", vbNullString, "C:\", SW_SHOWMINIMIZE
    '    ShellExecute 0&, "OPEN", "D:\temp\pps-cd\play.bat", vbNullString, "C:\", SW_SHOWNORMAL
    '    ShellExecute 0&, "OPEN", "outlook.exe", vbNullString, "C:\", SW_SHOWNORMAL
    '    ShellExecute 0&, "PRINT", "D:\temp\jazz.txt", vbNullString, "D:\temp", SW_SHOWMINIMIZE
        Beep
     '  c:\program files\outlook express\msimn.exe
     '  c:\ = Destination Folder
    End Sub
    SHELL() might help:
    Code:
    Option Explicit
    
    Private Sub Form_Load()
      Shell ("C:\Windows\System32\cmd.exe /c  " & _
         "C:\Windows\System32\ipconfig >> d:\myip.txt"), vbNormalFocus
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Talking Re: Load and run a program from Visual Basic.

    Hello Mark.

    This is actually pretty simple

    What you should do is make use of the Shell object to run rundll32.exe which runs shell32.dll which can run all these control panel applets. For example :

    Code:
    Shell ("rundll32.exe shell32.dll,Control_RunDLL inetcpl.cpl")
    runs the internet settings applet.

    Code:
    Shell ("RunDll32.exe shell32.dll,Control_RunDLL firewall.cpl")
    Runs the Firewall applet.

    I am attching a sample program here for you.

    BTW, this article :

    http://www.codeguru.com/vb/gen/vb_sy...le.php/c13963/

    I wrote for VB 2005, but if you just apply the same logic as per this post's attachment, you will be fine

    I hope this helps

    Hannes
    Attached Files Attached Files

  4. #4
    Join Date
    Aug 2000
    Location
    South East England
    Posts
    86

    Re: Load and run a program from Visual Basic.

    Thank you dglienna and HanneSThEGreaT
    Your code works and only one line.

    Code:
    Dim file as string
    file = "C:\Thankyou.txt"
    Call Shell ("rundll32.exe shell32.dll,Control_RunDLL " + file)
    Don't spend too much time on your computer.
    Sit on a chair instead!!
    It's a lot more comfortable and better for your hardware.

    :-/

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Load and run a program from Visual Basic.

    2 things...

    Don't call file a file, so as not to be confused with RESERVED WORDS

    Also, use & instead of + to concatenate strings. ("2" + "2" = 4, not "22")

    Code:
    Call Shell ("rundll32.exe shell32.dll,Control_RunDLL " & infile)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured