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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:23:27+00:00 2026-06-04T11:23:27+00:00

To simulate a call from c++, I’m trying the following code private delegate void

  • 0

To simulate a call from c++, I’m trying the following code

    private delegate void CppFuncDelegate(string message);

    private static void Main(string[] args)
    {
        Console.WriteLine("+++ BEGIN TEST +++");

        Action<string> action = str => Console.WriteLine("Received:" + str);
        IntPtr delegatePtr = action.Method.MethodHandle.GetFunctionPointer();

        CppFuncDelegate cppFuncDelegate = (CppFuncDelegate)
           Marshal.GetDelegateForFunctionPointer(delegatePtr, 
                                                        typeof(CppFuncDelegate));
        cppFuncDelegate.Invoke("Hello");
    }

but I get

PInvokeStackImbalance was detected.
A call to PInvoke function ‘Test!Test.Program+CppFuncDelegate::Invoke’
has unbalanced the stack. This is likely because the managed PInvoke
signature does not match the unmanaged target signature. Check that
the calling convention and parameters of the PInvoke signature match
the target unmanaged signature.

Can anyone tell me what I’m doing wrong?

NOTE: Please don’t tell me to do action.Invoke(); that’s not what this exercise is about. I want to get IntPtr handle to the delegate and use GetDelegateForFunctionPointer() and then invoke the returned delegate.

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-04T11:23:28+00:00Added an answer on June 4, 2026 at 11:23 am

    You cannot use GetDelegateForFunctionPointer() to get delegate of managed function. This is from MSDN:

    You cannot pass an invalid function pointer to
    GetDelegateForFunctionPointer. In addition, you can only use this
    method for pure unmanaged function pointers. You cannot use this
    method with function pointers obtained through C++ or from
    GetFunctionPointer. You cannot use this method to create a delegate
    from a function pointer to another managed delegate.

    I’m not sure why you can’t do this, but I guess it’s because Microsoft’s Common Language Runtime uses FastCall calling convention, but FastCall is not supported for PInvoke.

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

Sidebar

Related Questions

How can I simulate an incoming call from a private number -- in an
I am trying to send a broadcast to simulate an incoming call. I added
I'm trying to simulate the entry of a string in a textbox on a
I need to simulate a test scenario in which I call the getBytes() method
Can I simulate in C#/C++ code Control + Alt + Delete sequence in Vista?
I am trying to simulate a scenario where connection to the server of one
I'm wondering how you would simulate a :has_many :through using an AR call. Ultimately,
I have a global.php file that I call from the top of every script.
If I call an action X from another test A using LoadAndRunAction , are
I've created a custom control I call ControlFader. It inherits from the Selector primitive.

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.