none
Mixing Unmanaged C++, C++/CLI, and C# code RRS feed

Answers

  • Solved the debugging problem. A bit of research threw up a lot of theories. All had different suggestions. Tried them all and it eventually worked. Not sure which were the ones I really needed, in case any one asks.

    Now the work really begins,

    Thanks again for the help.

    • Marked as answer by Anna Cc Monday, June 9, 2014 9:04 AM
    Sunday, June 1, 2014 9:45 AM

All replies

  • Hi,

    I moved this thread to Visual C++ forum for better support because VS
    General Question forum  mainly discusses
    the usage of Visual Studio IDE such as WPF & SL designer,
    Visual Studio Guidance Automation Toolkit, Developer Documentation and Help
    System
    and Visual Studio Editor.

    Best regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, May 28, 2014 9:41 AM
  • Hi,

    Please consider the ShellExecute function.

    You can add the C++ project and the C# project in the same solution. And you can use the ShellExecute function in the start up project. 

    Such as: (a C# project as the start up project)

     [DllImport("shell32.dll")]
            public extern static IntPtr ShellExecute(IntPtr hwnd,
                                                     string lpOperation,
                                                     string lpFile,
                                                     string lpParameters,
                                                     string lpDirectory,
                                                     int nShowCmd
                                                    );
            public enum ShowWindowCommands : int
            {
    
                SW_HIDE = 0,
                SW_SHOWNORMAL = 1,
                SW_NORMAL = 1,
                SW_SHOWMINIMIZED = 2,
                SW_SHOWMAXIMIZED = 3,
                SW_MAXIMIZE = 3,
                SW_SHOWNOACTIVATE = 4,
                SW_SHOW = 5,
                SW_MINIMIZE = 6,
                SW_SHOWMINNOACTIVE = 7,
                SW_SHOWNA = 8,
                SW_RESTORE = 9,
                SW_SHOWDEFAULT = 10,
                SW_MAX = 10
            }
    Best regards,

    Wednesday, May 28, 2014 5:55 PM
  • The following says that it is possible to combine c#, c++ managed and c++ unmanaged code.

    Mixing Unmanaged C++, C++/CLI, and C# code

    Can this be done inside Visual Studio?

    Yes, you can certainly mix these together, and it's the C++/CLI portion that provides the glue. Note that C++/CLI is not true C++ code, but has syntax extensions to support .NET code generation.

    If you want to develop C++/CLI skills, I suggest you consider acquiring the book C++/CLI In Action by Nishant Sivakumar.

    Wednesday, May 28, 2014 6:36 PM
  • Hi Brian,

    Yes I know that you can mix these. The article that I pointed to says so. My problem is how to do it in Visual Studio.  I cannot find how to add a C++ code file to a C# project. Visual Studio does not appear to allow you to simply mix these in the same project. If you add a .cpp file it compiles it as c#.

    Why, if the languages allow it, does Visual Studio not?

    Cheers

    Wednesday, May 28, 2014 7:58 PM
  • You cannot write c++ code in C# project. Both are different languages.

    The example in the link only tells u how to link and use the classes once compiled separately.

    Ideally you can do the following

    1)Create a c++ class say MyCPlusClass in c++/CLI project (.NET supported)

    2) Build it as a dll say MyCPlusDLL.dll .

    3) Create a C# project , add the MyCPlusDLL.dll  as reference.

    4) Now you can use the MyCPlusClass(c++) class inside your C# project.

    This is how you can do it in Visual Studio.



    Sharing is profitable only if it happens with knowledge.

    • Proposed as answer by dharan06 Monday, June 2, 2014 4:41 AM
    Thursday, May 29, 2014 9:29 AM
  • The following says that it is possible to combine c#, c++ managed and c++ unmanaged code.

    Mixing Unmanaged C++, C++/CLI, and C# code

    Can this be done inside Visual Studio?

    Surely it can be done using custom build and/or pre-build and/or post-build steps. Maybe also a C++ poject and a C# project with the latter dependent on the former, but only actually using the former's generated .obj files.

    Many combinations come to mind.


    David Wilkinson | Visual C++ MVP

    Thursday, May 29, 2014 10:41 AM
  • The following says that it is possible to combine c#, c++ managed and c++ unmanaged code.

    Mixing Unmanaged C++, C++/CLI, and C# code

    Can this be done inside Visual Studio?

    You are definitely going to have lots of work ahead.

    Given you are using .SLOW

    move all C++ native code to a DLL

    then you can consolidate the rest.

    you can call a DLL from .SLOW easily


    Corsair Carbide 300R with window
    Corsair TX850V2 70A@12V
    Asus M5A99FX PRO R2.0 CFX/SLI
    AMD Phenom II 965 C3 Black Edition @ 4.0 GHz
    G.SKILL RipjawsX DDR3-2133 8 GB
    EVGA GTX 6600 Ti FTW Signature 2(Gk104 Kepler)
    Asus PA238QR IPS LED HDMI DP 1080p
    ST2000DM001 & Windows 8.1 Enterprise x64
    Microsoft Wireless Desktop 2000
    Wacom Bamboo CHT470M
    Place your rig specifics into your signature like I have, makes it 100x easier to understand!

    Hardcore Games Legendary is the Only Way to Play!

    Thursday, May 29, 2014 1:20 PM
  • Sorry being so long replying. Been busy on other things.

    Ok, thanks to those who suggested the two project/dll approach. I have got a test working ok. It's simple really. I need to try it on something serious now. The only problem I am having is that debugging the c++ is limited. It won't action breakpoints in the c++ code for instance. I get "The breakpoint will not currently be hit. No symbols have been loaded for this document". I presume that it's not picking up symbols for the code in the dll. Is that the .PDB file? Any ideas anyone.

    Many thanks for all the help.

    Cheers

    Sunday, June 1, 2014 9:06 AM
  • Solved the debugging problem. A bit of research threw up a lot of theories. All had different suggestions. Tried them all and it eventually worked. Not sure which were the ones I really needed, in case any one asks.

    Now the work really begins,

    Thanks again for the help.

    • Marked as answer by Anna Cc Monday, June 9, 2014 9:04 AM
    Sunday, June 1, 2014 9:45 AM
  • Ok, thanks to those who suggested the two project/dll approach. I have got a test working ok. It's simple really. I need to try it on something serious now.

    I guesss I am confused. Using C++/CLI  to create a managed class library is the standard way to use C++ in a C# application, but I thought you did not want to do that.

    David Wilkinson | Visual C++ MVP

    Sunday, June 1, 2014 10:58 AM
  • C++/CLI is able to be mixed with C# as both are .SLOW based

    the only code that needs to be in a DLL is any native C++

    I often use mobile machines so I prefer to use C++ across the board so that my products do not melt the CPU into the otherboard

    I noticed a problem with the power policies in Windows 8.1, I forced the policy for the CPU fan on even on battery, guess what, it was reverted back to off


    Corsair Carbide 300R with window
    Corsair TX850V2 70A@12V
    Asus M5A99FX PRO R2.0 CFX/SLI
    AMD Phenom II 965 C3 Black Edition @ 4.0 GHz
    G.SKILL RipjawsX DDR3-2133 8 GB
    EVGA GTX 6600 Ti FTW Signature 2(Gk104 Kepler)
    Asus PA238QR IPS LED HDMI DP 1080p
    ST2000DM001 & Windows 8.1 Enterprise x64
    Microsoft Wireless Desktop 2000
    Wacom Bamboo CHT470M
    Place your rig specifics into your signature like I have, makes it 100x easier to understand!

    Hardcore Games Legendary is the Only Way to Play!

    Sunday, June 1, 2014 1:43 PM
  • Ok, thanks to those who suggested the two project/dll approach. I have got a test working ok. It's simple really. I need to try it on something serious now.

    I guesss I am confused. Using C++/CLI  to create a managed class library is the standard way to use C++ in a C# application, but I thought you did not want to do that.

    David Wilkinson | Visual C++ MVP

    Hi David,

    I would have preferred to do it without, that was the point of the initial question, but there does not seem to be a simple way. Also, the managed class library approach, which I expected to be more complicated, does appear to give me all I need. As most people seem to think it's the usual way, I have given in, as you might say.

    However, I will try your suggestion below if I can work out how to get the C# project to pick up the C++ projects .obj files.

    "Maybe also a C++ poject and a C# project with the latter dependent on the former, but only actually using the former's generated .obj files."

    Maybe the Visual Studio development team should look at providing better support.

    Thanks


    Monday, June 2, 2014 11:33 AM
  • I would have preferred to do it without, that was the point of the initial question, but there does not seem to be a simple way. Also, the managed class library approach, which I expected to be more complicated, does appear to give me all I need. As most people seem to think it's the usual way, I have given in, as you might say.

    However, I will try your suggestion below if I can work out how to get the C# project to pick up the C++ projects .obj files.

    "Maybe also a C++ project and a C# project with the latter dependent on the former, but only actually using the former's generated .obj files."

    Maybe you could start with three projects (native DLL, C++/CLI DLL, and C# GUI), which you could use for debugging. Then add a post-build event to the C# project to build a different executable as described in your link. Anything you can do on the command line, you can do in a build event.

    David Wilkinson | Visual C++ MVP

    Monday, June 2, 2014 7:26 PM