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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:20:02+00:00 2026-05-26T01:20:02+00:00

I have the following native function interface in C++: int func1(void* param, int sizeOfParam).

  • 0

I have the following native function interface in C++:

int func1(void* param, int sizeOfParam).

In documentation the following example of call is provided:

typedef struct
{
    int x;
    int y;
    int width;
    int height;
} Rect;

Rect rect;

int func1((void*)&rect, sizeof(rect));

I need to call this function from C# code.

I have the following header in C# from developers of native library:

[DllImport(NATIVE_DLL_NAME, 
 CallingConvention = CallingConvention.Cdecl, 
 EntryPoint = "func1")]
private static extern int func1(IntPtr param, int sizeOfParam);

I also have the following C# structure Rect:

public struct Rect
{
    int x;
    int y;
    int width;
    int height;
};

I need to call func1 in C# code and pass Rect:

I do the following:

Rect rect = new Rect();
int rectSize = System.Runtime.InteropServices.Marshal.SizeOf(rect);

func1(???, rectSize);

What to place in position of ??? where rect should be passed (but it is not possible because of incompatible types)?

It seems that IntPtr should be passed and then converted to struct rect. How to achieve this?

(rect is output parameter here)

UPDATE:

It is desired not to change signatures of C++ code and C# wrappers – it is third part code.
Moreover it is not always variable of Rect is passed as first param of func1

  • 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-26T01:20:03+00:00Added an answer on May 26, 2026 at 1:20 am

    You changed the rules of the game to disallow modifications to the C# code. And so the P/invoke must be of this form:

    private static extern int func1(IntPtr param, int sizeOfParam);
    

    In that case you need to do the marshalling by hand:

    int size = Marshal.SizeOf(typeof(Rect));
    IntPtr param1 = Marshal.AllocHGlobal(size);
    try
    {
        func1(param1, size);
        Rect rect = (Rect)Marshal.PtrToStructure(param1, typeof(Rect));
    }
    finally
    {
        Marshal.FreeHGlobal(param1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to call a DllImport on the following Alsa function (from the documentation
I have following classes. class A { public: void fun(); } class B: public
I'm trying to call a native function from a managed assembly. I've done this
I'm using the PHP native mail() function to send HTML emails and have a
I have the following code, which seems to override the jQuery.error function: $.test =
I have a C#.NET 2.0 CF application that imports a function from a native
My problem is the following; I have a class that uses non-static native methods
Is it possible to call a native CPP function using JNI which takes generic
I have a native library which is invoked by the JNI function called from
I have a function that has several outputs, all of which native, i.e. integers

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.