Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

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

C++
extern "C"
{
    #include "cfile.h"
}

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

So i can use the managed code in C# like

C++
var test = managedClass.functionName(,,);
Posted
Updated 29-Apr-13 12:32pm
v2
Comments
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?
—SA
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.
—SA
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?

1 solution

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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900