Wrapping a C++ class

Mar 5, 2012 at 3:14pm
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.

Thanks.


Last edited on Mar 5, 2012 at 3:15pm
Mar 5, 2012 at 3:58pm
Use Assembly.LoadFrom() with the fully qualified path to the .dll?
http://msdn.microsoft.com/en-us/library/1009fa28.aspx

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.
Mar 5, 2012 at 4:25pm
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
Last edited on Mar 5, 2012 at 4:27pm
Mar 5, 2012 at 4:40pm
If you have set the 'Copy Local' property to true, the Assembly would be copied to the Project bin directory (IIRC).

And for Assembly.Load(), verify that you have specified the correct Version, Culture and PublicKeyToken.

Something like:
1
2
Assembly a = Assembly.Load( "myassembly, Version=2.3.1234.0, Culture=neutral, 
                             PublicKeyToken=Null" ) ;


Assembly.LoadFrom() doesn't require all that; just the path to the assembly file is all that is needed.

(The earlier caveat continues to apply.)
Last edited on Mar 5, 2012 at 5:02pm
Mar 5, 2012 at 5:18pm
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.
Last edited on Mar 5, 2012 at 5:21pm
Mar 5, 2012 at 5:40pm
> 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.






Mar 5, 2012 at 5:44pm
I've already tried rebuilding the wrapper project many times and that's not the problem. Even if the class was private, I should be able to have a

 
using CSharpWrapper;

declaration without it flagging up an error as namespaces are public by default.
Last edited on Mar 5, 2012 at 5:44pm
Mar 5, 2012 at 6:18pm
As you might have guessed by now, I've very little to offer by way of a solution.

Hopefully, someone who is not as ignorant in these matters as I am would be able to help you out.
Mar 5, 2012 at 7:05pm
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.
Topic archived. No new replies allowed.