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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:52:21+00:00 2026-05-16T01:52:21+00:00

The Following is code of a function from a unmanged dll. It takes in

  • 0

The Following is code of a function from a unmanged dll. It takes in a function pointer as argument and simply returns value returned by the called function.

extern __declspec(dllexport) int  _stdcall callDelegate(int (*pt2Func)());
extern __declspec(dllexport) int  _stdcall callDelegate(int (*pt2Func)())
{
    int r = pt2Func();
    return r;
}

In managed C# code I call the umanged function above with a delegate.

  unsafe public delegate int mydelegate( );

    unsafe public int delFunc()
    {
             return 12;
    }

    mydelegate d = new mydelegate(delFunc);
    int re = callDelegate(d);
   [DllImport("cmxConnect.dll")]
    private unsafe static extern int callDelegate([MarshalAs(UnmanagedType.FunctionPtr)] mydelegate d);

This all works great !! but if I want my function pointer/delegate to take arguments it crashed the program.
So if I modify the code as follows my program crashes.

Modified unmanaged c++ –

extern __declspec(dllexport) int  _stdcall callDelegate(int (*pt2Func)(int));
extern __declspec(dllexport) int  _stdcall callDelegate(int (*pt2Func)(int))
{
    int r = pt2Func(7);
    return r;
}

Modified C# code –

unsafe public delegate int mydelegate( int t);

        unsafe public int delFunc(int t)
        {
                 return 12;
        }

        mydelegate d = new mydelegate(delFunc);
        int re = callDelegate(d);
  • 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-16T01:52:22+00:00Added an answer on May 16, 2026 at 1:52 am

    The calling convention for the function pointer is wrong. Make it look like this:

     int (__stdcall * pt2Func)(args...)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following piece of code from a book. There is this function
Let's consider the following scenario: a function which can generate code colors from white
following up from yesterday... This portion of the code does work. $(document).ready(function(){ $('#listMenu a').click(function
I am trying to import a function from an unmanaged code c++ dll into
I want to call the following function from my managed code: short LS_LoadConfig(LS_ID SensorID,LPVARIANT
In continuing my question from yesterday , I have the following code: function VAL(str)
Trying to call a function from another file in C with the following code:
I' m trying to enable adding/deleting rows to/from table using folowing code: Script: $(document).ready(function()
I have the following code: function Person(){ this.age = 30; } function Stats(){ this.age
Suppose I have the following code function myFunction(param, callback) { ... if (err) {

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.