-1

I want to make use of the HIDAPI library for a home-brewed USB device. Rather than having to compile the VC++ project into its own .dll and reference it in the .Net project I have, I would prefer to just have the code present within the .Net project and be able to reference it directly.

The reason is I want to avoid having a .dll referencing another .dll, and instead just have a single .dll file.

I've done it in reverse, sort of, where I have used a C# class within a C++ program when I was doing some JNI stuff.

Is what I am trying to do here possible? Is there an idiots guide to using C++ in .Net projects somewhere?

Will
  • 3,413
  • 7
  • 50
  • 107

2 Answers2

3

"Is what I am trying to do here possible?"

No, it isn't possible this way. These sources can't be compiled within the same project type. You need to have a separate project for the c++/cli assembly, and refer to this one being imported by your c# project.

Warty
  • 7,237
  • 1
  • 31
  • 49
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Also check out http://stackoverflow.com/questions/1933210/c-cli-why-should-i-use-it for more information. – Warty Mar 16 '15 at 22:13
2

Wait, there is a yes answer, but obviously it's completely evil:

How do I mix C# and C++ code in a single assembly?

If your C++ code is not compiled with /clr:safe (i.e. it is compiled with /clr or /clr:pure), do the following:

1) compile your C++ code into .obj files

2) compile your C# code into a .netmodule, using /AddModule to reference the C++ .obj files

3) link the C# netmodule directly with the C++ object files using the C++ linker to create a mixed language assembly

Also this: Linking native C++ into C# applications

Yeah, don't do that.

Nathan Cooper
  • 6,262
  • 4
  • 36
  • 75