Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7727989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:36:13+00:00 2026-06-01T05:36:13+00:00

We have a big C++ project that’s compiled as native unmanaged code. We need

  • 0

We have a big C++ project that’s compiled as native unmanaged code. We need to use a feature from managed code, but we don’t want to compile the whole project in /clr.

So I made a DLL, have a ref class named B, which is exposed in exported native class A. Problem is I get a C1190: managed targeted code requires a ‘/clr’ option because of the vcclr.h include.

I’d like to know if there is a way to create some kind of interface that will have managed code within unmanaged methods.

Here’s my code:

#pragma once
#include "EX_Port.h"
#include <vcclr.h>

ref class B;

class EX_API A
{
    public:
        A();        
        int DeviceCount();

    private:
        gcroot<B^> _device;
};

I managed to make it work by gcnew the B class within the cpp. But then I have a local object while I’d like to have it in the global scope. I just began doing CLI programming so I might not be aware of some practices.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:36:14+00:00Added an answer on June 1, 2026 at 5:36 am

    Your big C++ program is going to have to load and initialize the CLR before it can execute any managed code. There are several ways to do this, ranked from most flexible to least:

    • It can use the CLR hosting interface to explicitly load the CLR and execute arbitrary managed code. Basic starter for that is this MSDN page and the many examples you can find on sites like CodeProject.com

    • You can make your managed classes [ComVisible]. Your C++ code then can use standard COM programming techniques to create an instance of the managed class and call its methods (CoInitializeEx and CoCreateInstance, the #import directive). The COM plumbing ensures that the CLR is automatically loaded and loads the proper assembly, no additional code is required to manage that yourself. Consider this option when you already have an investment in COM, not otherwise something you should consider if you have no working knowledge of COM.

    • The two above techniques allow any kind of managed code to be executed, not just C++/CLI code. Specific to C++/CLI, you can write a free function and apply the __declspec(dllexport) attribute to it. The compiler will generate a stub that exports the function so you can call it from your C++ code with LoadLibrary + GetProcAddress. The stub automatically loads the CLR. This is very easy to get going but is pretty inflexible since you are only exposing a simple function and not a class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a big project that also use many libraries. With jstack I found
I have a really big project that will use for example mails, contacts and
I'm writing mostly embedded code at work. We have a big long-term project that's
I have a big project that was first created in Borland C++ 6. We're
i have a big solution containing that much projects..i need to find a specific
I have a big project compiled into libProject.so-file (shared library), I made some modules
I have a big project that uses maven and I'm creating the project's jar.
I have inherited a project that has an awkwardly big interface declared (lets call
Some complex subversion merges are coming up in my project: big branches that have
I have a project that used to be under SVN, but now I'm moving

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.