Thread: I can't run a VBSscript with spawn?

  1. #1
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288

    I can't run a VBSscript with spawn?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <process.h>
    #include <windows.h>
    
    
    int main()
    {
        if ( spawnl(P_NOWAIT, "WARNING.vbs", "C:\\Windows\\System32\\wscript.exe", NULL) < 0 )
        {
            fprintf(stderr, "Error : %s", strerror(GetLastError()));
        }
        getchar();
        return 0;
    }
    I tried running that code to execute a VBS script that would activate the system's default voice telling the user that there has been a security breach ( part of a program I'm making, I could explain more, but you would probably think it was pointless ). The VBS script runs when I double click on it, but when I try to run it with spawn with the default application to execute these scripts, it doesn't work. No sound or anything. The error message that displays is "Unknown error". Any ideas?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    No idea what compiler/implementation you're using, but spawnl suggests spawnl is deprecated since VS 2005. Despite the lack of documentation due to deprecation, I believe it has the same parameter format as its replacement _spawnl (_spawnl, _wspawnl (CRT)).

    Note, most of your "spawn" type functions (like the exec* family in Linux) take parameters in the same order you would type them on the command line. So can you run your warning script by hand on the command line? If so, do you type
    Code:
    > WARNING.vbs C:\Windows\System32\wscript.exe
    // or
    > C:\Windows\System32\wscript.exe WARNING.vbs
    Make your call to spawnl match how you run it by hand.

    Also note that, if WARNING.vbs is not in the "current directory" when you're running the program (either via your IDE or from explorer or the command prompt), you will need to specify the full path, like C:\path\to\WARNING.vbs.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Try using ShellExecute instead.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by anduril462 View Post
    No idea what compiler/implementation you're using, but spawnl suggests spawnl is deprecated since VS 2005. Despite the lack of documentation due to deprecation, I believe it has the same parameter format as its replacement _spawnl (_spawnl, _wspawnl (CRT)).

    Note, most of your "spawn" type functions (like the exec* family in Linux) take parameters in the same order you would type them on the command line. So can you run your warning script by hand on the command line? If so, do you type
    Code:
    > WARNING.vbs C:\Windows\System32\wscript.exe
    // or
    > C:\Windows\System32\wscript.exe WARNING.vbs
    Make your call to spawnl match how you run it by hand.

    Also note that, if WARNING.vbs is not in the "current directory" when you're running the program (either via your IDE or from explorer or the command prompt), you will need to specify the full path, like C:\path\to\WARNING.vbs.
    Thanks, I tested this in the command line :

    Code:
    C:\Windows\System32\wscript.exe WARNING.vbs
    And it worked and played the sound.

    Then when I did this :

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <process.h>
    #include <windows.h>
    
    
    int main()
    {
        if ( _spawnl(P_NOWAIT, "C:\\Windows\\System32\\wscript.exe", "C:\\CProjects\\WARNING.vbs", NULL) < 0 )
        {
            fprintf(stderr, "Error : %s", strerror(GetLastError()));
        }
        getchar();
        return 0;
    }
    It just displayed the options for running scripts. Any idea of how to get it to play the sound instead?

    Oh, and I'm running Code::Blocks with mingw32 on windows 7.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I think iMalc is right, ShellExecute is the better function to use in this case, and I recommend following his advice, as he is much better at Windows programming than I am.

    That being said, if you read the documentation for _spawnl carefully, you will notice that it's _P_NOWAIT, with a leading underscore. That may matter. You also may want to try using _P_WAIT, which should make the parent process wait for the child process to finish before continuing. Just a guess, but there may be an issue where the parent spawns the child process and terminates, cleaning up resources (that the child inherited when it was spawned) that are needed to play sounds, before the child process can actually play any sound.

  6. #6
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by anduril462 View Post
    I think iMalc is right, ShellExecute is the better function to use in this case, and I recommend following his advice, as he is much better at Windows programming than I am.

    That being said, if you read the documentation for _spawnl carefully, you will notice that it's _P_NOWAIT, with a leading underscore. That may matter. You also may want to try using _P_WAIT, which should make the parent process wait for the child process to finish before continuing. Just a guess, but there may be an issue where the parent spawns the child process and terminates, cleaning up resources (that the child inherited when it was spawned) that are needed to play sounds, before the child process can actually play any sound.
    Quote Originally Posted by iMalc View Post
    Try using ShellExecute instead.
    Thanks for the responses, I fixed the code now and I'm able to execute the VBSscript and the sound plays. Here was the code I ended up with after adding ShellExecute.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <process.h>
    #include <windows.h>
    
    
    int main()
    {
        if ( ((int)(ShellExecute(NULL, NULL, "C:\\CProjects\\WARNING.vbs", NULL, NULL, SW_SHOWNORMAL))) < 33 )
        {
            fprintf(stderr, "Error : %s", strerror(GetLastError()));
        }
        
        return 0;
    }
    I followed Microsoft's instructions by casting the return value to an int, and testing to see if it was not above 32 for when it failed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Spawn on fly vs pre allocate threads
    By lordmule in forum C Programming
    Replies: 12
    Last Post: 03-27-2012, 08:53 AM
  2. How to spawn a process and have it keep ownership
    By paraglidersd in forum C Programming
    Replies: 5
    Last Post: 07-14-2011, 02:37 PM
  3. Spawn Problems
    By wilkisi in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-30-2002, 07:48 AM
  4. a little more of spawn()
    By tsarena in forum C++ Programming
    Replies: 0
    Last Post: 05-08-2002, 07:33 AM
  5. spawn
    By lambs4 in forum C Programming
    Replies: 2
    Last Post: 02-17-2002, 05:16 AM