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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:05:56+00:00 2026-06-18T05:05:56+00:00

I wrote a C++ DLL and now I need to call a native function

  • 0

I wrote a C++ DLL and now I need to call a native function from a managed app.

The exported native function appears like this:

extern "C" __declspec(dllexport) 
bool NativeMethod(char *param1, char *param2, char *result);

So, from C# I’ll call that function passing 2 input params, 1 output param and obviously I’ll read the return bool value.

I tried to wrap all this in many ways, but always I get a PInvokeStackImbalance exception.
The only way I know to call native function is by applying CallingConvention = CallingConvention.Cdecl) on .NET function declaration. However in this way I’m not able to read the output param (it’s empty string always) and also the return value is always true.

  • 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-18T05:05:57+00:00Added an answer on June 18, 2026 at 5:05 am

    First, I’d adjust the prototype of your native function.

    Since this function has a C interface, you should use a C type for booleans, not a C++ type like bool. You may want to use Win32’s BOOL type.

    Moreover, as it currently is, your function is prone to buffer overruns: it’s better to add another parameter to specify the maximum size of the destination result string buffer.

    Note also that a widespread calling convention for DLLs exporting pure C interface functions (like lots of Win32 API functions) is __stdcall (not __cdecl). I’d use that as well.

    Last, since the first two parameters are input strings, you may want to use const to make it clear and enforce const-correctness.

    So, I’d make the prototype of the exported native function like this:

    extern "C" __declspec(dllexport) 
    BOOL __stdcall NativeFunction(
        const char *in1, 
        const char *in2, 
        char *result, 
        int resultMaxSize);
    

    Then, on the C# side, you can use the following P/Invoke:

       [DllImport(
            "NativeDll.dll", 
            CharSet = CharSet.Ansi, 
            CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool NativeFunction(
            string in1,
            string in2,
            StringBuilder result, 
            int resultMaxSize);
    

    Note that for the output string, a StringBuilder is used.

    Note also that CharSet = CharSet.Ansi is used to marshal C#’s Unicode UTF-16 strings to ANSI (pay attention to the fact that the conversion is lossy – if you want a non-lossy conversion, just use wchar_t* strings on the C++ side as well).

    I did a test with a simple C++ native DLL:

    // NativeDll.cpp
    
    #include <string.h>
    #include <windows.h>
    
    extern "C" __declspec(dllexport) 
    BOOL __stdcall NativeFunction(
        const char *in1, 
        const char *in2, 
        char *result, 
        int resultMaxSize)
    {
        // Parameter check
        if (in1 == nullptr 
            || in2 == nullptr 
            || result == nullptr 
            || resultMaxSize <= 0)
            return FALSE;
    
        // result = in1 + in2
        strcpy_s(result, resultMaxSize, in1);
        strcat_s(result, resultMaxSize, in2);
    
        // All right
        return TRUE;
    }
    

    And it is called successfully by the following C# console app code:

    using System;
    using System.Runtime.InteropServices;
    using System.Text;
    
    namespace CSharpClient
    {
        class Program
        {
            [DllImport(
                "NativeDll.dll", 
                CharSet = CharSet.Ansi, 
                CallingConvention = CallingConvention.StdCall)]
            [return: MarshalAs(UnmanagedType.Bool)]
            static extern bool NativeFunction(
                string in1,
                string in2,
                StringBuilder result, 
                int resultMaxSize);
    
            static void Main(string[] args)
            {
                var result = new StringBuilder(200);
                if (! NativeFunction("Hello", " world!", result, result.Capacity))
                {
                    Console.WriteLine("Error.");
                    return;
                }
    
                Console.WriteLine(result.ToString());
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to import a function that I wrote in C++, and is now
I have a dll written in c++. now i need to call the functions
I wrote a wrapper DLL for some native c++ functions and compiled it in
I have a C++ DLL that I wrote that has a single exposed function,
I have an embedded DLL in my app and I need to write it
Still working on a problem that started from here Calling C++ dll function from
UPDATE - No need to answer this now, I have solved below. Hi, I'm
I need to forward a set of symbols from one DLL to another (to
For testing an existing application I wrote a dll that can be loaded into
I wrote a C++ DLL and it has been compiled. It requires Visual C++

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.