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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:05:41+00:00 2026-05-15T09:05:41+00:00

I have written a C++ wrapper DLL for C# to call. The DLL was

  • 0

I have written a C++ wrapper DLL for C# to call. The DLL was tested and worked fine with my C++ test program.

now integrated with C#, I got runtime error and crashed. Cannot use debugger to see more details.

The C++ side has only one method:

#ifdef DLLWRAPPERWIN32_EXPORTS
#define DLLWRAPPERWIN32_API __declspec(dllexport)
#else
#define DLLWRAPPERWIN32_API __declspec(dllimport)
#endif

#include "NB_DPSM.h"

extern "C" {
 DLLWRAPPERWIN32_API int WriteGenbenchDataWrapper(string fileNameToAnalyze, 
  string parameterFileName,  
  string baseNameToSaveData,
  string logFileName,
  string& message) ;
}

in the C# side, there is a definition,

[DllImport("..\\..\\thirdParty\\cogs\\DLLWrapperWin32.dll")]
public static extern int WriteGenbenchDataWrapper(string fileNameToAnalyze, 
              string parameterFileName,  
              string baseNameToSaveData,
              string logFileName, 
              ref string message);

and a call:

 string msg = "";
    int returnVal = WriteGenbenchDataWrapper(rawDataFileName, 
                   parameterFileName, outputBaseName, logFileName, ref msg);

I guess there must be something wrong with the last parameter of the function. string& in C++ should be ref string in C#?

EDIT:

Do we really need the extern "C"?

EDIT 2:

after I remove the extern "C from the dll, I got the EntryPointNotFoundException. When I look at the dll by using DLL Export Viewer, I found the function name is “int __cdecl WriteGenbenchDataWrapper(class std:: …” Do I need to include the ” __cdecl”?

  • 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-15T09:05:42+00:00Added an answer on May 15, 2026 at 9:05 am

    There are a bunch of rules for marsheling with PInvoke.
    For reference Marsheling between managaed & unmanaged

    Focusing on the C# side first.
    If you knew a reasonable size of the message up front you could use StringBuilder type and define that size, something like.

    [DllImport("DLLWrapperWin32.dll")]
    public static extern int WriteGenbenchDataWrapper(string fileNameToAnalyze, 
                                                        string parameterFileName,   
                                                        string baseNameToSaveData,
                                                        string logFileName, 
                                                        StringBuilder message
                                                        int messageLength );
    

    Impression from the name message (and other posts) indiciates you don’t know the size up front, and you won’t be passing a partial message to the function so maybe

    [DllImport("DLLWrapperWin32.dll")]
    public static extern int WriteGenbenchDataWrapper(in string fileNameToAnalyze, 
                                                        in string parameterFileName,   
                                                        in string baseNameToSaveData,
                                                        in string logFileName, 
                                                        out string message );
    

    Now on the C/C++ side – to match the second definition

    extern "C" // if this is a C++ file to turn off name mangling for this function only
    int WriteGenbenchDataWrapper( char * fileNameToAnalyze, 
                                  char * parameterFileName,   
                                  char * baseNameToSaveData,
                                  char * logFileName, 
                                  char ** message ) {
      string internalMessage;
      SomeFunc( internalMessage ); // these functions won't have extern "C" applied
      * message = (char *)::CoTaskMemAlloc(internalMessage.length()+1); 
      strcpy(* message, internalMessage.c_str());
    }
    

    Consideration of unicode/ansi strings is also important, refer to [MarshalAsAttribute(UnmanagedType.LPWSTR)]

    For release mode you will want to remove your development path settings “..\..\thirdParty\cogs”

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

Sidebar

Related Questions

First_Layer I have a win32 dll written in VC++6 service pack 6. Let's call
I have written a C# .NET Com callable wrapper DLL. I signed it with
i have a c# dll which is written to act as a wrapper to
I have written an C++/Cli wrapper for a native C++ dll, but when I
I have written a program in C# using EmguCV (wrapper of openCV). The program
I have written an O/R database wrapper that generates some wrapper methods for stored
I have written a google visualization treemap wrapper for GWT. I can successfully draw
I have a class written in c# which is acting as a wrapper around
I have a small command-line application written in C that acts as a wrapper/launcher
I have written a program in VB6. When I compile it and send it

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.