?
Solved

Execute a program with arguments in C++

Posted on 2003-03-07
11
Medium Priority
?
356 Views
Last Modified: 2010-04-01
Hello all,

I am new to programming in C++ and need a bit of help.  I am trying to write a program so that I can automate a program to convert files from .bmp to .jpeg.  They are screenshots from a game and there are about 10,000 so I need to convert them to jpeg, but in an automated fashion (I dont have 100 hours to site there and change the name of the file to convert).
So here is what I have
1) SFN00001.bmp - SFN1476.bmp
2) program called imgcon which needs the following syntax "imgcon -jpg SFN0001.bmp" to convert

Now I know how to automate it with a loop BUT i cant figure out how to execute it and have the name be a variable.  I am willing to change the names to numbers but if there is a way i dont have to that would be great.
I tryed doing a string name, cin >> name (SFN) and doing a int for rhe numbers.  I have seen people using system and exec but Im not sure which one to use, and which I can add variables for the path to.  Thank you to anyone who helps me, and im sorry I can only offer 75 points.
0
Comment
Question by:KodeKruncherKid
  • 6
  • 4
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 8089015
The following code snippet will do that for you:

DWORD ExecuteAndWaitForCompletion   (   LPSTR   pszCmd)
{
   STARTUPINFO         si;
   PROCESS_INFORMATION pi;

   BOOL                bRes;

   DWORD               dwCode  =   0;

   ZeroMemory  (   &si,    sizeof  (   STARTUPINFO));

   si.cb           =   sizeof  (   STARTUPINFO);
   si.dwFlags      =   STARTF_USESHOWWINDOW;
   si.wShowWindow  =   SW_SHOWNORMAL;

   bRes    =   CreateProcess   (   NULL,
                                   pszCmd,
                                   NULL,
                                   NULL,
                                   TRUE,
                                   NORMAL_PRIORITY_CLASS,
                                   NULL,
                                   NULL,
                                   &si,
                                   &pi
                               );

   while   (   WAIT_OBJECT_0   !=  MsgWaitForMultipleObjects   (   1,
                                                                   &pi.hProcess,
                                                                   FALSE,
                                                                   INFINITE,
                                                                   QS_ALLINPUT
                                                               )
           )
           {
               while   (   PeekMessage (   &msg,   NULL,   0,  0,  PM_REMOVE))
                       {
                           DispatchMessage     (   &msg);
                       }
           }

   GetExitCodeProcess  (   pi.hProcess,    &dwCode);

   CloseHandle (   pi.hProcess);
   CloseHandle (   pi.hThread);

   return  (   dwCode);
}

Just call it like

ExecuteAndWaitForCompletion ( "c:\\path\\imgcon.exe -jpg SFN0001.bmp");
0
 
LVL 86

Expert Comment

by:jkr
ID: 8089024
BTW, if you are not using a GUI app, you can reduce it to

DWORD ExecuteAndWaitForCompletion   (   LPSTR   pszCmd)
{
   STARTUPINFO         si;
   PROCESS_INFORMATION pi;

   BOOL                bRes;

   DWORD               dwCode  =   0;

   ZeroMemory  (   &si,    sizeof  (   STARTUPINFO));

   si.cb           =   sizeof  (   STARTUPINFO);
   si.dwFlags      =   STARTF_USESHOWWINDOW;
   si.wShowWindow  =   SW_SHOWNORMAL;

   bRes    =   CreateProcess   (   NULL,
                                   pszCmd,
                                   NULL,
                                   NULL,
                                   TRUE,
                                   NORMAL_PRIORITY_CLASS,
                                   NULL,
                                   NULL,
                                   &si,
                                   &pi
                               );

   WaitForSingleObject (     pi.hProcess, INFINITE);

   GetExitCodeProcess  (   pi.hProcess,    &dwCode);

   CloseHandle (   pi.hProcess);
   CloseHandle (   pi.hThread);

   return  (   dwCode);
}


0
 
LVL 12

Expert Comment

by:Salte
ID: 8089807
Just as an aside:

if you installed CYGWIN on your windows machine you can run bash shell and what you want to do can easily be done in that shell without making a program to do it:

for f in foo*.bmp; do
   jpgname=$(basename $f .bmp).jpg;
   convert $f $jpgname
done

and then just sit and wait until the job is done.
foo*.bmp will take all files named foo<something>.bmp in current directory and set variable f equal to each of them in turn. It is a loop so it will loop once for each such filename and set f equal to that filename.

$(basename $f .bmp) will take a string such as "foo1147.bmp" and change to "foo1147" with the .bmp removed.

In short basename a b will see if a is a string that has something followed by b and if it does then it will return that something in front of b. If it doesn't it will return a.

so $(basename $f .bmp).jpg will become "foo1147.jpg" if $f was "foo1147.bmp".

jpgfile=$(basename $f .bmp).jpg will then set that value to the variable named jpgfile.


Then you just run that command which you want to run and give the arguments you want. The bmp file is $f and the jpg file is $jpgfile.

Then it will take next filename and do the same.

When it has converted all the files, it is done.

bash runs under windows if you install CYGWIN, check out www.cygwin.com.

Alf
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 86

Expert Comment

by:jkr
ID: 8138347
KodeKruncherKid - anything else you need to know?
0
 

Author Comment

by:KodeKruncherKid
ID: 8139052
Do i run it as is?  I keep getting a syntax errror when it runs.
0
 

Author Comment

by:KodeKruncherKid
ID: 8139061
also it is not a GUI, its a DOS-based program
0
 
LVL 86

Expert Comment

by:jkr
ID: 8139282
>>Do i run it as is?  

Yes :o)

>>I keep getting a syntax errror when it runs.

What errors are you getting? For a console app,

DWORD ExecuteAndWaitForCompletion   (   LPSTR   pszCmd)
{
  STARTUPINFO         si;
  PROCESS_INFORMATION pi;

  BOOL                bRes;

  DWORD               dwCode  =   0;

  ZeroMemory  (   &si,    sizeof  (   STARTUPINFO));

  si.cb           =   sizeof  (   STARTUPINFO);
  si.dwFlags      =   STARTF_USESHOWWINDOW;
  si.wShowWindow  =   SW_SHOWNORMAL;

  bRes    =   CreateProcess   (   NULL,
                                  pszCmd,
                                  NULL,
                                  NULL,
                                  TRUE,
                                  NORMAL_PRIORITY_CLASS,
                                  NULL,
                                  NULL,
                                  &si,
                                  &pi
                              );

  WaitForSingleObject (     pi.hProcess, INFINITE);

  GetExitCodeProcess  (   pi.hProcess,    &dwCode);

  CloseHandle (   pi.hProcess);
  CloseHandle (   pi.hThread);

  return  (   dwCode);
}

should do it...
0
 

Author Comment

by:KodeKruncherKid
ID: 8143028
Error E2141 C:\users\steve\snf.cpp 1: Declaration syntax error
*** 1 errors in Compile ***

is what I get from Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
0
 
LVL 86

Expert Comment

by:jkr
ID: 8143513
That tells us nothing - what is at line 1 of your code?
0
 

Author Comment

by:KodeKruncherKid
ID: 8143539
your code that you said to compile as is!
0
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
ID: 8143560
Well, you do know that you need to add it to the rest of your program? E.g.

#include <windows.h>

DWORD ExecuteAndWaitForCompletion   (   LPSTR   pszCmd)
{
 STARTUPINFO         si;
 PROCESS_INFORMATION pi;

 BOOL                bRes;

 DWORD               dwCode  =   0;

 ZeroMemory  (   &si,    sizeof  (   STARTUPINFO));

 si.cb           =   sizeof  (   STARTUPINFO);
 si.dwFlags      =   STARTF_USESHOWWINDOW;
 si.wShowWindow  =   SW_SHOWNORMAL;

 bRes    =   CreateProcess   (   NULL,
                                 pszCmd,
                                 NULL,
                                 NULL,
                                 TRUE,
                                 NORMAL_PRIORITY_CLASS,
                                 NULL,
                                 NULL,
                                 &si,
                                 &pi
                             );

 WaitForSingleObject (     pi.hProcess, INFINITE);

 GetExitCodeProcess  (   pi.hProcess,    &dwCode);

 CloseHandle (   pi.hProcess);
 CloseHandle (   pi.hThread);

 return  (   dwCode);
}

int main () {

 ExecuteAndWaitForCompletion ( "command.com /c myfile1.bat");
 ExecuteAndWaitForCompletion ( "command.com /c myfile2.bat");
}
0

Featured Post

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.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

621 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