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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:28:04+00:00 2026-05-18T02:28:04+00:00

I’m trying to pass parameter of short type to C++ unmanaged function imported from

  • 0

I’m trying to pass parameter of short type to C++ unmanaged function imported from DLL. In my C++ DLL code I have a following function:

__declspec(dllexport)void ChangeIcon(char *executableFile, char *iconFile,
    UINT16 imageCount)
{
    // Some code, doesn't matter
}

In C# managed code I import this function with the following code:

[DllImport("IconChanger.dll")]
static extern void ChangeIcon(string executableFile, string iconFile,
    ushort imageCount);

And then I call it:

ushort imageCount = 2;
ChangeIcon("filePath", "iconPath", imageCount);

The application executes the function just fine; however, the message with the following text pops out:

Managed Debugging Assistant ‘PInvokeStackImbalance’ has detected a problem in ‘foo.exe’.
Additional Information: A call to PInvoke function ‘bar.IconChanger::ChangeIcon’ 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.

If I do not pass the last parameter, the message doesn’t pop out, so it must be due to passing short type. I have tried with int too, the same message appears.

Obviously, I’m doing something wrong when passing this numerical parameter. How to match parameters between the two?

  • 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-18T02:28:04+00:00Added an answer on May 18, 2026 at 2:28 am

    Make sure the calling convention matches. If you don’t specify a calling convention StdCall is assumed. However for C/C++ the standard calling convention is Cdecl. So either use __stdcall on your C++ function:

    void __declspec(dllexport) __stdcall ChangeIcon(char *executableFile, char *iconFile,
        UINT16 imageCount)
    {
        // Some code, doesn't matter
    }
    

    or specify CallingConvention.Cdecl on the DllImport:

    [DllImport("IconChanger.dll", CallingConvention = CallingConvention.Cdecl)]
    

    As a side note, UInt16 is not CLS a compliant type so you probably want Int16 if you need this compliance.

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

Sidebar

Related Questions

No related questions found

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.