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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:28:25+00:00 2026-06-03T20:28:25+00:00

Possible Duplicate: A call to PInvoke function '[…]' has unbalanced the stack I’m trying

  • 0

Possible Duplicate:
A call to PInvoke function '[…]' has unbalanced the stack

I’m trying to wrap a few functions written in c++, in order to use them in a c# program (WPF, or as an example a console app), but get run-time exceptions.

I guess this is a compilation issue (of either the c++ project of the C# one). When I change the platform target for the C# projects, I get different exceptions. when the platform target is x64 I get

An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

When I try to compile it as x86, I get:

A call to PInvoke function ‘Wrapper!Wrapper.IWrapper::create’ 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.

I’m running VS2010 on windows 7 64.

Would be happy for any ideas on how to solve this.

This is what I do:

c++ code. cpp:

int create(int Handle)
{
    return 1;
}

and h:

#define EXPORT_DLL __declspec(dllexport)

extern "C"
{
EXPORT_DLL int create(int Handle);
}

Then in a second C# project, I call this dll, where the c++ project’s name is wrapAttemptC

namespace Wrapper
{
    internal class IWrapper
    {
        const string SHADOW_DLL_NAME = "wrapAttemptC.dll";

        [DllImport(SHADOW_DLL_NAME)]
        public static extern int create(int Handle);
    }
    public class CWW
    {
        public CWW(){}

        public int Connect(int cam)
        {
            return IWrapper.create(cam);
        }
    }
}

Then, I put the DLL file created in the c++ project in the bin/Debug of this c# project and of the app c# project (WPF), and try to call

CWW cww = new CWW();
cww.Connect(5);
  • 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-03T20:28:27+00:00Added an answer on June 3, 2026 at 8:28 pm

    The default calling convention for C++ exported functions is cdecl, but the default calling convention for the DllImport attribute is WinApi (which defaults to stdcall). So you have a mismatch there that could indeed mess up your stack. Try stating the calling convention explicitly in the DllImport Attribute, your C++ function declaration, or better, both (so that they match). Like this:

    EXPORT_DLL __stdcall int create(int Handle);
    

    And in your C# project:

    [DllImport(SHADOW_DLL_NAME, CallingConvention = CallingConvention.StdCall)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: how to make phone call by using objective c? I am trying
Possible Duplicate: How can I pre-set arguments in JavaScript function call? (Partial Function Application)
Possible Duplicate: «F(5)» and «int x; F(x)» to call different functions? Can we overload
Possible Duplicate: Call php function from javascript I understand that php is server side
Possible Duplicate: How to call a JavaScript function from PHP? I have a php
Possible Duplicate: What will happen when I call a member function on a NULL
Possible Duplicate: Detect if an outgoing call has been answered How can I know
Possible Duplicate: how can I call javascript function from another project in same solution?
Possible Duplicate: Call to a member function on a non-object What is wrong with
Possible Duplicate: How to force child same virtual function call its parent virtual function

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.