Create an dll from existing c++ project

Hello...

I have a c++ project and want to use this in a c# program. So, I thought I could make a dll from this c++ code. But I didn't find the right solution at this moment. Maybe you can help me The c++ program consists of about 15 files (.cpp and .h). I think I have to remove the main method, but then ... In my c# programm, I want to call some functions of the c++ code...

Regards,
Jacquipre.


Answer this question

Create an dll from existing c++ project

  • Odelya

    You should look at using C++ interop instead of P/Invoke. It's far more convenient to use.

    And your error seems to indicate that the P/Invoke layer could not find such a function in the DLL. Possibly it's name mangled.

    Jacquipre wrote:
    Hmm...I now tried to use p/invoke stuff:

    My method in the c++ native code looks like this:

    __declspec(dllexport) void extractAll();


    In my c# code, I wrote:

    private void button1_Click(object sender, System.EventArgs e)
    {
    extractAll();
    }

    [DllImport("Extract_Mfcc.dll")]
    public static extern void extractAll();

    But now I get an error:
    'System.EntryPointNotFoundException'

    What is wrong



  • womalley

    See http://msdn2.microsoft.com/en-us/library/ms235281(VS.80).aspx

    Jacquipre wrote:
    Ok...I have now written the native .dll and exportet some functions.

    f.ex:

    __declspec(dllexport) double** GetMfccData(int);

    How do I write a managed wrapper
    How can I now use this in my c# program exactly Can you give me an example



  • Harald Kostinger

    Ok...now I got it ... and it works!

    Thanks for your help!

  • MinakiSerinde

    Harish

    So, are you still getting the IO exception



  • RaguV

    Well, if the c++ code is completely native then there are two ways of doing this. But the very first thing you need to do is to create a native DLL and export all the functions you wish to export. Under VS 2005 this is done by creating a win32 project and selecting DLL. You then enter all the code files you wish and there you go.

    Once you have the native DLL then you are ready to try and get the native functions exported. The ways of doing this are by either a managed DLL written in c++ which wraps the native functions, i.e. the managed functions just basically call the native functions. Or by using platform invoke under c#. There is pleanty of information on platform invoke in the msdn but I think the tricky part would be doing the managed wrapper.

    From my understanding, there is no real difference in performance between the two so the choice of what you do is up to you.

    If on the other hand it is a managed project, you have it easier, you just need to create a managed assembly and import that into the c# project.



  • ding ding

    Hi,
    In an effort to upgrade my old VC++ project to C#, I had to wrap a third party VC dll containg lots of classes and functions.
    Per Nish's say in this context, I have created a mixed-wrapper class, "ManagedDLL". Via a pointer, all the native C/C++ functions are being referred. The wrapped class was successfully referenced in the C# application. There are no compilation errors, but at runtime, I get:

    "An unhandled exception of type 'System.IO.FileNotFoundException' occurred in system.windows.forms.dll
    Additional information: File or assembly name Managed DLL, or one of its dependencies, was not found."
    I am not able to instantiate the dll constructor....
    This is a problem that is boggling me for a long time now and all my development time is running out!
    Suggestions/Tips please........

    Thanks in advance,
    Harish.

  • mirabel25

    From C#, using P/Invoke, you can only access exported functions in native DLLs. You cannot access exported C++ classes using that technique.

    If you insist on using P/Invoke, you'd have to write wrapper functions for every class method (or sequence of methods).

    For e.g., if you have a class A with methods int F1() and void F2(char*), you could do something like this :-

    int AF1()
    {
    A a;
    return a.F1();
    }

    and

    void AF2(char* p)
    {
    A a;
    a.F2(p);
    }

    Now export AF1 and AF2 and P/Invoke them from C#.

    Again, this technique won't be very convenient for classes that have state (like a class that opens a file or a network stream and have methods that access the opened file or stream)

    Jacquipre wrote:
    Normally, I write programs in c#. It's difficult for me to switch to c++.

    I didn't get it.

    Maybe, I try to explain: I have some native c++ classes in one project. I compile this project as a .dll. This works, I think. If I now want to write a managed wrapper for some methods, how will I do this
    Must I made a new project (in visual studio) and include the unmanaged .dll to wrap it Then I made a second (managed) .dll Is that right

    Sorry... :-)



  • planetmarshalluk

    Thanks Nish.
    hhhmm..... Yes, the original dll has to be in the same folder as the wrapper class.

    By the way, the code of the original dll is available. Just for confirmation, in these situations, do you feel that C++ Interops (Implicit PInvoke) would be a better choice over wrapper classes Even otherwise, I could create the wrapper class quiet easily with a little project-setting changes for a mixed dll.



  • Andrew Whiddett

    Ok...I have now written the native .dll and exportet some functions.

    f.ex:

    __declspec(dllexport) double** GetMfccData(int);

    How do I write a managed wrapper
    How can I now use this in my c# program exactly Can you give me an example

  • marwansiala

    Here is the whole picture.
    x.dll is a native VC++ dll of a third party (with code). I have to use this in my C# application. It has got a lot of classes and each are having large number of functions. So created a VC .net class library (xdnet.dll). First compiled x.dll in \clr and referenced in xdnet.dll. For test reasons, I wrapped one of the classes of the x.dll in this fashion:

    public __gc class Widget
    {
    private:
    CWidget* m_pObj; // ptr to native object
    public:
    Widget() { m_pObj = new CWidget; }
    ~Widget() { delete m_pObj; }
    int Method(int n) { return m_pObj->Method(n); }
    // etc.
    };

    apart from this, I have not used any other technique or method. I am able to access the functions in the native class .
    The managed xdnet.dll hence generated is referenced in C# and access the class in xdnet.dll,
    which in turn points to the native class. No compilation errors, at runtime I get IO exception and dll/its dependecies missing.
    This was my approach.
    Rewriting x.dll in VC was a tedious job as it had templates and wrappers to C code! So resolved to wrap the whole dll.
    Sorry for describing twice!
    some good examples explaining the steps along with the project-settings details would be a great for this scenario.
    your comments

  • AndrewStopford

    HarishRavi wrote:
    Thanks Nish.
    hhhmm..... Yes, the original dll has to be in the same folder as the wrapper class.

    By the way, the code of the original dll is available. Just for confirmation, in these situations, do you feel that C++ Interops (Implicit PInvoke) would be a better choice over wrapper classes Even otherwise, I could create the wrapper class quiet easily with a little project-setting changes for a mixed dll.


    When you write a mixed-mode wrapper, you are still using C++ Interop there. I guess your question is whether a wrapper DLL needs to be there, when you can directly use the original DLL, (converting it to a /clr DLL) since you have its source code.

    I'd probably do the wrapper DLL - that way the original DLL is kept native, and fast, and native callers can call directly into it.



  • Dream

    Normally, I write programs in c#. It's difficult for me to switch to c++.

    I didn't get it.

    Maybe, I try to explain: I have some native c++ classes in one project. I compile this project as a .dll. This works, I think. If I now want to write a managed wrapper for some methods, how will I do this
    Must I made a new project (in visual studio) and include the unmanaged .dll to wrap it Then I made a second (managed) .dll Is that right

    Sorry... :-)

  • Tim Pedersen

    HarishRavi wrote:
    Hi,
    In an effort to upgrade my old VC++ project to C#, I had to wrap a third party VC dll containg lots of classes and functions.
    Per Nish's say in this context, I have created a mixed-wrapper class, "ManagedDLL". Via a pointer, all the native C/C++ functions are being referred. The wrapped class was successfully referenced in the C# application. There are no compilation errors, but at runtime, I get:

    "An unhandled exception of type 'System.IO.FileNotFoundException' occurred in system.windows.forms.dll
    Additional information: File or assembly name Managed DLL, or one of its dependencies, was not found."
    I am not able to instantiate the dll constructor....
    This is a problem that is boggling me for a long time now and all my development time is running out!
    Suggestions/Tips please........

    Thanks in advance,
    Harish.

    The original DLL needs to be in the same folder as the wrapper DLL. Is that the case here



  • etaardvark

    Hmm...I now tried to use p/invoke stuff:

    My method in the c++ native code looks like this:

    __declspec(dllexport) void extractAll();


    In my c# code, I wrote:

    private void button1_Click(object sender, System.EventArgs e)
    {
    extractAll();
    }

    [DllImport("Extract_Mfcc.dll")]
    public static extern void extractAll();

    But now I get an error:
    'System.EntryPointNotFoundException'

    What is wrong

  • Create an dll from existing c++ project