0

For some reasons, I have to create a C++ DLL file containing functions with __stdcall calling convention.

namespace NS
{
    __declspec(dllexport) std::string __stdcall Function(int Num);
}

To be able to use it in C#, I did this way:

[DllImport("mydll.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "?Function@NS@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z"]
static extern String Function(int Num);

The EntryPoint string was obtained with DUMPBIN. When I run the application, I get the error Unable to find an entry point named '?' in DLL 'mydll.dll'.

Both C++ and C# applications were constructed with Visual Studio 2020 in x64 Debug mode.

Can someone suggest how to fix the problem?

Note-> I did not use extern "C" to be able to use std::string and std::vector

  • 2
    You cannot just pass a `std::string` (or `std::vector`) from native c++ to c#. You need to marshal them. And as far as I know pInvoke only supports "old style" c functions. – wohlstad Jun 17 '23 at 12:30
  • You also can't call a mangled name like that, you would need to do some messing around with `GetProcAddress`. If you would have used `extern C` and a normal `LPWSTR` buffered parameter it would be far easier. – Charlieface Jun 17 '23 at 23:59

0 Answers0