Thread: C# and C++ static library integration?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    35

    Question C# and C++ static library integration?

    This may not be the proper place to ask but here goes...

    When creating a GUI to interface with my C/C++ code in the past I have used MFC. Now I have switched to strictly using C# for creating GUI's.

    The issue is that I need to create a GUI for a C++ app and I do not want to go back to MFC to create it.

    Could someone point me in the proper direction to understand how I can add my C# designed interface into my MSVS solution and have it call functions from my C++ project.

    For instance, my C# dialog handlers need to be able to access function in other C++ projects. These projects will have be included in the solution and are compiled into static libraries. So presumably I would be able to call these function simply by making them dependencies of my C# project.

    I have heard of issues with managed and umanaged code, and I am not sure whether they will apply to my situation or if it is a non issue and I should ignore it.

    any ideas ?

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    The company I worked for tried to do exactly this. It doesn't work very well. Unfortunately, I can't remember exactly what issues we ran into; I can only remember in broad terms. From what I remember, there's a compiler bug preventing you from putting managed and unmanaged code in the same assembly (except in the most trivial cases). There's another issue with passing memory either from an assembly of managed code into a library of unmanaged code or vice versa.

    I'd love to be more specific, but it's been two years since we tried that. We spent four months just trying to get the C#-C++ interface to work. It took us about another four months to just rewrite all of our C++ code in C#, and we haven't looked back since. If you get it to work, more power to you.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    AFAIK there is no way to have C++ and C# code in the same assembly. You need a well defined interface to seperate you presentation layer ( GUI ) from your logic ( algorithms, database access, etc ).

    You have two choices:

    Create normal C++ dlls. You can call them from C# rather easy if you have simple types ( primitives and fixed length structures ).

    OR

    Create a wrapper dll in Managed C++ ( now C++/CLI ). You can include both managed and unmanaged code here, so you can have your C++ classes AND your managed interface. Beware, Managed C++ is a pain in the behind, but wrapping complex classes is well worth it.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    35

    c++ dll's

    So for my purpose if I recompile my static libs into C++ dll's how do I access them from my C# code?

    What headers, includes etc... will I need to pull them in and what will I need to change in my C# project settings?

    Once this is complete what will I have access to within the dll's?

  5. #5
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Add the reference to the c++ dll in your project ( using visual studio ).

    Use the keyword "using namespace" to include that namespace ( the namespace defined in the c++ dll ).

    Once this is done, you can access everything from the dll.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    35
    I was working in .NET 2003 but I didn't specify anywhere to use managed C++ code so I assume it's unmanaged. Ergo it would be too much overhead to make these compatible so I scraped the C# GUI and rebuilt it in MFC

    thanks for the replies

Popular pages Recent additions subscribe to a feed