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 8865561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:32:34+00:00 2026-06-14T16:32:34+00:00

I have a C++ DLL with unmanaged code and a C# UI. There’s a

  • 0

I have a C++ DLL with unmanaged code and a C# UI. There’s a function imported from C++ DLL that takes a written-by-me struct as parameter.

After marshalling the written-by-me struct (MyImage) from C# to C++ I can access the content of the int[] array inside of it, but the content is different. I do not know what I am missing here as I spent quite some time and tried a few tricks to resolve this (obviously not enough).

MyImage struct in C#:

[StructLayout(LayoutKind.Sequential)]
struct MyImage
{
    public int width;
    public int height;
    public int[] bits; //these represent colors of image - 4 bytes for each pixel
}

MyImage struct in C++:

struct MyImage
{
    int width;
    int height;
    Color* bits; //typedef unsigned int Color;

    MyImage(int w, int h)
    {
         bits = new Color[w*h];
    }

    Color GetPixel(int x, int y)
    {
          if (x or y out of image bounds) return UNDEFINED_COLOR;
          return bits[y*width+x];
    }
}

C# function declaration with MyImage as parameter:

[DLLImport("G_DLL.dll")]
public static extern void DisplayImageInPolygon(Point[] p, int n, MyImage texture, 
                                                  int tex_x0, int tex_y0);

C++ implementation

DLLEXPORT void __stdcall DisplayImageInPolygon(Point *p, int n, MyImage img,
                                               int imgx0, int imgy0)
{
    //And below they have improper values (i don't know where they come from)
    Color test1 = img.GetPixel(0,0);
    Color test2 = img.GetPixel(1,0);
}

So when debugging the problem I noticed that the MyImage.bits array in c++ struct holds different data.

How can I fix it?

  • 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-14T16:32:36+00:00Added an answer on June 14, 2026 at 4:32 pm

    Since the bits field is a pointer to memory allocated in the native code, you are going to need to declare it as IntPtr in the C# code.

    struct MyImage
    {
        public int width;
        public int height;
        public IntPtr bits;
    }
    

    If you want to access individual pixels in the C# code you’ll need to write a GetPixel method, just as you did in the C++ code.

    Note that since the bits field is a pointer to memory allocated in the native code, I’d expect the actual code to have a destructor for the struct that calls delete[] bits. Otherwise your code will leak.

    This also means that you are going to need to create and destroy instances in the native code, and never do so in the managed code. Is this the policy you currently follow? I suspect not based on the code that I can see here.

    You also need to reconsider passing the struct by value. Do you really want to take a copy of it when you call that function? Doing so means you’ve got two instances of the struct whose bits fields both point to the same memory. But, which one owns that memory? This structure really needs to be passed by reference.

    I think you’ve got some problems in your design, but I can’t see enough of the code, or know enough about your problem to be able to give you concrete advice.


    In comments you state that your main goal is to transfer these bits from your C# code to the C++ code. I suggest you do it like this:

    MyImage* NewImage(int w, int h, Color* bits)
    {
        MyImage* img = new MyImage;
        img->w = w;
        img->h = h;
        img->bits = new Color[w*h];
        for (int i=0; i<w*h; i++)
            img->bits[i] = bits[i];
        return img;
    }
    
    void DeleteImage(MyImage* img)
    {
        delete[] img->bits;
        delete img;
    }
    
    void DoSomethingWithImage(MyImage* img)
    {
        // do whatever it is you need to do
    }
    

    On the C# side you can declare it like this:

    [DllImport(@"dllname.dll", CallingConvention=CallingConvention.Cdecl)]
    static extern IntPtr NewImage(int w, int h, int[] bits);
    
    [DllImport(@"dllname.dll", CallingConvention=CallingConvention.Cdecl)]
    static extern void DeleteImage(ImtPtr img);
    
    [DllImport(@"dllname.dll", CallingConvention=CallingConvention.Cdecl)]
    static extern void DoSomethingWithImage(ImtPtr img);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple DLL written in unmanaged C++ that I access from
I have a unmanaged DLL that exposes a function that takes a pointer to
I have an unmanaged dll that contains a function to read a data from
I have a C++/CLI DLL that uses some managed code (written in C#). I
I have a fortran (unmanaged code) dll as the calculation engine, and a C#
I have an unmanaged dll with a class MyClass in it. Now is there
Here's my problem: I have an unmanaged dll that I made.I'm calling one of
I have a native/unmanaged DLL and it has a CreateObject function which returns a
I have a simple application that loads an unmanaged dll and passes a few
I have DLL and application that will call some function in this dll. For

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.