C# (platform: Active (Any CPU) vs C++ (Win32) DLL ===>>> HRESULT 0x8007000B

Stan Huang 421 Reputation points
2021-01-29T02:04:02.563+00:00

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.61594-x86-c.jpg61595-x86-c.jpg

Developer technologies | C++
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Stan Huang 421 Reputation points
    2021-02-04T05:24:20.02+00:00

    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.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. 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.


  2. 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

    37685-db32.png

    64-bit DLL

    37711-db64.png


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.