4

I don't know if you know excel-dna project, it's a project that help to integrate .net assembly and language in excel addins.

My problem is that I want to unpack a dll from an xll file (excel-dna is able to pack resources inside xll).

I donwloaded the excel-dna sources and already write this base on the source code :

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);
        }

but it doesn't work. Anyone have an idea to unpack a dll from xll ?

thanks in advance,

Lindsey1986
  • 381
  • 1
  • 3
  • 15
Dragouf
  • 4,676
  • 4
  • 48
  • 55
  • Could you please give some more detail. What does not work? Is the content byte array empty? Is hModule zero? Do you get an exception? – Hans Sep 17 '11 at 07:42
  • Yes the problem is that I can't get hModule pointer. LoadLibrary from Kernel32 return zero. I tried with GetModuleHandle but with it's same. – Dragouf Sep 17 '11 at 12:49
  • In the excel dna source project you can find LoadResourceBytes in AssemblyManager.cs (http://exceldna.codeplex.com/SourceControl/changeset/view/67101#209445). Dll inside the XLL file was packed with ExcelDnaPAck project. – Dragouf Sep 17 '11 at 12:54

2 Answers2

4

If you're looking to extract the .NET assemblies from an Excel-DNA add-in, I wrote a small utility called ExcelDnaUnpack. Source code is on GitHub: https://github.com/augustoproiete/exceldna-unpack

ExcelDna-Unpack is a command-line utility to extract the contents of ExcelDna add-ins packed with ExcelDnaPack

ExcelDna-Unpack

Usage: ExcelDna-Unpack.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: ExcelDna-Unpack.exe --xllFile=MyAddIns\FirstAddin-packed.xll
         The extracted files will be saved to MyAddIns\unpacked
C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
3

I think you are trying to load the x86 .xll file into a x64 bit process. It is not possible to mix x86 and x64 bit code. Instead use the LoadLibraryEx function to load your .xll file as a data file.

Here is a small code sample:

[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);
}

Hope, this helps.

Hans
  • 12,902
  • 2
  • 57
  • 60