I talked with the author of the original C# application program and he mentioned that he linked some 64-bit DLLs which make the application run at 64-bit modes.
So, I tried another alternative: build both DLL & application into 64-bit ones and should not have conflict. It works.
The important is that even using dumpbin /headers to get the 32-bit headers, there is still some 64-bit codes embedded in it.
C# (platform: Active (Any CPU) vs C++ (Win32) DLL ===>>> HRESULT 0x8007000B

I called C++ DLL from C# application. C++ configuration is (x86, Win32) and C# configuration is (x86, Active (Any CPU). I've got HRESULT: 0x8007000B. The two configurations are as attached.
Developer technologies | C++
Developer technologies | C#
-
Stan Huang 421 Reputation points
2021-02-04T05:24:20.02+00:00
2 additional answers
Sort by: Most helpful
-
Timon Yang-MSFT 9,606 Reputation points
2021-01-29T08:56:14.277+00:00 When calling c++ dll in c# code, you need to pay attention to CallingConvention.
The default in C++ is the cdecl calling convention, and the default in C# is the stdcall calling convention.
Therefore, please try to set the calling convention to cdecl in C#.
[DllImport("xx.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] public static extern int AddNumber(int a, int b);
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. -
RLWA32 49,666 Reputation points
2021-01-29T12:05:59.53+00:00 Are you absolutely sure that the C++ DLL that is being used by the C# code is actually a 32-bit DLL?
If you mistakenly copied a 64-bit DLL to the folder that contains the C# executable that is the DLL that will be used since your example does not provide a path to the DLL in the DllImport statement. A subsequent build of a 32-bit DLL C++ project will not necessarily place the DLL output where C# will find it and so the wrong DLL (64-bit version) will be used.
You can verify the bitness of the C++ DLL being used by running dumpbin /headers on it. For examples of the output that show the bitness of a DLL -
32-bit DLL
64-bit DLL