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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:27:00+00:00 2026-05-27T14:27:00+00:00

I have a very simple function in an unmanaged DLL, but I’m not getting

  • 0

I have a very simple function in an unmanaged DLL, but I’m not getting the correct return value back from it.

I can confirm that the general PInvoke mechanism is working with one function in my C DLL:

/* Return an integer */
extern "C" __declspec(dllexport) long get_num()
{
    return 42;
}

I call the above unmanaged entry point like so from C# .NET:

[DllImport("My_C_DLL.dll")]
extern static long get_num();
// ...
long ans = get_num();
Console.WriteLine("The answer is {0}.", ans);

This works fine, but passing marshalled parameters to another function in the DLL returns a wrong result:

/* Add two integers */
extern "C" __declspec(dllexport) long add_num(long a, long b)
{
    long sum = a + b;

    return sum;
}

Called from C# as:

[DllImport("My_C_DLL.dll")]
extern static long add_num(long a, long b);

long a = 6, b = 12;
long sum = add_num(a, b);
Console.WriteLine("The answer is {0}.", sum);

This gives me back a result of “6”, or whatever I set the input value of a to be.

I’m guessing that some incorrect marshalling of the input values is messing up the call stack, resulting in the bad return value, but where is the error?

  • 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-27T14:27:01+00:00Added an answer on May 27, 2026 at 2:27 pm

    Two problems here. First of all C# long does not match C long. On Windows, C long is 32 bits. Use int in your C# code to match up with your C long.

    The other problem is that the calling conventions probably don’t match. You most likely have cdecl in your C DLL but the C# default is stdcall. Fix this by changing your p/invoke.

    [DllImport("My_C_DLL.dll", CallingConvention=CallingConvention.Cdecl)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple function that takes a list of comma separated (x,y)
I have written a very simple function: function editCategory() { $ID = urlencode($_GET['id']); $cname
I have a very simple CLR Function for doing Regex Matching public static SqlBoolean
I have got the following very simple code: function init() { var articleTabs =
I have very simple select like this: SELECT * FROM table WHERE column1 IN
Sorry, a beginner's question: I have a very simple function in my Django application
I have a very simple hover function that appears to work fine in everything
I have a small very simple function. $('#desk, #chair, #seat, #tablet').hover( function(){ $(this).find('.hoverBlack').stop().animate({bottom:'0', opacity:'1'},{queue:false,duration:200})
I have a very simple question that is not answered on WordPress Codex. The
I have a very simple script that slides a message down from the top

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.