We have a situation where we use a set of third-party unmanaged C++ libraries in our C# (WPF) application, but we also use a subset of their include libraries to build our own unmanaged libraries to use in our application.
These libraries produce metadata, which is stored in a database. However, we must replicate some constants related to this metadata in our C# code, in order to use the metadata.
There is a potential issue, then, if some of these constants change between versions. Is there a clean way for the C# application to use the constants defined in the C++ include files?
Thanks,
wTs
I have a solution that you may not consider terribly “clean” but which will work. The problem is that using either an enum or #define preprocessor directives will discard the symbolic name you’re using for the constant (like ERR_OUT_OF_MEMORY will really just be some integer).
In the C++ code, you could define a function which takes in a string name of the symbol and returns its value. You could use an std::map, then add pairs such as std::pair(“ERR_OUT_OF_MEMORY”, ERR_OUT_OF_MEMORY) Then you can call that function from the DLL using DllImportAttribute which allows unmanaged interop in .NET. Unfortunately this requires going through and string-ifying the constants, but the advantage is that if these constants change, it will be automatic.
Another solution is to write a simple script that goes through preprocessor #defines and creates a gigantic .NET class with a bunch of static constants with the same name. This actually wouldn’t be that hard considering the simplicity of the preprocessor (should only take a few lines of Perl)