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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:39:06+00:00 2026-06-17T07:39:06+00:00

I know this subject was discussed many times here, but I couldn’t find the

  • 0

I know this subject was discussed many times here, but I couldn’t find the answer for my specific situation.

I need to call in C# an unmanaged C method which takes a pointer on a struct object (I don’t speak C fluently :

int doStuff(MYGRID* grid, int x);

But the struct itself references an other struct object :

struct MYGRID {

    int hgap;
    int vgap;

    MYIMAGE* image;

}

struct MYIMAGE {

    int res;
    int width;
    int height;

}

And I also need to set directly the image pointer like this :

MYGRID* pGrid = new MYGRID;
MYIMAGE* pImage = new MYIMAGE;
pGrid->image = pImage;

So, my question is : in C# code, should I use a “struct” object and passing it by “ref” like the P/Invoke Interop Assistant suggests me ? Which means the following code :

MyGrid myGrid = new MyGrid();
MyImage myImage = new MyImage();

myGrid.image = Marshal.AllocHGlobal(Marshal.SizeOf(image)); // A IntPtr in my struct
myGrid.image = Marshal.StructureToPtr(image, myGrid.image, false);

doStuff(ref myGrid, 0);

Or could I use “class” instead of “struct” in order to have the very simple following code :

MyGrid myGrid = new MyGrid();
MyImage myImage = new MyImage();

myGrid.image = myImage;

doStuff(myGrid, 0);

In the first case I use an “IntPtr” in my struct MyGrid, and just a MyImage object in the second case.

  • 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-17T07:39:07+00:00Added an answer on June 17, 2026 at 7:39 am

    Don’t confuse C# struct with C++ struct. They are not the same thing. A C# struct is used to declare a value type. When you aggregate a value type in another type you store it directly in the containing instance instead of storing a reference to an instance stored on the heap. A C++ struct is simply a class where all members by default are public.

    In your case, because MYGRID contains a pointer to MYIMAGE, you should use class as you are doing in you second example. However, the ref on the myGrid parameter should be removed.

    Below is some sample code I have tested. The C++ code:

    #include "windows.h"
    
    struct MYIMAGE {
    
      int res;
      int width;
      int height;
    
    };
    
    struct MYGRID {
    
      int hgap;
      int vgap;
    
      MYIMAGE* image;
    
    };
    
    extern "C" __declspec(dllexport) int doStuff(MYGRID* grid, int x) {
      return 0;
    }
    

    Declaring the C# classes and the external function:

    [StructLayout(LayoutKind.Sequential)]
    class MyGrid {
    
      public int hgap;
      public int vgap;
    
      public IntPtr image;
    
    }
    
    [StructLayout(LayoutKind.Sequential)]
    class MyImage {
    
      public int res;
      public int width;
      public int height;
    
    }
    
    [DllImport("MyDll")]
    static extern int doStuff(MyGrid grid, int x);
    

    Calling the external function:

    MyImage image = new MyImage();
    
    MyGrid grid = new MyGrid();
    grid.image = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MyImage)));
    Marshal.StructureToPtr(image, grid.image, false);
    
    doStuff(grid, 0);
    

    If you turn on unmanaged debugging in your C# project you can use the debugger to step into the C++ function and verify that the classes have been correctly marshaled.

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

Sidebar

Related Questions

I know this subject was many times debated on SO, but I didn't find
I know this subject has been discussed here a bunch of times already. But
I know that this subject has been seen many times here, but I didn't
I know this subject has been covered before on here but I need a
I know this subject was discussed here, but I really can't get this to
I know this subject has been discussed in SO but yet I wasn't able
I know this is not a specific question, but I'm reading about this subject
OK I know that this subject has been mentioned many times before on SO,
I know this subject has been discussed before but still have problem with refresh
I know this SO question, but it deals with the subject in more general

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.