6

XCode's ARC refactoring forced my Cocoa Library DLL to be 64bit, and I don't know if I can still DllImport that DLL from an x86 C# application. Is this possible, and are there any consequences of doing so?

Mr. Smith
  • 4,288
  • 7
  • 40
  • 82
  • Good question...I'll be interested to see the answers. Off the top of my head I'd think you wouldn't be able to go from 64 bit to 32 bit, but I could be wrong. – Tim Mar 13 '13 at 21:33
  • Forward compatibility is always iffy, but this case was known far enough in advance that I too am interested to see the answer. – Pieter Geerkens Mar 13 '13 at 21:36
  • You can do that, but you'll get a BadImageException at runtime. – harold Mar 13 '13 at 21:44
  • Opposite question http://stackoverflow.com/questions/128445/calling-32bit-code-from-64bit-process has the same answer -mix not supported – Michael Freidgeim Dec 17 '16 at 12:28

3 Answers3

8

You cannot mix 32 bit and 64 bit code in a single process. So the only way to use mix bitness code is to have more than one process. You'll need some form of IPC to make it work. You cannot do it with DllImport since that is in-process.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

The problem is not really C# - it is the hosting process in the OS. Since one process can only load DLLs with the same "bitness", either the process is 64bit or you cannot directly load your DLL. No matter what language or framework you are using.

One solution would be to target the C# project to use "any" cpu or specifically point it to X64.

Another solution would be to create a hosting process that you can communicate with using IPC or similar models.

Roman Gruber
  • 1,411
  • 11
  • 16
0

The solution when necessary is to call an EXE in pipeline or similar. This assumes of course a 64 bit windows. If not, punt.

Joshua
  • 40,822
  • 8
  • 72
  • 132