?
Solved

C++ Passing Parameters to Silent Program

Posted on 2009-04-18
9
Medium Priority
?
515 Views
Last Modified: 2013-12-14
In a prior question I asked how to make the console window not show up in a Windows application and I was told to replace void main() with int __stdcall WinMain(), which worked perfectly. But now I wish to pass this silent program parameters when I execute the program from an external program and I am unsure of how I would do this with this function.

Previous Question: http://www.experts-exchange.com/Programming/Languages/CPP/Q_24330131.html

Can anyone help me?
0
Comment
Question by:Cyber-Dragon
7 Comments
 
LVL 9

Expert Comment

by:JohnGaby
ID: 24177925
WinMain can also have parameters which include the command line:

http://msdn.microsoft.com/en-us/library/ms633559(VS.85).aspx
0
 

Author Comment

by:Cyber-Dragon
ID: 24177958
I don't quite understand this:
lpCmdLine
[in] Pointer to a null-terminated string specifying the command line for the application, excluding the program name. To retrieve the entire command line, use the GetCommandLine function.
0
 
LVL 40

Accepted Solution

by:
evilrix earned 1000 total points
ID: 24178622
The parameters are passed as a string (e.g. "param1 param2 param2") or you can call GetCommandLine, which will return the same string.

http://msdn.microsoft.com/en-us/library/ms683156.aspx

You can call CommandLineToArgvW, which will return an argv/argc style array and count (as wide strings), which is what I guess is what you are looking for.

http://msdn.microsoft.com/en-us/library/bb776391.aspx
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 1000 total points
ID: 24187857
>>>> You can call CommandLineToArgvW

If you have an ANSI project you may use the following function instead.





int CommandLineToArgvA(const char* lpCmdLine, char* szargs[], int maxargs)
{
   bool quotes = false;
   bool escape = false;
   int  lpos = 0;    
   int nargs = 0; 
   for (int i = 0; i <= (int)strlen(lpCmdLine); ++i)
   {
      char c = lpCmdLine[i];
      switch (c)
      {
      case '\\':
         if (quotes && !escape) 
            escape = true;
         else if (escape)
            escape = false;
         break;
      case '"': 
         if (quotes)
         {
            if (!escape)  
               quotes = false;
         }
         else
            quotes = true;
         break;
      case ' ':
      case '\0':
         if (quotes) break;
         if (lpos < i)
         {
            szargs[nargs] = new char[i-lpos+1];
            strncpy(szargs[nargs], &lpCmdLine[lpos], i-lpos);
            szargs[nargs][i-lpos] = '\0';
            if (++nargs >= maxargs) 
               return nargs; 
         }
         lpos = i+1;
         break;
      default:
         escape = false;
      }
 
   }
   return nargs;
}

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 24191589
>> If you have an ANSI project you may use the following function instead.
Yup, very true... I probably should have pointed this out. Thanks Alex.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24192754
>>>> I probably should have pointed this out.

I actually looked first for the CommandLineToArgvA in WINAPI and was puzzled that it doesn't exist. And writing the code above took more time than I thought ;-)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 25332337
{http:#24178622} answers this with {http:#24187857} assisting.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question