Click here to Skip to main content
14,216,965 members
Rate this:
Please Sign up or sign in to vote.
See more:

I have a C function without a class. its just a function in a header file.

I want to expose it as managed code, so i am creating a new C++ file which had

extern "C"
    #include "cfile.h"

public ref class managedClass
         ///// I want to expose the function from cfile.h

So i can use the managed code in C# like

var test = managedClass.functionName(,,);
Updated 29-Apr-13 12:32pm
Philippe Mori 29-Apr-13 18:34pm
Not really a question. Just define a new function in your class and call the original one from the implementation possibly adjusting parameters for easier usage from .NET side.
Sergey Alexandrovich Kryukov 29-Apr-13 18:40pm
Right. Alternatively, it would be better to move the whole function into a "ref" class, if this is possible.
Will you just put your comment as a formal answer, to close the issue?
Marius Bancila 30-Apr-13 7:19am
So you need to wrap the stand alone function with a member function? So what's the actual problem?
Philippe Mori 30-Apr-13 8:35am
The best solution depends a lot on existing code complexity (and if the code is used elsewhere). If the user has a single function then maybe P/Invoke from C# would be the best solution. If the function is very simple, rewitting it in C# might be the best choice...
Sergey Alexandrovich Kryukov 30-Apr-13 11:12am
Sure, agree.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

you only need to call the c-func in a member function. But you better do some error checking around it.

If you link the dll with "delayload" you will get some startup benefit.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100