c# 工程加入 win32 资源文件

来源:互联网 发布:网络时间同步服务器ip 编辑:程序博客网 时间:2024/05/22 01:13

这种事情就是一个字烦人,蛋疼,特别蛋疼。 搞了我一天。

方法有几种,都特别烦,一个是 build event , 一个是 msbuild

今天说 build event

1.在 Build Events 中 , Pre-build 

输入:

copy  $(ProjectDir)Time/norlib.Time.Holidays.xml  "$(TargetDir)norlib.Time.Holidays.xml"set VCInclude=C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\atlmfc\includeset SDKInclude=C:\Program Files (x86)\Windows Kits\8.0\Include\umset SDKShared=C:\Program Files (x86)\Windows Kits\8.0\Include\sharedecho $(IntermediateOutputPath)set RCDIR=IF EXIST "C:\Program Files (x86)\Windows Kits\8.0\bin\x86\rc.exe" (set RCDIR="C:\Program Files (x86)\Windows Kits\8.0\bin\x86\rc.exe")echo  (%RCDIR%  /i "%VCInclude%"  /i "%SDKInclude%" /r /fo "$(IntermediateOutputPath)\res1.res" "$(ProjectDir)Controls\OpenFileOrFolderDialog\res1.rc")if not defined RCDIR ( echo "Error! Unable to find rc.exe.")  else  (%RCDIR%  /i "%VCInclude%"  /i "%SDKInclude%" /i "%SDKShared%" /r /fo "$(ProjectDir)res1.res" "$(ProjectDir)Controls\OpenFileOrFolderDialog\res1.rc")if not defined RCDIR (Exit 1)

主要是从第二行开始看

首先要注意 c# 下面很多宏是失效的, 比如 $(VCInstallDir) , 还有写宏和 vc 是指向不一样的目录,比如他妈的: $(FrameworkSdkDir)

这个宏 $(FrameworkSdkDir)  在vc下 指向: C:\program files(x86)\windows kits\8.0\  在里面bin\x86 我们可以找到rc.exe

但是在 c# 下指向:C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\

瞬间晕死, google了半天发现个帖子:

http://go4answers.webhost4life.com/Example/get-winsdk-configuration-tool-work-41266.aspx

他这么说的:

After some investigation and reflecting using Redgate Reflector, I found that the variable $(FrameworkSDKDir) will always resolve to the 6.0a installation if that SDK is installed and the target framework is 3.5. This is due to a possible bug in the .NET Type Microsoft.Build.Shared.FrameworkLocationHelper, in assembly Microsoft.Build.Utilities.v3.5, Version=3.5.0.0 that shipped with .NET Framework 3.5. The problem I see is in the property PathToDotNetFrameworkSdkV35 in this class. It first reads the registry value for SOFTWARE\Microsoft\Microsoft SDKs\Windows\v6.0A\InstallationFolder, while I believe it should actually first read SOFTWARE\Microsoft\Microsoft SDKs\Windows\CurrentInstallFolder. I worked around this problem by hacking the key SOFTWARE\Microsoft\Microsoft SDKs\Windows\v6.0A\InstallationFolder to point to 6.1. This enables me to use rc.exe from SDK 6.1, while it might affect other products that expects to retrieve the 6.0a installation folder from the registry.I have the following questions regarding the issues described above:Why would rc.exe install  to different locations on two otherwise seemingly equivalent machines? Is the code that I refered to in Microsoft.Build.Shared.FrameworkLocationHelper affected by a bug? and if soIs there a work around / fix other that the method that I described?

反正要解决也挺麻烦的,后来如你所见我就在 build events 里面创建了一个 %Macro%

比如:


set VCInclude=C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\atlmfc\include

这里要注意了,你不能写 VCInclude =....... 这样Macro变成了 %VCInclude % .........还有就是不需要双引号。


这样编译成功后, 就有了一个 res 文件,我们千辛万苦就是为了得到这个res文件

然后 c# Project Property -> Application -> Resources -> Resource file (radio button):  输入res文件的绝对路径

这样就能加入了win32资源文件了


里面挺好玩的。


参考:

http://stackoverflow.com/questions/8057080/how-to-embed-a-resource-in-a-net-pe-executable

http://blogs.msdn.com/b/astebner/archive/2006/02/28/541036.aspx

以后还是要上MSBUILD