Question

I see in msdn many pages explaining how to use msbuild to build a C++ project from the command line. But is it possible to use msbuild to build a C++ project from inside the code of another C++ project?

To be more specific: In a C++ solution, I have 2 projects (exe, dll). Is it possible to build the dll project from the exe project on runtime by msbuild, then to load the dll and call one of the dll's function?

I need this since I need to change the dll project's code on runtime, to build it and to call its function on runtime.

Any help will be appreciated.

Was it helpful?

Solution

Just call CreateProcess, for example (using msbuild 4):

#include <windows.h>
#include <string>
#include <iostream>

bool RunMsBuild( const char* args )
{
  STARTUPINFO startupInfo;
  PROCESS_INFORMATION procInfo;
  memset( &startupInfo, 0, sizeof( startupInfo ) );
  memset( &procInfo, 0, sizeof( procInfo ) );

  std::string cmdLine( "C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\MSBuild.exe " );
  cmdLine += args;

  if( !CreateProcessA( 0, const_cast< char* >( cmdLine.c_str() ),
        0, 0, FALSE, 0, 0, 0, &startupInfo, &procInfo ) )
    return false;
  WaitForSingleObject( procInfo.hProcess, INFINITE );
  DWORD dwExitCode;
  GetExitCodeProcess( procInfo.hProcess, &dwExitCode );
  CloseHandle( procInfo.hProcess );
  CloseHandle( procInfo.hThread );
  return dwExitCode == 0;
}

int main()
{
  if( RunMsBuild( "full\\path\\to\\ptojectfile /t:Build" ) )
    std::cout << "ok";
  else
    std::cout << "not ok";
  std::endl;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top