Why can't I return a char* string from C++ to

2019-03-09 11:08发布

问题:

I'm attempting to call the following trivial C function from C#:

SIMPLEDLL_API const char* ReturnString()
{
    return "Returning a static string!";
}

With the following P/Invoke declaration (with or without the return attribute, it makes no difference):

[DllImport("SimpleDll")]
[return: MarshalAs(UnmanagedType.LPStr)]
public static extern string ReturnString();

It works if the DLL is a Debug build but crashes in a Release build (AccessViolationException).

I am calling over a dozen other simple functions and this is the only one that fails (here are the others:)

[DllImport("SimpleDll")] public static extern int NextInt();
[DllImport("SimpleDll")] public static extern void SetNextInt(int x);
[DllImport("SimpleDll")] public static extern int AddInts(int a, int b);
[DllImport("SimpleDll")] public static extern int AddFourInts(int a, int b, int c, int d);
[DllImport("SimpleDll")] public static extern double AddDoubles(double x, double y);
[DllImport("SimpleDll")] public static extern IntPtr AddDoublesIndirect(ref double x, ref double y);
[DllImport("SimpleDll")] [return: MarshalAs(UnmanagedType.U1)]
public static extern char CharStringArgument([MarshalAs(UnmanagedType.LPStr)]string s);
[DllImport("SimpleDll")] [return: MarshalAs(UnmanagedType.U2)]
public static extern char WCharStringArgument([MarshalAs(UnmanagedType.LPWStr)]string s);
[DllImport("SimpleDll")] [return: MarshalAs(UnmanagedType.LPWStr)]
public static extern string ReturnWString();
[DllImport("SimpleDll")] [return: MarshalAs(UnmanagedType.BStr)]
public static extern string ReturnBSTR();
[DllImport("SimpleDll")] public static extern System.Drawing.Point MakePoint(int x, int y);
[DllImport("SimpleDll")] public static extern IntPtr MakePointIndirect(int x, int y);
[DllImport("SimpleDll")] public static extern int GetPointY(System.Drawing.Point p);
[DllImport("SimpleDll")] public static extern int GetPointYIndirect(ref System.Drawing.Point pp);
[DllImport("SimpleDll")] public static extern int SumIntegers(ref int firstElem, int size);

回答1:

Or maybe try to use

[DllImport("SimpleDll")]
public static extern IntPtr ReturnString();

and in your calling code, use the Marshal Class

string ret = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(PInvoke.ReturnString());


回答2:

It's also possible to do that with a custom Marshaler:

class ConstCharPtrMarshaler : ICustomMarshaler
{
    public object MarshalNativeToManaged(IntPtr pNativeData)
    {
        return Marshal.PtrToStringAnsi(pNativeData);
    }

    public IntPtr MarshalManagedToNative(object ManagedObj)
    {
        return IntPtr.Zero;
    }

    public void CleanUpNativeData(IntPtr pNativeData)
    {
    }

    public void CleanUpManagedData(object ManagedObj)
    {
    }

    public int GetNativeDataSize()
    {
        return IntPtr.Size;
    }

    static readonly ConstCharPtrMarshaler instance = new ConstCharPtrMarshaler();

    public static ICustomMarshaler GetInstance(string cookie)
    {
        return instance;
    }
}

And use it like this:

[DllImport("SimpleDll")]
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(ConstCharPtrMarshaler))]
public static extern string ReturnString();


回答3:

The C++ compiler in Release mode is putting the constant into the data page, which is protected; attempting to hand this off to C# is causing problems. In Debug mode, the compiler isn't optimizing the constant into the data page, and so there's no protection problem.



回答4:

In my P/Invoke experience, you usually have 2 parameters: 1. a pre-allocated buffer in, and 2, the length of the buffer. The Return value is the length of data returned (not to exceed the original length).

Usually the DEBUG releases won't move memory around as much.

BTW, You can pass in a pre-allocated StringBuilder, then set sb.Lenght = the return value of the C function, then you won't have a bunch of \0 nulls at the end of your string.



回答5:

I ran into a similar problem and specifying the "CharSet" seemed to fix it.

[DllImport("SimpleDll", CharSet = CharSet.Ansi)]

Not sure why this would be different in debug and release.