views:

560

answers:

3

Is there a way to invoke an external script or batch file from VC6 (and later) project files?

I have a background process that I need to kill before attempting to build certain projects (DLLS, executables) and haven't found a way to successfully do so from the project itself. I'd like simply to call a batch file with a taskkill command in it.

(Yes, I could run the batch file from a command line before building the projects, but I don't always remember to do so and having it done automatically would be more convenient and less irritating for the whole development team.)

A: 

You could invoke it from a custom build step or a build event.

I don't mind the -1 but I'd like to know why this is a bad idea. (PS: Please don't delete this comment again)
A: 

At least for C# in Visual Studio 2008, you can open the project file and find within the file the following comment:

<!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
   Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->

Uncomment the one that works best for you, in this case the "BeforeBuild" item. Then substitute your batch file for the one I have here:

<Target Name="BeforeBuild">
   <Exec Command="MyBatchFile.bat" />
</Target>

That's all there is to it; whenever you build that project, this will take place each and every time.

That said, I do not know if this works the same for VS 2005 or, especially, VC6. YMMV!

Jason Bunting
I couldn't find anything that looks like this in my VC++ 2008 vcproj file. MMDV!
Ah, thanks for letting me know - I use C# and didn't know whether or not the same would go for all languages within the IDE. I will edit the answer to reflect this.
Jason Bunting
+1  A: 

You can create a utility project (configuration type: Utility in the project property pages) that has a post build event. You then call the batch file from that Post-Build event. If I remember correctly, utility configuration appeared in VS2005. But I believe the same can be achieved with another type of configuration on VC6.

Here is an example of a setup (this is the text of the Command Line property of the Post-Build Event):

set solutionDir=$(SolutionDir)
set platformName=$(PlatformName)
set configurationName=$(ConfigurationName)

call $(SolutionDir)PostBuild.bat

As you can see, you have all the flexibility of customizing the batch environment based on VisualStudio macros.

If you want to have this batch file called every time you build, add a dependency to the requiring project (your main executable or dll project for example). You can add your batch file to the solution items for convenient access (right-click on the solution and select Add -> Existing Item...).

You can even invoke the build command on this utility project to force the execution of the batch file.

At work we have a similar setup to start our unit tests each time a build is triggered.

David