Click here to Skip to main content
11,925,896 members (64,513 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++ C++/CLI C

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(,,);
Posted 29-Apr-13 13:19pm
Edited 29-Apr-13 13: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?
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.
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

Rate this: bad
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 30 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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