Why isn't my c++ dll properly loading into my c# exe?

Forrest Crawford 80 Reputation points
2024-06-13T13:31:40.4133333+00:00

I'm continually getting this error message but I can't find the source. Here's a link to the .dll class I want to utilize:

link

And here's the c# class I'm calling it from:

link

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,029 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,764 questions
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 45,701 Reputation points
    2024-06-13T13:58:54.7766667+00:00

    There are two initial problems with the projects in the posted links.

    1. The debug configuration of the C++ DLL project is configured to build an Application instead of a Dynamic Link Library.
    2. After correcting to build a DLL the .Net application will not find it because the default location used for output from the C++ project is not searched. You can insert the fully qualified path to the DLL in the .Net code or have a post-build event to copy the C++ DLL to the .net bin sub-folders where the WPF application is located or change the C++ project property so that the DLL is created in the same folder that the WPF project uses for outputting its .exe
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Castorix31 85,956 Reputation points
    2024-06-13T14:45:57.44+00:00

    I quickly tested your DLL and you create m_pDirect2dFactory after InitD3D where you call it in

    hr = m_pDirect2dFactory->CreateDxgiSurfaceRenderTarget(pBackBuffer, &props, &m_pBackBufferRT);

    so it crashes as m_pDirect2dFactory = NULL.

    (and you don't need any DLL for Direct2D, you can use P/Invoke in WPF (like in the WinUI 3 samples I posted...)

    0 comments No comments

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.