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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:52:25+00:00 2026-05-26T21:52:25+00:00

In C I have a function which registers a callback and a state object

  • 0

In C I have a function which registers a callback and a state object and then passes the state object to the callback every time it is called, much like EventHandler<EventArgs> works in C#. I have a class in C# that registers a managed callback with the C function and currently passes IntPtr.Zero and I simply do not use it because I have not found a good and clean way of passing a managed object reference to C and getting it back to C#.

I do not want the managed object to be accessible in C, I just want to pass it to C and have it passed back (verbatim) to the managed callback every time it is called from C.

Looking for the “right” way of doing this, basically.

Here is my delegate for the callack:

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.SysInt)]
internal delegate IntPtr LuaAllocator(
    [In, MarshalAs(UnmanagedType.AsAny)] object args,
    [In, MarshalAs(UnmanagedType.SysInt)] IntPtr ptr,
    [In, MarshalAs(UnmanagedType.SysInt)] IntPtr originalSize,
    [In, MarshalAs(UnmanagedType.SysInt)] IntPtr newSize);

Here is the current DllImport I’m doing:

    [DllImport(DllName, EntryPoint = "lua_newstate", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    [return: MarshalAs(UnmanagedType.SysInt)]
    public static extern IntPtr NewState(
        [In, MarshalAs(UnmanagedType.FunctionPtr)] LuaAllocator allocator,
        [In, MarshalAs(UnmanagedType.AsAny)] object args);

And the error I’m getting right now is:

MarshalDirectiveException was unhandled:
Cannot marshal ‘parameter #1’: AsAny cannot be used on return types, ByRef parameters, ArrayWithOffset, or parameters passed from unmanaged to managed.

This error goes away if I keep the delegate’s first argument as an IntPtr and use MarshalAs.SysInt but I then cannot reconstruct the managed object, but I do get with a value other than IntPtr.Zero which is curious and may be useful.

  • 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-26T21:52:25+00:00Added an answer on May 26, 2026 at 9:52 pm

    I have found a solution. The HandleRef comment pointed me to GCHandle which I’m already familiar with but had never done this with. By making a non-pinned GCHandle from the object you want to marshal you can call the static GCHandle.ToIntPtr(GCHandle) method to acquire an IntPtr representing that particular handle. Inside the callback I called GCHandle.FromIntPtr(IntPtr) to re-acquire the handle and then retrieved the System.Object from the GCHandle.Target property.

    Here’s how you set up the callback and the state object:

    // Omitted
    string test = "This is a test string.";
    GCHandle handle = GCHandle.Alloc(test, GCHandleType.Normal);
    LuaCore.NewState(allocateCallback, GCHandle.ToIntPtr(handle));
    // Omitted
    

    And here is how you utilize it in the callback:

    private IntPtr Allocate(IntPtr sender, IntPtr ptr, IntPtr originalSize, IntPtr newSize)
    {
        GCHandle handle = GCHandle.FromIntPtr(sender);
        string test = handle.Target as string;
    
        // Omitted
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function which searches an STL container then returns the iterator when
i have a function which retrieves values from a webservice , then loops through
I have foreach function which calls specified function on every element which it contains.
I have a class called GUIMain which registers, creates, and shows a main window
I have this function: RegisterGlobalHotKey(Keys.F6, MOD_SHIFT | MOD_CONTROL); which call an API to register
I have a function which parses one string into two strings. In C# I
I have an function which decodes the encoded base64 data in binary data but
I have a function which gets a key from the user and generates a
I have a function which accepts a string parameter such as: var1=val1 var2=val2 var3='a
I have a function which returns a one-sided intersection of values between two input

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.