CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2004
    Posts
    4

    how to call vcvarsall.bat with Process::Start method?

    I try to invoke oracle pro*c in C++/CLI project. This is my sample code.

    #include "stdafx.h"

    using namespace System;
    using namespace System::IO;
    using namespace System:iagnostics;


    void call_Process (ProcessStartInfo^ info) {

    try {

    Process^ exeP = Process::Start(info);
    StreamReader^ reader = exeP->StandardOutput;
    String^ result = reader->ReadToEnd();

    Console::Write(result);
    Console::ReadLine();
    }
    catch(Exception^ e) {
    Console::WriteLine(e->Message);
    Console::ReadLine();
    }
    }

    int main(array<System::String ^> ^args)
    {
    ProcessStartInfo^ pStartInfo = gcnew ProcessStartInfo();

    pStartInfo->UseShellExecute = false;
    pStartInfo->RedirectStandardOutput = true;
    pStartInfo->WindowStyle = ProcessWindowStyle::Hidden;

    pStartInfo->FileName = "\"C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\vcvarsall.bat\"";
    pStartInfo->Arguments = "x64";

    call_Process(pStartInfo);

    pStartInfo->FileName = "proc.exe";
    pStartInfo->Arguments = "oracle_connect.pc";

    call_Process(pStartInfo);

    pStartInfo->FileName = "\"C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\bin\\amd64\\cl.exe\"";
    String^ arg1 = " /I C:\\app\\Administrator\\product\\11.2.0\\dbhome_1\\precomp\\public";
    String^ arg2 = " /link C:\\app\\Administrator\\product\\11.2.0\\dbhome_1\\precomp\\LIB\\ORASQL11.LIB";
    pStartInfo->Arguments = arg1 + " oracle_connect.c" + arg2;

    call_Process(pStartInfo); // throws exception.

    pStartInfo->FileName = "oracle_connect.exe";

    call_Process(pStartInfo);

    return 0;
    }

    When I type in ,call vcvarsall.bat in command prompt window myself and run this codes, it works. No exception.
    But when I run this code in another command prompt window without calling vcvarsall.bat, it throws exception.
    Calling vcvarsall.bat with Process::Start method doesn't work!

    Pls, advise me how to call vcvarsall.bat with Process::Start method. Thanks in advance.
    Best Regards!

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: how to call vcvarsall.bat with Process::Start method?

    Your problem most probably is not that you're not able to start vcvarsall.bat, it rather is that it runs, but due to its nature has no effect. vcvarsall.bat sets up the environment vaiables for the Visual Studio tools. However, environment variables are local to each command shell (process instance of cmd.exe). When your program runs vcvarsall.bat, under the hood it runs a command shell that executes vcvarsall.bat and then terminates. (.bat files are considered document files for the command shell, even if you set UseShellExecute to false, much like .txt files are document files for Notepad.) At this point the environment variabes set up by vcvarsall.bat are lost, since they're local to the command shell it runs in.

    When you explicitly start a command shell, then run vcvarsall.bat in it and then your program, the situation is different: The explicit call to vcvarsall.bat sets up the environment variables for that shell, and when you then run your program inside it, it inherits the environment settings, and in turn do all programs your program starts. (The environment settings made by your program's call to vcvarsall.bat are still lost, but that doesn't matter now since the environment already is set up correctly.)

    One solution is, instead of starting three separate processes, have your program create a temporary .bat file containing all three commands you want to execute, then save and execute that. That way all three commands would be executed in the same command shell and the initial environment setup would be effective.

    That way your program, as posted above, would decay to merely writing always the same temporary .bat file to disk and running it, though, so that you could just as well write the .bat file manually and then run that; no more need for your program at all. However, the program as posted may just be a demo and in reality not all parameters of the .bat file are known in advance, which may make up a different situation.

    Please use code tags when posting code.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured