views:

209

answers:

3

Can we compile C library as .Net dll (containing and opening access to all C libs functions) by just compiling cpp project containing code like

extern "C" {
#include <library.h>
}

with /clr:pure argument with VS? (VS10)

Or we should do something more trickey?

+2  A: 

It really depends on your C code.

P/Invoke is often the easiest to start with, and IMO is pretty workable for a handful of functions. Performance isn't necessarily great, and I wouldn't construct an entire program out of it - but to reuse some functions, it's worthwhile.

Going from C to /clr:pure requires you to:

  1. Convert your C code to C++
  2. Convert your C++ code to Visual C++
  3. Compile with /clr option
  4. Compile with /clr:pure

The current state of your code (and it's libraries) will dictate how painful that process is.

Mark Brackett
"thay" say you do not need P/Invoke to use .Net dll. "thay" say you can compile C++ (CPP files) code into .Net dll using 'clr:pure' argument with VS.
Blender
@Ole - "They" are correct. It's getting to that point from C which is likely to give you fits.
Mark Brackett
In his other (I've lost count, maybe five) questions about this, Ole set a requirement that everything compile into a single file. `DllImport` won't get you there.
Ben Voigt
+3  A: 

I found it is the best to use the old style Managed C++ for this.

CLR:PURE just wont cut it.

Example:

extern "C" int _foo(int bar)
{
  return bar;
}

namespace Bar
{
  public __gc class Foo
  {
  public:
    Foo() {}

    static int foo(int bar)
    {
      return _foo(bar);
    }
  };
};

Compile with: /clr:oldSyntax

Now you can reference the assebmly, and call Bar.Foo.foo() from .NET.

leppie
What do you mean by it - please provide simple samle?
Blender
@Ole Jak: Example added.
leppie
could you explain what '__gc ' is, how will this code compile into .net dll (I mean what will we acsess in it and how?)
Blender
@Ole Jak: `__gc` just indicate that this is a managed class, part of the old style syntax. You can view that in Reflector. Will be an internal class called `Foo` in the 'module' namespace.
leppie
@Ole Jak: I added a namespace and made it public, so it can be tested easier.
leppie
I like your answer - it is a ~relevantly fast and easy way of creating .Net librarys from pure C code thru C++ code using Visual studio tools. gonna try it as soon as possible. thank you very much!
Blender
I love your technique, but I wonder how to use it for more or less complex C structures and pointers? So I asked here http://stackoverflow.com/questions/3239090/ Could you please give an answer?
Blender
Don't try to make the existing code publicly visible, just get it to compile. Then write a handful of functions in C++/CLI (avoid oldSyntax as a rule of thumb, but especially if you plan to use ILMerge) that implement the high-level tasks of your application using the x.264 library. These can call into the C library functions just as easily as native C++ can.
Ben Voigt
@leppie After all, could you please at least give some notes on stackoverflow.com/questions/3239090 there is bounty btw=)
Blender
+1  A: 

It's not generally a given that you can even compile C code as C++ without making some changes. If you can get your C code to compile as C++, then you can try getting it to compile as C++/CLI (that's what the clr:pure option does).

At that point, you can create some kind of class which exposes all your exported functions as static methods of a public (managed) class.

Some flavours of this sort of stuff can be done with C++ preprocessor tricks (macros, etc), sometimes you end-up writing wrappers manually.

So the basic information that you can compile C++ into .NET assemblies using /clr:xxx options is true, but that doesn't mean it's the only thing you need to do to get a useful .NET assembly.

Will Dean
are out there any step by step tuts or stories of sucsess in blog articles or something like that?
Blender