从Excel-DNA构建的Excel .XLL文件中解压缩内容

时间:2011-09-13 12:56:19

标签: c# excel unpack xll excel-dna

我不知道你是否了解excel-dna项目,这是一个有助于在excel插件中集成.net程序集和语言的项目。

我的问题是我想从xll文件中解压缩一个dll(excel-dna能够在xll中打包资源)。

我已经下载了excel-dna源代码,并且已经在源代码上写了这个基础:

string xlllib = @"C:\pathtomyxllfile\myaddin.xll";
string xlloutput = @"C:\pathtomyxllfile\myaddin.dll";
var hModule = ResourceHelper.LoadLibrary(xlllib);
var content = ResourceHelper.LoadResourceBytes(hModule, "ASSEMBLY_LZMA", "MYASSEMBLYNAME");

using (BinaryWriter binWriter = new BinaryWriter(File.Open(xlloutput, FileMode.Create)))
        {
            binWriter.Write(content);
        }

但它不起作用。 任何人都有从xll解压缩dll的想法吗?

提前感谢,

2 个答案:

答案 0 :(得分:3)

我认为您正在尝试将x86 .xll文件加载到x64位进程中。无法混合x86和x64位代码。而是使用LoadLibraryEx函数将.xll文件作为数据文件加载。

这是一个小代码示例:

[Flags]
enum LoadLibraryFlags : uint
{
  DONT_RESOLVE_DLL_REFERENCES = 0x00000001,
  LOAD_IGNORE_CODE_AUTHZ_LEVEL = 0x00000010,
  LOAD_LIBRARY_AS_DATAFILE = 0x00000002,
  LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE = 0x00000040,
  LOAD_LIBRARY_AS_IMAGE_RESOURCE = 0x00000020,
  LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008
}

internal unsafe static class ResourceHelper
{
  [DllImport("kernel32.dll")]
  public static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hFile,   LoadLibraryFlags dwFlags);
 // other methods such as LoadResourceBytes go here...
}


string xlllib = @"C:\pathtomyxllfile\myaddin.xll";
string xlloutput = @"C:\pathtomyxllfile\myaddin.dll";
var hModule = ResourceHelper.LoadLibraryEx(xlllib, IntPtr.Zero, LoadLibraryFlags.LOAD_LIBRARY_AS_DATAFILE | LoadLibraryFlags.LOAD_LIBRARY_AS_IMAGE_RESOURCE);

var content = ResourceHelper.LoadResourceBytes(hModule, "ASSEMBLY_LZMA", "YOUR_ASSEMBLY_NAME_WITHOUT_EXTENSION");

using (BinaryWriter binWriter = new BinaryWriter(File.Open(xlloutput, FileMode.Create)))
{
  binWriter.Write(content);
}

希望,这有帮助。

答案 1 :(得分:3)

如果您想从Excel-DNA加载项中提取.NET程序集,我编写了一个名为 ExcelDnaUnpack 的小工具。源代码在GitHub上:https://github.com/caioproiete/ExcelDnaUnpack

  

ExcelDnaUnpack 是一个命令行实用程序,用于提取ExcelDnaPack打包的ExcelDna加载项的内容

Usage: ExcelDnaUnpack.exe [<options>]

Where [<options>] is any of:

--xllFile=VALUE    The XLL file to be unpacked; e.g. MyAddIn-packed.xll
--outFolder=VALUE  [Optional] The folder into which the extracted files will be written; defaults to '.\unpacked'
--overwrite        [Optional] Allow existing files of the same name to be overwritten

Example: ExcelDnaUnpack.exe --xllFile=MyAddins\FirstAddin-packed.xll
         The extracted files will be saved to MyAddins\unpacked