在发布版本为什么我不能返回一个char *字符串从C ++到C#? [英] Why can't I return a char* string from C++ to C# in a Release build?

查看:250
本文介绍了在发布版本为什么我不能返回一个char *字符串从C ++到C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用从C#以下琐碎的C函数:

  SIMPLEDLL_API为const char * ReturnString()
{
    返回返回一个静态字符串!;
}
 

通过后的P / Invoke的声明(有或没有回报的属性,它使没有区别):

  [的DllImport(SimpleDll)
[返回:的MarshalAs(UnmanagedType.LPStr)
公共静态外部串ReturnString();
 

它的工作原理,如果DLL是一个调试版本,但崩溃的发布版本(AccessViolationException)。

我打电话了十几个等简单功能,这是唯一一个出现故障(这里的其他人:)

  [的DllImport(SimpleDll)]公共静态外部INT NextInt();
[的DllImport(SimpleDll)]公共静态外部无效SetNextInt(INT X);
[的DllImport(SimpleDll)]公共静态外部INT AddInts(INT A,INT B);
[的DllImport(SimpleDll)]公共静态外部INT AddFourInts(INT A,INT B,INT C,INT D);
[的DllImport(SimpleDll)]公共静态外部双AddDoubles(双X,双Y);
[的DllImport(SimpleDll)]公共静态外部的IntPtr AddDoublesIndirect(REF双X,裁判双Y);
[的DllImport(SimpleDll)] [返回:的MarshalAs(UnmanagedType.U1)
公共静态外部字符CharStringArgument([的MarshalAs(UnmanagedType.LPStr)字符串s);
[的DllImport(SimpleDll)] [返回:的MarshalAs(UnmanagedType.U2)
公共静态外部字符WCharStringArgument([的MarshalAs(UnmanagedType.LPWStr)字符串s);
[的DllImport(SimpleDll)] [返回:的MarshalAs(UnmanagedType.LPWStr)
公共静态外部串ReturnWString();
[的DllImport(SimpleDll)] [返回:的MarshalAs(UnmanagedType.BStr)
公共静态外部串ReturnBSTR();
[的DllImport(SimpleDll)]公共静态外部System.Drawing.Point MakePoint(INT X,int y)对;
[的DllImport(SimpleDll)]公共静态外部的IntPtr MakePointIndirect(INT X,int y)对;
[的DllImport(SimpleDll)]公共静态外部INT GetPointY(System.Drawing.Point P);
[的DllImport(SimpleDll)]公共静态外部INT GetPointYIndirect(REF System.Drawing.Point PP);
[的DllImport(SimpleDll)]公共静态外部INT SumIntegers(REF INT firstElem,诠释大小);
 

解决方案

或者,也许尝试使用

  [的DllImport(SimpleDll)
公共静态外部的IntPtr ReturnString();
 

和您的通话code,使用Marshal类

 字符串RET = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(PInvoke.ReturnString());
 

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);

解决方案

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());

这篇关于在发布版本为什么我不能返回一个char *字符串从C ++到C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆