2011-09-13 5 views
4

excel-dna 프로젝트를 알고 계시는지 모르겠다. Excel 어셈블리를 Excel 통합에 도움이되는 프로젝트입니다.Excel-DNA로 작성된 Excel .XLL 파일의 내용 압축 해제

내 문제는 xll 파일 (excel-dna가 xll 내부의 리소스를 압축 할 수 있음)에서 dll을 압축 해제하려고합니다.

나는 엑셀-DNA 소스를 donwloaded 이미 소스 코드에이 기본 쓰기 :

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을 압축 해제 할 생각이 있습니까? 사전에

덕분에,

+0

로 포장 ExcelDna 추가 기능의 내용을 추출하는 명령 줄 유틸리티입니다. 작동하지 않는 것은 무엇입니까? 내용 바이트 배열이 비어 있습니까? hModule은 0입니까? 예외가 있습니까? – Hans

+0

예, 문제는 hModule 포인터를 가져올 수 없다는 것입니다. Kernel32의 LoadLibrary는 0을 반환합니다. 나는 GetModuleHandle로 시도했지만 그와 동일하다. – Dragouf

+0

excel dna 소스 프로젝트에서 AssemblyManager.cs에서 LoadResourceBytes를 찾을 수 있습니다 (http://exceldna.codeplex.com/SourceControl/changeset/view/67101#209445). XLL 파일 내의 Dll은 ExcelDnaPAck 프로젝트로 압축되었습니다. – Dragouf

답변

2

나는 당신이 64 비트 프로세스에 파일을 .xll는 x86를로드하려고 생각합니다. 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); 
} 

희망이 도움이됩니다.

+0

훌륭하고 작동합니다! 고마워요 Hansjoerg! – Dragouf

3

Excel-DNA 추가 기능에서 .NET 어셈블리를 추출하려는 경우 ExcelDnaUnpack이라는 작은 유틸리티를 작성했습니다. 소스 코드는 GitHub의에 : https://github.com/caioproiete/ExcelDnaUnpack

ExcelDnaUnpack는 좀 더 자세히 전해 주 시겠어요 ExcelDnaPack

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 
+0

2 년 전 : https://github.com/dragouf/ExcelDnaUnPacker. 하지만 네가 좀 더 일반적인 것 같아. – Dragouf

+1

@Dragouf 와우! 이제 저는 1 년 전에 이것을 썼을 때 제가 검색했으면 좋았을 것입니다 ... 저에게 많은 일을 덜어 주셨으면합니다! –