0

I understand that preprocessor directives are appropriate here, based on this question: Preprocessor directive in C# for importing based on platform

A simple copy paste of

#if WIN64
    [DllImport("ZLIB64.dll", CallingConvention=CallingConvention.Cdecl)]
#else
    [DllImport("ZLIB32.dll", CallingConvention = CallingConvention.Cdecl)]
#endif

at the beginning of my namespace didn't work because Visual Studio is complaining that

Attribute 'DLLImport' is not valid on this declaration type. It is only valid on the 'method' declarations

However, changing the #else line to #elif WIN32 got it to compile.

How would I apply that technique of loading different DLLs based on the operating system to this specific case of Microsoft.Office.Interop.Excel for version 11 vs version 12?

5
  • Probably you have to do it not during compilation time but during runtime. .NET 4 has the dynamic keyword. In previous versions I successfully used Reflection to call methods of Office COM classes.
    – Uwe Keim
    Sep 28, 2012 at 16:13
  • 1
    Preprocessor directives come into play when you build a solution, not when you deploy a solution, so I don't know that they will do what you expect.
    – D Stanley
    Sep 28, 2012 at 16:14
  • Sounds like you're got the DllImport in the wrong spot. It probably builds when you add #elif WIN32 because NEITHER of those constants are defined so it doesn't add the code at all.
    – D Stanley
    Sep 28, 2012 at 16:16
  • @UweKeim this is running on .NET 3.5 Sep 28, 2012 at 16:25
  • @DStanley what would cover the case of deploying? VS won't let me publish if there are build errors, and I'm on Windows 7 64-bit. Sep 28, 2012 at 17:29

1 Answer 1

1

If you want to link to one particular version at compile time (as you sample suggests) - you need to have 2 projects that will include different versions of Interop assembly and refer to the same set of source files.

If you want to do it at run-time and use strongly typed objects you'd need to wrap methods that are interesting to you and expose them through your custom classes/interfaces because interop assemblies do not share any base classes (siblings instead of 11 is parent of 12 as you may wish).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.