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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:41:48+00:00 2026-05-25T02:41:48+00:00

I am attempting to call a C++ function in a DLL from C# via

  • 0

I am attempting to call a C++ function in a DLL from C# via interop. The DLL was written by someone else. The function signature is as follows:

AXI_DLL_EXPORT int __stdcall GetFileType(StringParam *stringParam, int *fileType)

StringParam is a struct defined as follows:

struct StringParam
{
    int size;    // 4 byte integer for the size of the string buffer
    LPWSTR buff; // Pointer to a wide char buffer
}

This struct is used for passing strings back and forth. It can be used as both in and out parameters. The purpose of the size field in the struct is primarily for returning strings to the caller and to indicate the size of the buffer required. If the size of the buffer (provided by the caller) is too small to hold the string the caller can be informed of this and can then provide a larger buffer in a successive call to the function.

In C# I created a corresponding struct as follows:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct StringParam
{
    public int Size;

    [MarshalAs(UnmanagedType.LPWStr)]
    public string Text;

    public StringParam(string text)
    {
        this.Text = text;
        this.Size = text.Length;
    }
}

I declared the DllImport function call as follows:

[DllImport("Custom.dll", CharSet = CharSet.Unicode)]
public static extern int GetFileType(StringParam stringParam, out int fileType);

The call fails with the following error:

A call to PInvoke function ‘… GetFileType’ 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.

How should I declare the struct and call the native function from C#?

  • 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-25T02:41:49+00:00Added an answer on May 25, 2026 at 2:41 am
    public static extern int GetFileType(ref StringParam stringParam, out int fileType); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to call a method written in C++/CLI from C#. The C++/CLI code
This is my first time attempting to call an ASP.NET page method from jQuery.
I'm using ASP.NET and attempting to call a method with a signature of [WebMethod]
I'm attempting to do an AJAX call (via JQuery) that will initiate a fairly
I'm attempting to use VBAs Application.OnTime method to call a function every minute. The
I'm attempting to parse a function call for a language that I am creating,
When I want to call a function in javascript with arguments supplied from elsewhere
I'm attempting to call a function inside of a lua file called test2.lua This
Is it possible to call a function defined in a non-specialised template class from
I am attempting to send a message via sockets from VBA. I have made

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.