我試圖做一些SFX:使一個程序生成一個wrapping.exe圍繞另一個wrapped.exe。如何從exe文件中提取與用於創建此exe文件相同的.ico文件?
Wrapping.exe將wrapped.exe作爲資源嵌入,並在執行時將wrapped.exe保存到臨時文件夾中,並使用特定命令行參數執行,然後將其刪除。 wrapped.exe並不總是一個.Net程序,我沒有它的源代碼。
wrapping.exe應該在.net 3.5中完成,以便可以在Windows 7 SP1和更高版本上使用,而無需事先安裝.Net。
Wrapping.exe是使用.Net 4.6中的Roselyn在C#程序中生成的。
我需要wrapping.exe通過資源管理器像wrapped.exe一樣可視化。我用硬編碼的.ico文件做了一個成功的測試。代碼看起來像這樣(簡體):
var compilation = CSharpCompilation.Create(...);
var resourceDescription = new ResourceDescription(resourceName: "SFX.resourceName",
dataProvider:() => File.OpenRead("wrapped.exe"),
isPublic: false);
using (var iconStream = File.OpenRead(@"wrapped.ico"))
using (var peStream = File.Create("wrapping.exe"))
using (var pdbStream = File.Create("wrapping.pdb"))
using (var win32resStream = compilation.CreateDefaultWin32Resources(
versionResource: true,
noManifest: false,
manifestContents: null,
iconInIcoFormat: iconStream))
{
var emitResult = compilation.Emit(peStream: peStream,
pdbStream: pdbStream,
manifestResources: new[] { resourceDescription },
win32Resources: win32resStream,
options: new EmitOptions(subsystemVersion: SubsystemVersion.Windows7));
return emitResult;
}
現在我試圖從「wrapped.exe」得到iconStream。我試圖取代:
using (var iconStream = File.OpenRead(@"wrapped.ico"))
有:
var iconStream = new MemoryStream();
Icon icon = Icon.ExtractAssociatedIcon("wrapped.exe");
icon.Save(iconStream);
iconStream.Seek(0, SeekOrigin.Begin);
但我只得到了32 * 32的圖標。
如何提取完全相同的.ico文件(包括所有格式,例如16 * 16 32位BMP,32 * 32 32位BMP,48 * 48 32位BMP,64 * 64 32位BMP和256 * 256 32位PNG)用於創建'wrapped.exe'?
這只是錯誤的做法。該圖標由程序集鏈接器作爲* unmanaged *資源嵌入到可執行文件中。必需的,因爲操作系統不知道有關管理資源的bean。所有原生的winapi便利功能只會從資源中產生一個圖標圖像,從文件中挖掘完整的圖標需要一個痛苦的拼寫錯誤。使用Assembly.GetManifestResourceStream()的圖標的託管版本。 –
@HansPassant我試圖從非託管資源提取圖標,因爲wrapped.exe並不一定是與.Net(我的firts使用是嵌入InnoSetup EXE)。 – MuiBienCarlota
@HansPassant做了一個編輯來澄清它。 – MuiBienCarlota