1

I have a C++ developed DLL which I want to load and call a function on C# but I got Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Error. My C++ code is like below:

#include "EvenOdd.h"

extern "C"
{
    __declspec(dllexport) std::string CheckEvenOdd(const int iNum)
    {
        std::string strRes = "NULL";
        if(iNum%2 == 0)
        {
            strRes = "The given number is Even";
        }
        else
        {
            strRes = "The given number is Odd";
        }

        return strRes;
    }
}

My C# code is like Bellow:

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;

namespace C_Sharp_DLL_Test1
{
    class Program
    {
        [DllImport("C:\\Users\\saja\\Downloads\\LoadLibrary\\GetEvenOdd\\Debug\\GetEvenOdd.dll", CallingConvention=CallingConvention.StdCall)]
        [MethodImpl(MethodImplOptions.ForwardRef)]
        static extern string CheckEvenOdd(int num);
        static void Main(string[] args)
        {
            string s = CheckEvenOdd(23);
            Console.WriteLine(s);   
            Console.ReadLine();
        }
    }
}

That error occurs on string s = CheckEvenOdd(23); line. I test this code in Windows 10, Windows 7, Visual Studio 2019, Visual Studio 2010, and the debug profile on Visual Studio is on X86 CPU. This is DLL download link. This is Error Screenshot download link.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • 3
    I've serious concerns that `std::string` (C++) is compatible to `string` (C#). – Scheff's Cat Jan 25 '21 at 07:53
  • 2
    FYI: [SO: How to pass strings from C# to C++ (and from C++ to C#) using DLLImport?](https://stackoverflow.com/q/10799685/7478597) – Scheff's Cat Jan 25 '21 at 07:54
  • 1
    You can't handle `std::string` (or `str::vector`, or `std::**anything**`) from C#, fullstop. Pass around C types. – xanatos Jan 25 '21 at 08:02
  • I try to call a function that takes no argument but still the same error. – Hojat Sajadinia Jan 25 '21 at 08:04
  • I tried StringBuilder and Char* but still the same error. – Hojat Sajadinia Jan 25 '21 at 08:07
  • @xanatos Check top comments plz – Hojat Sajadinia Jan 25 '21 at 08:11
  • @HojatSajadinia Scheff has serious concerns. I have certainty about the argument. And in general you can't pass out something that is on the stack C++-side. Your std::string is on the stack at it will be freed before returning to C#. There is no copy-constructor between C# and C++ – xanatos Jan 25 '21 at 08:27
  • 1
    You certainly cannot use `std::string` in pinvoke. Some research will give you the possibilities open to you. Naturally this is a topic that has been considered before. A great number of times. – David Heffernan Jan 25 '21 at 09:17
  • If needed, I have a [github](https://github.com/xanatos/CSharpCPlusPlusInteropSamples) of examples of passing strings, structs and arrays between C/C++ and C# – xanatos Jan 25 '21 at 09:24
  • You could consider using c++/cli rather than pinvoke. It might be simpler to do the type conversions yourself. – JonasH Jan 25 '21 at 10:52

0 Answers0