5

I want to compile a C code from c# programmatically. I am trying but i have not found any solution yet. Here is my code.

try {
    var info = new ProcessStartInfo {
        FileName = "cmd.exe",
        Arguments = "mingw32-gcc -o a Test.c"
    };
    var process = new Process { StartInfo = info };
    bool start = process.Start();

    process.WaitForExit();
    if (start) {
        Console.WriteLine("done");
    }
} catch (Exception) {
    Console.WriteLine("Not done");
}

I am using VS2010 in windows 7 and I have installed mingw32-gcc and my environment variable for mingw32-gcc is C:\Program Files\CodeBlocks\MinGW\bin Any Help will be appreciated. Thanks in advance.

7
  • So that we understand the context: What do you intend to do with the resulting C code? Commented Mar 25, 2012 at 21:47
  • 1
    What is wrong with your code? Commented Mar 25, 2012 at 21:47
  • 1
    I am building a webbased online judge for my semester final project.So normally there should be an option where contestent can submit there code. So I have to take the submitted code and save into a .c file than have to compile it and provide feedback to the contestant. I have compile a c code in a desktop application in java But facing problem in C#. Thanks Commented Mar 25, 2012 at 21:51
  • 2
    +1 for finally allowing C-injection on a website. I hate SQL. Commented Mar 25, 2012 at 21:54
  • What's the error you got and what the environment variable you talk about? Or does it simply never end (which is what I expect, since you open up a command prompt). Have you tried with Filename set to the actual path of mingw32-gcc.exe, no need to call cmd.exe? Make sure to include ".exe". Commented Mar 25, 2012 at 21:56

2 Answers 2

8

Try

Process process = Process.Start(
         @"C:\Program Files\CodeBlocks\MinGW\bin\mingw32-gcc.exe", "-o a Test.c");
Sign up to request clarification or add additional context in comments.

3 Comments

@YoryeNathan I wouldn't really call mingw32 something to enjoy.
@ta.speot.is Doesn't matter, he should get the credit and the thank you if the answer helped the OP.
@YoryeNathan It seems the sarcasm train was an express.
2

Calling the cmd.exe program is not necessary. You can directly call the mingw32-gcc.exe program with arguments.

Edit:

string szMgwGCCPath = "C:\\mingw32\\bin\\mingw32-gcc.exe"; // Example of location
string szArguments = " -c main.c -o main.exe"; // Example of arguments
ProcessStartInfo gccStartInfo = new ProcessStartInfo(szMgwGCCPath , szArguments );
gccStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(gccStartInfo );

Regards

3 Comments

Directly calling mingw32-gcc.exe with arguments did not solve my problem man.Can you be specific? Thanks for replying. Regards
@ForhadHussain: can you be specific with "did not solve my problem"? Errors, locks, what? Grifos is right in that you should not call "cmd.exe".
Right @Abel i did not call cmd.exe then. But my program is now working correctly because i have edited it. Thanks you all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.