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

  • SEARCH
  • Home
  • 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 723841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:09:16+00:00 2026-05-14T06:09:16+00:00

So what I have is a C++ API contained within a *.dll and I

  • 0

So what I have is a C++ API contained within a *.dll and I want to use a C# application to call methods within the API.

So far I have created a C++ / CLR project that includes the native C++ API and managed to create a “bridge” class that looks a bit like the following:

// ManagedBridge.h
#include <CoreAPI.h>
using namespace __CORE_API;

namespace ManagedAPIWrapper
{
    public ref class Bridge
    {
        public:
            int             bridge_test(void);
            int             bridge_test2(api_struct* temp);
    }
}

.

// ManagedBridge.cpp
#include <ManagedBridge.h>

int Bridge::bridge_test(void)
{
    return test();
}

int Bridge::bridge_test2(api_struct* temp)
{
    return test2(temp);
}

I also have a C# application that has a reference to the C++/CLR “Bridge.dll” and then uses the methods contained within. I have a number of problems with this:

  1. I can’t figure out how to call bridge_test2 within the C# program, as it has no knowledge of what a api_struct actually is. I know that I need to marshal the object somewhere, but do I do it in the C# program or the C++/CLR bridge?
  2. This seems like a very long-winded way of exposing all of the methods in the API, is there not an easier way that I’m missing out? (That doesn’t use P/Invoke!)

EDIT: Ok, so I’ve got the basics working now thanks to responses below, however my struct (call it “api_struct2” for this example) has both a native enum and union in the C++ native code, like the following:

typedef struct
{
    enum_type1  eEnumExample;
    union
    {
            long        lData;
            int     iData;
            unsigned char   ucArray[128];
            char        *cString;
            void        *pvoid;
    } uData;
} api_struct2;

I think I have figured out how to get the enum working; I’ve re-declared it in managed code and am performing a “native_enum test = static_cast(eEnumExample)” to switch the managed version to native.

However the union has got me stumped, I’m not really sure how to attack it.. Ideas anyone?

  • 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-05-14T06:09:16+00:00Added an answer on May 14, 2026 at 6:09 am

    Yes, you are passing an unmanaged structure by reference. That’s a problem for a C# program, pointers are quite incompatible with garbage collection. Not counting the fact that it probably doesn’t have the declaration for the structure either.

    You can solve it by declaring a managed version of the structure:

    public value struct managed_api_struct {
      // Members...
    };
    

    Now you can declare the method as

    int bridge_test2(managed_api_struct temp);   // pass by value
    

    or

    int bridge_test2(managed_api_struct% temp);  // pass by reference
    

    Pick the latter if the structure has more than 4 fields (~16 bytes). The method needs to copy the structure members, one-by-one, into an unmanaged api_struct and call the unmanaged class method. This is unfortunately necessary because the memory layout of a managed structure is not predictable.

    This is all pretty mechanical, you might get help from SWIG. Haven’t used it myself, not sure if it is smart enough to deal with a passed structure.

    A completely different approach is to make the wrapper class cleaner by giving it a constructor and/or properties that lets you build the content of an api_struct. Or you could declare a wrapper ref class for the structure, much like you would in managed code.

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

Sidebar

Related Questions

We have a number of plugin applications that can all run independently but also
I have an array that contains a list of vertices which I copy to
Using Deepzoom Composer creates a nice ClientBin and a nice HTML page that works
I have sort of a tricky problem I'm attempting to solve. First of all,
-- Running within ASP.NET MVC alongside jQuery -- I'm using jQuery to render a
Am working on web based Job search application using Lucene.User on my site can
I'm looking to build a server with lots of tiny files delivered by an
In Java, web apps are bundled in to WARs. By default, many servlet containers
I am in the process of refactoring our BI layers to make our code

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.