我有AC#Visual Studio 2012解决方案,它依赖于我使用PInvoke访问的本机DLL.部署应用程序时,我必须确保此Dll位于app文件夹中.
无论如何我可以将这个Dll合并到可执行文件中吗?
也许作为一种资源?
我听说过ILMerge,但我被告知它无法应对本机代码.
任何帮助,将不胜感激.
您可以使用Visual Studio 创建一个安装程序包项目,将所有文件部署到正确的位置或使用其他第三方打包软件(如完整的InstallShield或替代品)
但是,您的问题提醒我在Open Hardware Monitor项目中,它们将驱动程序包含为嵌入式资源,并在用户启动应用程序时将其解压缩.它的工作原理是这样的:他们已经加入WinRing0.sys,并WinRing0x64.sys到项目并设置其生成操作,以嵌入的资源,那么他们必须提取从资源驱动器的方法:
private static bool ExtractDriver(string fileName) {
string resourceName = "OpenHardwareMonitor.Hardware." +
(OperatingSystem.Is64BitOperatingSystem() ? "WinRing0x64.sys" :
"WinRing0.sys");
string[] names =
Assembly.GetExecutingAssembly().GetManifestResourceNames();
byte[] buffer = null;
for (int i = 0; i < names.Length; i++) {
if (names[i].Replace('\\', '.') == resourceName) {
using (Stream stream = Assembly.GetExecutingAssembly().
GetManifestResourceStream(names[i]))
{
buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
}
}
}
if (buffer == null)
return false;
try {
using (FileStream target = new FileStream(fileName, FileMode.Create)) {
target.Write(buffer, 0, buffer.Length);
target.Flush();
}
} catch (IOException) {
// for example there is not enough space on the disk
return false;
}
// make sure the file is actually writen to the file system
for (int i = 0; i < 20; i++) {
try {
if (File.Exists(fileName) &&
new FileInfo(fileName).Length == buffer.Length)
{
return true;
}
Thread.Sleep(100);
} catch (IOException) {
Thread.Sleep(10);
}
}
// file still has not the right size, something is wrong
return false;
}
Run Code Online (Sandbox Code Playgroud)
他们将资源读入缓冲区,将该缓冲区写入磁盘并等待文件刷新到磁盘.