Can Any Processor's .NET Application Use P / Invoke Calls in Multiple Environments?

I have a .NET application that uses some API calls like GetPrivateProfileString . Until now, he has always worked on 32-bit machines.

To run on 64-bit machines do I need to change Platform Target to x86? Or is there a way to let the runtime know which API DLL to call depending on the runtime?

+4
source share
2 answers

You need to make sure that you only use P / Invoke calls for the 64-bit DLL.

One option is to move all your “methods” to the standard interface (or abstract base class) and provide 2 implementations, one 32-bit and one 64-bit. You may have a factory method to build the appropriate instance of the class depending on the size of the IntPtr.

This allows the AnyCPU application to correctly, at runtime, determine which DLL for P / Invoke is working.

+3
source

You will have no problem if the DLL whose export you are P / Invoke is also available in 64-bit. Which definitely holds for a Windows DLL, such as kernel32.dll. GetPrivateProfileString () will work just as well, you do not need to change the [DllImport] attribute. Assuming you used IntPtr where you need it.

Odds become lower when using a third-party DLL or COM server that is outdated or not included in Windows. You'll quickly find out if you need to redefine the x86 Target platform, a run-time exception is loud enough. You will get a BadImageFormat exception for pinvoked DLLs, 32-bit COM servers throw a Not Registered class exception.

+4
source

Source: https://habr.com/ru/post/1306844/


All Articles