Thread: Quiet System Calls in C++

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    6

    Quiet System Calls in C++

    Hello all

    I am attempting to make quiet system calls from C++ in order to run a batch file without the user knowing. I am going to eventually make this cross platform (windows and linux) although for now i am currently looking for a windows solution.

    So I guess i'm looking for two things...
    1. To call a program from windows without the user knowing
    2. C++ program to pause until the system call has completed

    Ideas?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Hiding your intentions from the user could be bad.

    Such a task is O/S specific anyway, so you'll have to write it multiple ways.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    88
    >Such a task is O/S specific anyway, so you'll have to write it multiple ways.

    MacGyver, I have to say, you rock at patronizing people for the sole purpose of making yourself look smarter.


    If you're just running batch files, system() sounds like a good bet. AFAIK, it's "quiet".

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by UMR_Student View Post
    >Such a task is O/S specific anyway, so you'll have to write it multiple ways.

    MacGyver, I have to say, you rock at patronizing people for the sole purpose of making yourself look smarter.
    I really don't get how that comment is patronizing in any way.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Yeah... i dont see that either.

    But yeah, call it with system()

    and make sure the batch file has this at the start:
    Code:
    @echo off
    I believe that will do the trick.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    88
    Well, maybe "pointless" is a more appropriate decription than "patronizing". He obviously already knows it's OS specific and will have to be written two different ways.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    6
    i tried using system() but the command prompt window appears... anyway to hide this?

    I also tried ShellExecute() with the hide flag but the c++ app doesn't wait for the command to finish before continuing on. I seem to be between a rock and a hard place. But there has to be an answer out there someplace.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    ShellExecuteEx can with some parameters combination return handle of the new process.
    You then can wait for this handle using some of Win API waiting functions like WaitForSingleObject
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    6
    how would that be? I was looking into making waitforsingleobject work for me, but ShellExecuteEx looks to return only bool values.

    Ideas/explanations?

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You would call it on the HANDLE variable named hProcess that is inside the struct argument you give to ShellExecuteEx().

    Notes from MSDN on that parameter:

    Handle to the newly started application. This member is set on return and is always NULL unless fMask is set to SEE_MASK_NOCLOSEPROCESS. Even if fMask is set to SEE_MASK_NOCLOSEPROCESS, hProcess will be NULL if no process was launched. For example, if a document to be launched is a URL and an instance of Microsoft Internet Explorer is already running, it will display the document. No new process is launched, and hProcess will be NULL.

    Note ShellExecuteEx does not always return an hProcess, even if a process is launched as the result of the call. For example, an hProcess does not return when you use SEE_MASK_INVOKEIDLIST to invoke IContextMenu.

  11. #11
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by UMR_Student View Post
    Well, maybe "pointless" is a more appropriate decription than "patronizing". He obviously already knows it's OS specific and will have to be written two different ways.
    really? I've seen no evidence that he knows that.

    To the op, in windows you should use CreateProcess.

    If you want a portable way to do this, have a look at the proposal for Boost.Process
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  12. #12
    Registered User
    Join Date
    May 2007
    Posts
    88
    > really? I've seen no evidence that he knows that.

    Wow.

  13. #13
    Registered User
    Join Date
    May 2007
    Posts
    6
    Jesus, i know its OS specific, but don't concern yourselves with that right now.

    I am currently looking into the ShellExecuteEX() right now. Any further help would be appreciated.

  14. #14
    Registered User
    Join Date
    May 2007
    Posts
    88
    ChaosEngine is right, you probably want to focus on CreateProcess() instead

  15. #15
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    #include <iostream>
    #include <windows.h>
    
    int main()
    {
    	bool bLoop = true;
    	std::string szProg;
    	SHELLEXECUTEINFO shellinfo;
    	
    	ZeroMemory(&shellinfo,sizeof(SHELLEXECUTEINFO));
    	shellinfo.cbSize = sizeof(SHELLEXECUTEINFO);
    	shellinfo.lpVerb = "open";
    	shellinfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    	shellinfo.nShow = SW_SHOWDEFAULT;
    	
    	do
    	{
    		std::cout << "Enter a string to pass to ShellExecuteEx: ";
    		std::cout.flush();
    		getline(std::cin,szProg);
    		if(szProg.compare("exit"))
    		{
    			// process command
    			shellinfo.lpFile = szProg.c_str();
    			ShellExecuteEx(&shellinfo);
    			if(shellinfo.hProcess)
    			{
    				std::cout << "Waiting for process..." << std::endl;
    				WaitForSingleObject(shellinfo.hProcess,INFINITE);
    			}
    			else std::cout << "Error starting process." << std::endl;
    		}
    		else bLoop = false;
    	}while(bLoop);
    	
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic system calls help.
    By AmbliKai in forum C Programming
    Replies: 5
    Last Post: 03-21-2008, 07:18 AM
  2. Opinions on custom system build
    By lightatdawn in forum Tech Board
    Replies: 2
    Last Post: 10-18-2005, 04:15 AM
  3. Problem With My Box
    By HaVoX in forum Tech Board
    Replies: 9
    Last Post: 10-15-2005, 07:38 AM
  4. School Mini-Project on C/C++ (Need Your Help)..
    By EazTerence in forum C++ Programming
    Replies: 4
    Last Post: 09-08-2005, 01:08 AM
  5. System Calls && Variables
    By Okiesmokie in forum C++ Programming
    Replies: 6
    Last Post: 03-06-2002, 09:10 PM