1

I have a C++ dll which is consumed by C# application. I want to return a string from C++ dll to C# application. below is my sample code. DLL

#include <iostream>
#include<comdef.h>
#include <iomanip>

#include <comutil.h> // #include for _bstr_t

extern "C" __declspec(dllexport) std::string getName();

std::string getName() {
    std::string _name = "XYZ";
    return _name;
}

C# code

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
     [DllImport("NativeLibrary.dll") ]
    public static extern string getName();
    private void btnAdd_Click(object sender, EventArgs e)
    {
        string res = getName();
        Console.WriteLine(res);
    }
}

I am able to build it successfully, but nothing happens when I run the application. How could I get rid of this problem?

Praveer Kumar
  • 912
  • 1
  • 12
  • 25

0 Answers0