QL-Win / QuickLook.Plugin.HelloWorld

Illustrating the plugin development process of QuickLook.
MIT License
21 stars 21 forks source link

Bad image format when loading C++ dll #2

Closed siyu6974 closed 3 years ago

siyu6974 commented 3 years ago

Hi,

Thanks for this great piece of software, I'm devlopping a viewer plugin for a particular astroimage format called FITS. Here is the project link.

The core of my plugin is in C++ which is built as a DLL. The plugin.cs calls functions in it like

 [DllImport(@"viewer_core.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
        public static extern IntPtr FitsImageCreate(IntPtr path);

However at runtime, my plugin crashes and the exception log in QuickLook indicates bad image format.

As far as my little knowledge in the Windows world and some Googling goes, this means the plugin.dll is loading my viewer_core.dll which targets the wrong architecture. Here is some information that I gathered in this regard.

  1. My machine is running 64bit windows. The plugin.cs project is using build configuration "AnyCPU", and my C++ project is built as x64. This produces the above exception.

  2. Same machine. The plugin.cs project is using build configuration "x86", and my C++ project is built as win32. This causes QuickLook to crash at startup.

  3. I have a standalone viewer in c# just for testing purpose, it shares the same code as plugin.cs. This viewer runs ok as x86 with a win32 C++ dll.

It seems to me that neither x64 nor win32 build works. So please advice on how to build a correct C++ DLL.

Thanks

xupefei commented 3 years ago

My machine is running 64bit windows. The plugin.cs project is using build configuration "AnyCPU", and my C++ project is built as x64. This produces the above exception.

You can verify if QL is running in x64 by looking at the Task Manager. Also, you should check the arch of viewer_core.dll using dumpbin: https://docs.microsoft.com/en-us/cpp/build/reference/dumpbin-reference?view=msvc-160

Same machine. The plugin.cs project is using build configuration "x86", and my C++ project is built as win32. This causes QuickLook to crash at startup.

This is expected since 64-bit QL is trying to load your 32-bit DLL and failed.

Basically, you'll have to provide two viewer_core.dll as viewer_core_32.dll and viewer_core_64.dll, and compile your plugin.cs as AnyCPU plus perfer-32-bit OFF.

siyu6974 commented 3 years ago

Thanks a ton, I finally got it working. I think the lesson learnt is to check the cpp.dll that's actually packed with the plugin.dll. Mine kept copying an old x86 version. I was lost in the many output diretories that VS generates (x64, bin/release, bin/x64, etc)