Results 1 to 10 of 10

Thread: Shell function - how do I do this?

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2007
    Posts
    53

    Question Shell function - how do I do this?

    Hey all,

    I'm needing to run an .EXE outside the program and return a number for its result (the .EXE is a serial number validity checker); 1 if the serial number is valid, and anything else isn't.

    Right now, i'm using the Shell command/function to shell out to the .EXE, then checking the return variable assigned to the Shell command to see if its 1. I need it to return to the program only after it completes running the outside .EXE; it states by default it runs things asynchronously; is there a way to change this? It doesn't seem to work properly...am I doing this correctly?

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,513

    Re: Shell function - how do I do this?

    ' this will run the windows calculater
    Dim ReturnValue
    ReturnValue = Shell("calc.exe", 1) ' Run Calculator.
    AppActivate ReturnValue ' Activate the Calculator.

    Does this help

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2007
    Posts
    53

    Re: Shell function - how do I do this?

    Quote Originally Posted by wes4dbt
    ' this will run the windows calculater
    Dim ReturnValue
    ReturnValue = Shell("calc.exe", 1) ' Run Calculator.
    AppActivate ReturnValue ' Activate the Calculator.

    Does this help
    I just had:
    Code:
    ReturnValue = Shell("hornetrez.exe", 1)
    if ReturnValue<>1 then msgbox "Error: code " & ReturnValue &"."
    Would this not work? Do I have to use the "AppActivate" thing for it to get a return value from the program, and not return to the program until its done executing?

  4. #4
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Shell function - how do I do this?

    Search the forums for ShellWait (or ShellAndWait). Someone on these forums wrote a function a while ago that returned a value, only when the application that was run had finished executing.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  5. #5
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,513

    Re: Shell function - how do I do this?

    try this

    ' this will run the windows calculater
    Dim ReturnValue
    ReturnValue = Shell("calc.exe", 1) ' Run Calculator.
    msgbox returnvalue

    ReturnValue equal the application ID not any results.

  6. #6
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Shell function - how do I do this?

    That will execute straight away. What they are after is, calc runs.. and when it closes, the Messagebox pops up. Not all at once..

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  7. #7
    Fanatic Member
    Join Date
    Oct 2005
    Posts
    586

    Re: Shell function - how do I do this?

    You might want to stay away from the Shell command. It doesn't work properly under Vista. You should use a system call to the ShellExecute function. It passes things along to Windows and lets the system handle it properly. Define Shell Execute like this:

    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


    Now that you have it defined you can call it. If you have a document you want to open like "mydoc.doc" or a file that has an extension registered with Windows like a .JPG file you can do it this way:

    OpenThisFile$ = "MyDoc.Doc"

    ShellExecute hWnd, "open", OpenThisFile$, vbNullString, vbNullString, SW_SHOW


    If you have an EXE file you want to open you can do it this way:

    OpenThisProg$ = "myprog.exe"
    rc = ShellExecute(0&, vbNullString, OpenThisProg$, vbNullString, vbNullString, vbMaximizedFocus)


    Hope that helps.

    --DB

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2007
    Posts
    53

    Re: Shell function - how do I do this?

    Quote Originally Posted by Darkbob
    You might want to stay away from the Shell command. It doesn't work properly under Vista. You should use a system call to the ShellExecute function. It passes things along to Windows and lets the system handle it properly. Define Shell Execute like this:

    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

    If you have an EXE file you want to open you can do it this way:

    OpenThisProg$ = "myprog.exe"
    rc = ShellExecute(0&, vbNullString, OpenThisProg$, vbNullString, vbNullString, vbMaximizedFocus)
    --DB
    Yes, my program should work under most modern versions of Windows (maybe NT through Vista?).

    With your above example, how would I put the Error or Return Code (I guess that's what I need returned) of running the .EXE into a variable I can refer to later? Will this ShellExecute thing only return to the program once its done executing?

    I need actual code in this case; i'm lost. .EXE name is hornetrez.exe, and I need to check to see if the code returned from this .EXE is 1 after execution is complete, if so, then the serial number is valid, otherwise display a message box error.

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Shell function - how do I do this?

    If you ran this program, by itself, and it verified a serial number, what would it do to indicate that?

  10. #10

    Thread Starter
    Member
    Join Date
    Sep 2007
    Posts
    53

    Re: Shell function - how do I do this?

    Quote Originally Posted by Hack
    If you ran this program, by itself, and it verified a serial number, what would it do to indicate that?
    The .EXE itself is supposed to check two different locations to verify the serial number is present and valid (I think it connects to a database of some kind to do so) and then is designed to return a code: 1 if everything went ok, and then anything is else is supposed to be an error. I'm implementing it as part of a serial protection scheme for a product about to be released (company and product not given for obvious reasons ).

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