Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Another solution is to make your project a mixed-mode executable. You can have different C++ files in it compiling as different things. Most of it will be just as it was, but you can have different compiler settings for individual C++ files, and compile <em>just those</em> with the <code>/clr</code> compiler flag. You can call .NET objects from that C++/CLI code directly.</p> <p><strong>How you link them is to have a header file in common that has NO .NET aspects to it, just a class or function prototypes, and have the <em>implementation</em> of those in the file that's compiled with <code>/clr</code>.</strong></p> <p>The main "gotchas" here are:</p> <ul> <li>You'll need to disable a lot of other options if you're only compiling that one file <code>/clr</code> like precompiled headers and such <em>for that file</em>. You can still have it on the rest of the project, but not that one. Basically, add your new file, use the <code>/clr</code> option, then start going through your compiler errors, un-checking and changing fields in the properties until it works.</li> <li>Be triple-sure that you're altering the one file's compile options, not the entire project's. Right-click the .cpp file itself.</li> <li>Include the <code>.h</code> file both where you need to call the functions from (unmanaged) and where you're implementing them, which is the <code>.cpp</code> file compiled with <code>/clr</code>.</li> <li>One "cheat" from another website is to actually add to the project a "UI->Windows Form" and since that's CLR, it'll add one file, and set up the rest of the project for you. Then you just remove it, and add back the actual source file you want. Kind of works.</li> <li>Another cheat is to add a new project of type "C++->CLR->CLR Console Application" and then compare the project files so you can get a few things like FrameWork version and such right</li> <li>After doing all of this, save, close, and re-open your solution. Then in the "properties" of the C++ project, you'll be able to add essential things like references to .NET assemblies, like <code>System</code> and anything else you'll need for your CLR sections. In contrast to above, where compiler options need to be per-file, in this case the assembly includes are project-wide.</li> </ul> <p>So be very careful to ONLY set /clr for the specific .cpp files (probably just 1) you want compiled that can access .NET. The rest of your compilation should be unaffected. Then just call your managed static methods (and instantiate classes too, if necessary) from those specific C++ files.</p> <p>If you want a working project for this, send me a PM and I'll email you a 2012 project that does exactly this. </p> <p>Edit: Here's the project: <a href="http://www.mediafire.com/download/rfhk5hx6x27fp0m/MixedModeExecutable.7z" rel="nofollow">http://www.mediafire.com/download/rfhk5hx6x27fp0m/MixedModeExecutable.7z</a></p> <p>Put that into a 2012 solution, and compile it. It should "just work" for things.</p> <p>As for how to make it:</p> <ol> <li>Make a new Win32 console application.</li> <li>Add both a .cpp file and a .h file to the project.</li> <li>Define your functions and/or classes in the .h file using PURE C++ code, not anything that requires the CLR (no <code>String^</code> or whatever) but you CAN forward-declare pointers to classes that WILL contain such things. You'll see this in the example project.</li> <li>Right-click the .cpp file that will contain the <em>implementation</em> of the class and ensure that "Common Language Runtime support" is enabled under "C/C++->General" options. You will also have to change "Debug Information Format" to "Runtime Database" Under "code generation" you will also have to change "Enable Minimal Rebuild" to "No", and "Enable C++ Exceptions" to "Yes with SEH Exceptions" or "No" and "Basic Runtime checks" to "Default". Change "Precompiled Headers" to "Not using". There's a way to use them, but it's a lot of extra work.</li> <li>The bodies of the functions and methods in the special C++ file <strong>can</strong> contain CLR code, and should.</li> <li>If you want to reference additional assemblies, you have to save, then close and re-open the solution, and then you can add them under "Common Properties->Frameworks and references" in the project menu. If you want to make the framework version 4.5 (defaults to 4.0), you have to change the project file and ensure that the line <code>&lt;TargetFrameworkVersion&gt;v4.5&lt;/TargetFrameworkVersion&gt;</code> is there in the right spot in the <code>.vcxproj</code> file. It's correctly 4.5 in the attached project.</li> </ol> <p>So I tried that, and it worked for me. If it doesn't, examine the attached file and see if THAT works for you on VS 2012.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload