I know this isn't a C++/CLI forum but having made posts on codegurus C++/CLI forum and receiving no replies I thought I'd try here. I have a C++ class that I need to use several instances of in C#, so a C++/CLI wrapper is necessary.
After attempting this however the C# project throws an exception "FileNotFound" exception when I try to call Assembly.Load(), so some depencies of the DLL must be missing. I don't understand where I need to add dependencies though, whether my C++/CLI project can just have the source and header files of C++ class I want to wrap added to the solution, or whether it needs a reference to the class etc...so if I lay out exactly what I have could someone try to explain what needs to reference what? It's a fairly simple class.
I have a C++ class called 'Comms' which includes winsock and several other standard C++ headers, and also depends on a header file 'StructDefs.h' which contains the definition of packet structures.
I need a C++/CLI class which just has an instance of comms and wraps all of its public functions, which can be used in C#. Do I need to create a CLR class library? That's what I did on my first attempt, and then I added the header and source files from the C++ project.
FileNotFoundException means the assembly loader could not find the assembly file. If the file was found, and dependencies were not found, the exception would have been of type FileLoadException
Caveat: AFAIK. My knowledge level about things like CLI and JVM can be approximated to zero.
Well the DLL is definitely present, it should be in the same folder as the project file right? Like "Visual Studio 2010\Projects\<ProjectName>\<ProjectName>"
I've added a reference to the DLL too so I shouldn't need the full path should I?
EDIT - I just gave it the full path instead and it found the DLL, now I have a FileLoadException
I was told earlier that if I just add a reference to the DLL in the C# app then I won't need to Load the DLL as it's a .net assembly not a true DLL so it's just like referencing the regular microsoft assemblies. I've added a reference to the assembly however and I can't even see the namespace declared in the wrapper class.
My C++/CLI wrapper defines a public ref class which wraps a C++ class like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
namespace CSharpWrapper {
public ref class Export
{
public:
Export() : m_pComms(new CComms) {}
~Export(){ delete m_pComms; }
UINT Init();
// lots more public functions
private:
CComms* m_pComms; //unmanaged class which is wrapped by Export
};
After adding a reference to the assembly DLL this produces should I be able to see the CSharpWrapper namespace and create instances of the Export class without doing any loading of the assembly? Currently I am unable to.
> After adding a reference to the assembly DLL this produces should I be able to see the
> CSharpWrapper namespace and create instances of the Export class without doing any loading of the assembly?
To the best of my knowledge, yes.
> I can't even see the namespace declared in the wrapper class.
Have no clue about how these things work; so I'm just making a wild guess.
Perhaps an old version of the DLL (in which the class was not public) was copied when you added the reference. If so, removing the reference and adding it again might do the trick.
Have you tried including a reference to the CLR library and using it that way? I created a test c++ project, wrapped it up in a CLR library project, created a C# console app. I then added a reference to the CLR lib in the C# solution, added a using directive for the namespace of my CLR lib and was able to use the PUBLIC interface I created. I did not have to include anything but the dll from the CLR.
I think you said you already tried this after reading your other posts, but leaving these steps just in case.