CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: shell(problem)

  1. #1
    Join Date
    Mar 2009
    Posts
    2

    shell(problem)

    I am having trouble running a dos command from my VB6 app. The command I want to run is:

    Code:
    rundll32 printui.dll,PrintUIEntry /if /f C:\x64\DKAAT940.inf /b "PrinterA" /r \\SERVER\pphplj2100 /m "Dell Laser Printer 5210n PS"
    But no matter what I try, I can not get it to shell properly from VB. If i open a cmd window and paste it in, works fine. Start->Run works fine.

    Ive tried:
    Shell(rundll32 printu...blah)
    Shell(cmd /c rundll32 printu...blah)
    Shell(start rundll32 printu...blah)
    Shell(cmd /c start rundll32 printu...blah)
    Shell(start cmd /c rundll32 printu...blah)

    Any Ideas? printui.dll,PrintUIEntry IS invoked, but its failing somewhere. I have been stuck on this for a few days now, and I am fustrated. I have simplified this as much as i could in the VB program, just to try to get this to work. shell(textbox1.text) but no dice. I also noticed that if, in the command window I try to run "cmd /c rundll32 printu...blah" It will fail. I had thought that perhaps I need to try using escape characters, but so far, I haven't been able to figure it out. Any help would be much appreciated.

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

    Re: shell(problem)

    It usually works more like this:

    Shell into PIPE

    Code:
    Option Explicit
    
    Private Sub Form_Load()
      Shell ("C:\Windows\System32\cmd.exe /c  " & _
         "C:\Windows\System32\ipconfig >> d:\myip.txt"), vbNormalFocus
    End Sub
    You probably want to use ShellExecute() though:

    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_SHOWNORMAL As Long = 1
    Private Const SW_HIDE As Long = 0
    
    Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
    
    Dim sSave As String, Ret As Long
    
    Private Sub Form_Load()
        'KPD-Team 1998
        'URL: http://www.allapi.net/
        'E-Mail: KPDTeam@Allapi.net
        Dim sSave As String, Ret As Long
        'Create a buffer
        sSave = Space(255)
        'Get the system directory
        Ret = GetSystemDirectory(sSave, 255)
        'Remove all unnecessary chr$(0)'s
        sSave = Left$(sSave, Ret)
    End Sub
    
    Private Sub Command1_Click()
        ShellExecute Me.hwnd, "Open", sSave & "CMD.exe", " /c dir c: > D:\Myfile.txt", "D:\", SW_SHOWNORMAL
    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 2008
    Location
    WV
    Posts
    5,362

    Re: shell(problem)

    You can write the commands to a bat file then use shell to execute the bat file.

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: shell(problem)

    It could simply be a problem of the quotes which are embedded in the command string.
    You have to embed the whole string which is the shell command in quotes. So any quotes which are embedded in this string have to be doubled to stay in place when the command is passed to the command interpreter.
    Try:
    Code:
    Shell "rundll32 printui.dll,PrintUIEntry /if /f C:\x64\DKAAT940.inf /b ""PrinterA"" /r \\SERVER\pphplj2100 /m ""Dell Laser Printer 5210n PS"""

  5. #5
    Join Date
    Mar 2009
    Posts
    2

    Re: shell(problem)

    Thank you all for your suggestions. I have tried each of them with some success. (each method works!) BUT... So far I have had no luck getting rundll32 to do its magic. Each time I get "Operation could not be completed".

    I have narrowed it down to not being a VB or batch file issue... so ill end my questions now. Thank you very much for the help!
    Last edited by kptkill; March 8th, 2009 at 05:39 PM. Reason: not a vb issue

Tags for this Thread

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