A continued saga of C# interoprability with unmanaged C++

Posted by Gilad on Stack Overflow See other posts from Stack Overflow or by Gilad
Published on 2012-09-20T09:27:27Z Indexed on 2012/09/20 9:37 UTC
Read the original article Hit count: 292

After a day of banging my head against the wall both literally and metaphorically, I plead for help:

I have an unmanaged C++ project, which is compiled as a DLL. Let's call it CPP Project. It currently works in an unmanaged environment. In addition, I have created a WPF project, that shall be called WPF Project. This project is a simple and currently almost empty project. It contains a single window and I want it to use code from Project 1. For that, I have created a CLR C++ project, which shall be called Interop Project and is also compiled as a DLL.

For simplicity I will attach some basic testing code I have boiled down to the basics.

CPP Project has the following two testing files:

tester.h

#pragma once
extern "C" class __declspec(dllexport) NativeTester
{
public:
    void NativeTest();
};

tester.cpp

#include "tester.h"
    void NativeTester::NativeTest()
    {
        int i = 0;
    }

Interop Project has the following file:

InteropLib.h

#pragma once
#include <tester.h>
using namespace System;
namespace InteropLib {
    public ref class InteropProject
    {
    public:
        static void Test()
        {
            NativeTester nativeTester;
            nativeTester.NativeTest();
        }
    };
}

Lastly, WPF Project has a single window refrencing Interop Project:

MainWindow.xaml.cs

using System;
using System.Windows;
using InteropLib;
namespace AppGUI
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            InteropProject.Test();
        }
    }
}

And the XAML itself has an empty window (default created).

Once I am trying to run the WPF project, I get the following error:

System.Windows.Markup.XamlParseException: 'The invocation of the constructor on type 'AppGUI.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'. ---> System.IO.FileNotFoundException: Could not load file or assembly 'InteropLib.dll' or one of its dependencies. The specified module could not be found.
at AppGUI.MainWindow..ctor()

Interestingly enough, if I do not export the class from CPP Project, I do not get this error. Say, if i change tester.h to:

#pragma once
class NativeTester
{
public:
    void NativeTest()
    {
        int i = 0;
    }
};

However, in this case I cannot use my more complex classes. If I move my implementation to a cpp file like before, I get unresolved linkage errors due to my not exporting my code. The C++ code I want to actually use is large and has many classes and is object oriented, so I can't just move all my implementation to the h files.

Please help me understand this horrific error I've been trying resolve without success.

Thanks.

© Stack Overflow or respective owner

Related posts about c++

Related posts about wpf