xp_cmdshell

  • I wanted to execute a .exe file develop in VB. I have no problem executing the exe file from the commandline. But when I use xp_cmdshell to execute, it does not seems to work. it does not return me with an error. It just output as NULL. Why?

  • VB return an msgbox to raise error right and there uis no way a message box is shown at xp_cmdshell. but on that machine you will see that messagebox. You can check in vb if it's fired through commandline and then return the error code.

    I have done this a long before but forget which is the function name to return error.

    Cheers,

    Prakash

    Prakash Heda
    Lead DBA Team - www.sqlfeatures.com
    Video sessions on Performance Tuning and SQL 2012 HA

  • Further more you can't return messages to stdout from VB so the output will always be NULL

    You can get a return code from VB that maps to the ERRORLEVEL in DOS which then maps to the return value of xp_cmdshell. You have to use an API call to ExitProcess.

    Simon Sabin

    Co-author of SQL Server 2000 XML Distilled

    http://www.amazon.co.uk/exec/obidos/ASIN/1904347088


    Simon Sabin
    SQL Server MVP

    http://sqlblogcasts.com/blogs/simons

  • Simon that is right and not right. VB can write to stdout but it will not process in a Command Prompt window by the way the handles work. However, there is a hack out on http://www.expert-exchange.com if you search stdout and vb or visual basic that I saw awhile back that can help with this, I never played with. Otherwise it is not really thread safe. May be better to find someone willing to convert to an Exteneded SP if not to difficult in C++.

  • Or you can use VBS script file to do the work. You may have to make a few code change but in vbscript you can write to the stdout quite easy liek so

    Ex.

    Create a fiole "test.vbs"

    with this as the only line.

    WScript.StdOut.Write "Hello"

    and save.

    Then execute like so

    cscript.exe //NoLogo drive:\path\test.vbs

    Note: //NoLogo is need or you will get the MS header like so

    Microsoft (R) Windows Script Host Version 5.6

    Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

    in you output.

    Edited by - antares686 on 11/26/2002 05:25:21 AM

  • Ok you can do it with one helluv a hack http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20169020.html

    Simon Sabin

    Co-author of SQL Server 2000 XML Distilled

    http://www.amazon.co.uk/exec/obidos/ASIN/1904347088


    Simon Sabin
    SQL Server MVP

    http://sqlblogcasts.com/blogs/simons

  • Never said it was easy, just that it could be done.

  • Also take a look at:

    How to Write CGI Applications in Visual Basic

    http://support.microsoft.com/default.aspx?scid=KB;en-us;239588&

    Tim C.

    //Will write code for food


    Tim C //Will code for food

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply