从VC6 / VC2005 / VC2008项目文件调用批处理/脚本文件

时间:2022-09-01 09:20:08

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

有没有办法从VC6(及更高版本)项目文件中调用外部脚本或批处理文件?

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.

我有一个后台进程,我需要在尝试构建某些项目(DLLS,可执行文件)之前杀死,并且还没有找到从项目本身成功完成此操作的方法。我想简单地调用一个带有taskkill命令的批处理文件。

(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.)

(是的,我可以在构建项目之前从命令行运行批处理文件,但我并不总是记得这样做并且自动完成它对整个开发团队来说会更方便,更少刺激。)

3 个解决方案

#1


1  

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.

您可以创建具有后期构建事件的实用程序项目(配置类型:项目属性页中的实用程序)。然后,您从该Post-Build事件中调用批处理文件。如果我没记错的话,VS2005中会出现实用程序配置。但我相信在VC6上使用其他类型的配置也可以实现同样的目的。

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

以下是设置示例(这是Post-Build事件的命令行属性的文本):

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.

如您所见,您可以灵活地基于VisualStudio宏自定义批处理环境。

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...).

如果要在每次构建时调用此批处理文件,请向需要的项目(例如,主要可执行文件或dll项目)添加依赖项。您可以将批处理文件添加到解决方案项目以便于访问(右键单击解决方案并选择添加 - >现有项目...)。

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

您甚至可以在此实用程序项目上调用build命令来强制执行批处理文件。

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

在工作中,我们有类似的设置,每次触发构建时都要开始我们的单元测试。

#2


0  

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

您可以从自定义构建步骤或构建事件中调用它。

#3


0  

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

至少对于Visual Studio 2008中的C#,您可以打开项目文件并在文件中找到以下注释:

<!-- 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:

取消注释最适合您的那个,在本例中为“BeforeBuild”项。然后用您的批处理文件替换我在这里的文件:

<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!

也就是说,我不知道VS 2005或者特别是VC6的工作原理是否相同。因人而异!

#1


1  

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.

您可以创建具有后期构建事件的实用程序项目(配置类型:项目属性页中的实用程序)。然后,您从该Post-Build事件中调用批处理文件。如果我没记错的话,VS2005中会出现实用程序配置。但我相信在VC6上使用其他类型的配置也可以实现同样的目的。

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

以下是设置示例(这是Post-Build事件的命令行属性的文本):

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.

如您所见,您可以灵活地基于VisualStudio宏自定义批处理环境。

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...).

如果要在每次构建时调用此批处理文件,请向需要的项目(例如,主要可执行文件或dll项目)添加依赖项。您可以将批处理文件添加到解决方案项目以便于访问(右键单击解决方案并选择添加 - >现有项目...)。

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

您甚至可以在此实用程序项目上调用build命令来强制执行批处理文件。

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

在工作中,我们有类似的设置,每次触发构建时都要开始我们的单元测试。

#2


0  

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

您可以从自定义构建步骤或构建事件中调用它。

#3


0  

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

至少对于Visual Studio 2008中的C#,您可以打开项目文件并在文件中找到以下注释:

<!-- 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:

取消注释最适合您的那个,在本例中为“BeforeBuild”项。然后用您的批处理文件替换我在这里的文件:

<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!

也就是说,我不知道VS 2005或者特别是VC6的工作原理是否相同。因人而异!