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

  • SEARCH
  • Home
  • 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 7973667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:10:18+00:00 2026-06-04T08:10:18+00:00

I have been spending the day working on come c++ code that I need

  • 0

I have been spending the day working on come c++ code that I need to run in c#. I ran through this DLL tutorial and have been having trouble using it in my c# app. I will post all the code below.

I am getting this PInvokeStackImbalance error: ‘A call to PInvoke function ‘frmVideo::Add’ 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.’

Thanks as always,
Kevin

DLLTutorial.h

#ifndef _DLL_TUTORIAL_H_
#define _DLL_TUTORIAL_H_
#include <iostream>

#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif

extern "C"
{
   DECLDIR int Add( int a, int b );
   DECLDIR void Function( void );
}

#endif

DLLTutorial.cpp

#include <iostream>

#define DLL_EXPORT

#include "DLLTutorial.h"


extern "C"
{
   DECLDIR int Add( int a, int b )
   {
      return( a + b );
   }

   DECLDIR void Function( void )
   {
      std::cout << "DLL Called!" << std::endl;
   }
}

C# code to use the DLL:

using System.Runtime.InteropServices;
[DllImport(@"C:\Users\kpenner\Desktop\DllTutorialProj.dll"]
public static extern int Add(int x, int y);
int x = 5;
int y = 10;
int z = Add(x, y);
  • 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-04T08:10:20+00:00Added an answer on June 4, 2026 at 8:10 am

    You C++ code uses the cdecl calling convention and the C# code defaults to stdcall. This mismatch explains the message that you see.

    Make the two sides of the interface match:

    [DllImport(@"...", CallingConvention=CallingConvention.Cdecl]
    public static extern int Add(int x, int y);
    

    Alternatively you could use stdcall for your C++ exports:

    DECLDIR __stdcall int Add( int a, int b );
    

    It’s up to you which of these two options you choose, but make sure that you only change one side of the interface and not both, for obvious reasons!

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

Sidebar

Related Questions

Have been working on this question for a couple hours and have come close
I have been spending some time on this. My problem involves bringing back domain
I have been spending the last hours trying to figure this out and now
I am sure this may be super silly but I have been spending so
I have been writing a growing code base in Haskell. My problem is that
I have been spending some time playing with Clojure-CLR. My REPL is working, I
I have been spending too much time trying to extract this. I have the
In a Ruby project that I have been spending some time on lately, I
i have a report that i have been sending a parameter to which worked
I have been tasked with refactoring some components that used xmlbeans to now make

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.