gpt4 book ai didi

c# - 如何将 C++ 非托管 DLL 转换为 C#

转载 作者:太空狗 更新时间:2023-10-30 01:12:36 24 4
gpt4 key购买 nike

我在 C++ 中有非托管 dll,它工作正常,我尝试用 C# 重新实现它,但出现以下错误:

System.ArgumentException: Value does not fall within the expected range

at System.StubHelpers.ObjectMarshaler.ConvertToNative(Object objSrc, IntPtr pDstVariant)  
at Demo.on_session_available(Int32 session_id) in C:\\Users\\Peyma\\Source\\Repos\\FastViewerDataClient\\FastViewerDataClient\\Demo.cs:line 69

ExceptionMethod: 8
ConvertToNative
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.StubHelpers.ObjectMarshaler
Void ConvertToNative(System.Object, IntPtr)

HResult:-2147024809

Source: mscorlib

C++代码如下:

typedef void(*func_ptr)(
int sId,
unsigned char cId,
const unsigned char* buf,
int len,
void* context);

struct configuration
{
func_ptr send;
};

struct send_operation
{
int session_id;
unsigned char channel_id;
std::string data;
};

auto op = new send_operation();
op->sId = sessionId;
op->cId = channelId;
op->data = "Some Text";

configuration.send(
sessionId,
channelId,
reinterpret_cast<const unsigned char*>(op->data.c_str()),
op->data.length(),
op);

然后在C#中翻译如下:

[StructLayout(LayoutKind.Sequential)]
public struct Configuration
{
public Send send { get; set; }
}

[StructLayout(LayoutKind.Sequential)]
public struct send_operation
{
public int session_id { get; set; }
public sbyte channel_id { get; set; }
public string data { get; set; }
};

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void Send(int sessionId, sbyte channelId, sbyte[] buffer, int len, object context);


var op = new send_operation
{
session_id = session_id,
channel_id = 0,
data = "This is a test message!!"
};

var bytes = Encoding.UTF8.GetBytes(op.data);

config.send(sessionId, 0, Array.ConvertAll(bytes, Convert.ToSByte), op.data.Length, op);

更新:

public static void on_session_available(int session_id)
{
WriteOnFile($"Open session id:{session_id}");

try
{
var op = new send_operation
{
session_id = session_id,
channel_id = 0,
data = "This is a test message!!"
};


var bytes = Encoding.UTF8.GetBytes(op.data);

config.send_data(session_id, op.channel_id, bytes, op.data.Length, op);
}
catch (Exception e)
{
WriteOnFile($"Error in sending data:{JsonConvert.SerializeObject(e)}");
if (e.InnerException != null)
{
WriteOnFile($"Error in inner sending data:{e.InnerException.Message}");
}
}
}

最佳答案

一些变化:

C++,std::stringunsigned char*,因为它很难在 C# 中编码。

struct send_operation
{
int session_id;
unsigned char channel_id;
unsigned char* data;
};

C#,object contextIntPtr context,因为send_operation 是一个struct,这将传递装箱对象而不是struct 数据。

public delegate void Send(int sessionId, sbyte channelId,
sbyte[] buffer, int len, IntPtr context);

如何通过:

IntPtr ptr = IntPtr.Zero;
try
{
ptr = Marshal.AllocHGlobal(Marshal.SizeOf(op));
Marshal.StructureToPtr(op, ptr, false);
config.send_data(session_id, op.channel_id, bytes, op.data.Length, ptr);
}
finally
{
Marshal.FreeHGlobal(ptr);
}

关于c# - 如何将 C++ 非托管 DLL 转换为 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57971238/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com