sytem woes

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

sytem woes

 
0
  #1
May 2nd, 2006
Hullo,

I'm trying to make a write a program to automatically create a jar file in java. I've got this far and this works.

  1. #include <windows.h>
  2. #include <direct.h>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.  
  10. chdir("C:/j2sdk1.4.2_04/bin");
  11. system("javac Saluton.java");
  12. system("java Saluton");
  13.  
  14.  
  15.  
  16.  
  17. remove ("Saluton.class");
  18.  
  19.  
  20.  
  21. return 0;
  22.  
  23. }

But I was wondering is there a better way, using windows API to execute the system("javac Saluton.java") command?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: sytem woes

 
0
  #2
May 2nd, 2006
Search for CreateProcess
Originally Posted by Platform SDK
BOOL CreateProcess(
LPCTSTR lpApplicationName,
LPTSTR lpCommandLine,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCTSTR lpCurrentDirectory,
LPSTARTUPINFO lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation
);
in the Platform SDK Documentation or MSDN site.

Edit:
Okay here it is.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: sytem woes

 
0
  #3
May 2nd, 2006
I have no idea what that means. Seeing as you have taken the time to learn the msdn manual by heart do you think you could code up a sample snippet. :cheesy:

Or is using system() ok here?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: sytem woes

 
0
  #4
May 2nd, 2006
Originally Posted by iamthwee
I have no idea what that means. Seeing as you have taken the time to learn the msdn manual by heart do you think you could code up a sample snippet. :cheesy:
There is an example at the bottom of the page I linked.
Originally Posted by iamthwee
Or is using system() ok here?
Personally for something like this I would use system. The overhead for the system call is nothing compared to the effort required to understand and use the CreateProcess function. I would use CreateProcess only if there are some intricate attributes that need to be set in the child processes. But there will be people here who would complain vehemently on the use of system.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: sytem woes

 
0
  #5
May 2nd, 2006
Ah, just noticed your edit...

This works although I need to wrap it up into a function:

#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <direct.h>

int main(  )
{
    chdir("C:/j2sdk1.4.2_04/bin");
    
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    LPTSTR szCmdline=_tcsdup(TEXT("javac Saluton.java"));

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line)
        szCmdline,      // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        getchar();
        //return 0;
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
    
    getchar();
}

>But there will be people here who would complain vehemently on the use of system.

Yeah I'm kind of in two minds as to whether or not to just use system here. Mmkay.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 1306 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC