Excel 2016 VSTO Addin With Native Code DLL

1

Creating an Excel addin using an SDK from a 3rd party. The SDK contains a native code DLL (in both 32 and 64 bit versions). My addin code is in C# and it appears that addins only run if compiled under "AnyCPU" option.

When I attempt to run the app I get "An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)" which usually means a 64/32 mismatch, and the error is from attempting to load the 3rd party native code DLL.

My question is: are my assumptions correct about AnyCPU and is there a way to run native code DLL's from an addin compiled under AnyCPU? Thanks!

c#
c++
excel
dll
vsto
asked on Stack Overflow Apr 5, 2017 by Barry Briggs

2 Answers

0

before .net 4.5 the AnyCPU depends on your current compile machine. if your machine is 32bit CPU, AnyCPU will compile your program as 32bit program. if it's 64bit CPU, i t will compile as 64bits.

after .net 4.5, the AnyCPU changed:

  • If the process runs on a 32-bit Windows system, it runs as a 32-bit process. IL is compiled to x86 machine code.
  • If the process runs on a 64-bit Windows system, it runs as a 32-bit process. IL is compiled to x86 machine code.
  • If the process runs on an ARM Windows system, it runs as a 32-bit process. IL is compiled to ARM machine code.

If you want your program can run both 32bit and 64bit cpu, you should link your native code DLL with 32 bit version, otherwise you need to compile them in separate CPUs

answered on Stack Overflow Apr 5, 2017 by jay liu • edited Apr 5, 2017 by jay liu
0

Yes it is possible, you can create some wrappers functions that redirect the call to the native dll depending on the current architecture , example :

        [DllImport("bin32\\Native86Dll", EntryPoint = "MyFunc", CharSet = CharSet.Ansi, ExactSpelling = true)]
        public static extern int MyFunc_32(string sCommand);

        [DllImport("bin64\\Native64Dll", EntryPoint = "MyFunc", CharSet = CharSet.Ansi, ExactSpelling = true)]
        public static extern int MyFunc_64(string sCommand);

        public static int MyFunc(string sCommand )
        {
            return System.Environment.Is64BitProcess ? MyFunc_64(sCommand) : MyFunc_32(sCommand);
        }
answered on Stack Overflow Apr 5, 2017 by Malick

User contributions licensed under CC BY-SA 3.0