?
Solved

Execute a program with arguments in C++

Posted on 2003-03-07
11
Medium Priority
?
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

777 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