1

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.

4
  • What do you mean with "on runtime"? And how/where do you change the dll code, and how.where do you call it? Commented Aug 7, 2013 at 8:34
  • To change the dll code I mean to add a method signature and implementation to a class in the dll project. In runtime, I mean during when the exe run. The exe is an MFC application that asks the user for a source file to test, that contains a single C++ function. To test this function I copy it to a file in my dll project class and add its signature to the class header. Then I add a call to this function from another function in my dll project. Then I need to call it to test the user code's behavior. So I need to build the dll on runtime. Commented Aug 7, 2013 at 9:10
  • Ok so if I understand this correctly, you want to build the dll project from within your MFC executable, not from within your MFC project? That's just a matter of starting the msbuild process with correct arguments just as you would on the command line, right? Commented Aug 7, 2013 at 9:22
  • Yes. I want to build the dll project from within my MFC executable. The dll's project is in the same solution with the exe project, and the dll will be loaded by the exe once built. Commented Aug 7, 2013 at 10:12

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, I tries it. It works ok on the 1st time only: Once the DLL is built, I load it by LoadLibrary, then I call its method and then I release it by FreeLibrary. Now I change the dll code (add a function to a class at the dll's project), and I do the same again: building loading calling and releasing but it fails on 2nd build.
I'm not sure FreeLibrary is sufficient to remove all handles to the dll - run your exe under the debugger (F5 in VS), and look in the Modules window to check when your dll gets loaded and unloaded.
Thanks, I checked this. The dll get loaded and unloaded from the module window. Still failing on the 2nd time. How to remove all handles to the dll? What to do in addition to call FreeLibrary?
if it's not in the module window I'd say it is not loaded by your exe. So if it's still not accessible, you'll have to figure out what esle is accessing it eg techsupportalert.com/content/…
No idea, can be anything. try to make a simple reproduction case and post a new question.
|

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.