Is it possible to call 64-bit native C ++ DLL files from a .NET application compiled for "Any processor" that works as 64-bit code?

I am writing a program in .NET 3.5, which in some places should exit and execute code from the hardware APIs. All this is good, and works on 32-bit Windows 7, Windows XP, etc. The problem is related to Windows 7 n 64 bit.

I compile to "Any processor" and it works on 64-bit on Windows 7 bit 64 bit. So, in order to interact with this one hardware, I had to write my own wrapper library in unmanaged C ++.

So, after I had everything working in 32 bits, I switched the switches to 64 bits, and that is the problem. The problem I am facing is pretty much the exact opposite of the stack overflow question "An attempt was made to load a program with the wrong format," even if the platforms are the same ... In this question, he is trying to get his code to work in 32 bits (make it ) on Windows 7.

OK, now I do not want to do this, since there is a 64-bit API for this hardware. So, I compiled my code in 64-bit mode, and then after a lot of trial and error, I got it LINK correctly in 64-bit mode, and I have the 64-bit PURE CLEAR library, and it Depends.execonfirms this. Now Depends.exealso displays the provider .dll file as pure 64-bit. This is good, because the first time the code didn't work, I started researching with the manufacturer, and they updated their code a bit with my help, or I have to say with my questions ... :)

, , , . DLL , HW API.dll ( ++) .NET , P/Invoke 64- ...

, , :

[DllImport("xBiometrics.dll", ExactSpelling=true, SetLastError=true)]
[return: MarshalAs(UnmanagedType.I4)]
public static extern Int32 bioScanOpenDevice();

, , .NET :

"System.BadImageFormatException: 
An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)"

, -, - 32- ... ? , , , DLL , 64 , .NET 64-, "Any CPU". ...

- ? , , P/Invoke 64 - ? , ?

, , ? 64- .NET 64- DLL ? DLL 64- DLL ? , 64- DLL DLL ? DLL - DLL . void , , # ( " " ..) , ... ++ #.

+3
5

, 64- DLL #, "Any CPU" 64- .

:

1) # IntPtr.Size . == 8, 64- .

2) 64- DLL DllImport. :

[DllImport(@"c:\fullpath\xBiometrics.dll", ExactSpelling=true, SetLastError=true)]

( , ), , 32- DLL - DLL. .

+4

, - - . Int32, IntPtr.

, 64- . , , VS , , , . , 32- 64-.

, DLL . , .

+3

, , COM- DLL-.

COM 32 - 64 .

, .

+3

, , , , , - , 64-.

0

You may find that for some reason you are actually running a 32-bit infrastructure. You can manage the found DLL files by specifying alternative paths to find the necessary DLL to load. See answer to stack overflow question. How to use the correct unmanaged dll file according to CPU architecture? (32/64 bit) .

0
source

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


All Articles