您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。
- 内容介绍
- 文章标签
- 相关推荐
本文共计300个文字,预计阅读时间需要2分钟。
原创新意,创意无限,灵感迸发。以下是对伪原创以下开头内容的简短
创意火花,点燃新篇章,思维碰撞。
内容
C#是无法直接接收C++的vector的,需要在C++中将vector转换成对应类型的指针数组,再将指针数组传递到C#中
c++
extern "C" __declspec(dllexport) double* __stdcall ArrTest();
double* __stdcall ArrTest()
{
vector vec({ 6,2,3,4,5 });
double* output = new double[vec.size()];
memcpy(output, &vec[0], vec.size() * sizeof(double));
return output;
}
c#
namespace test
{
class Program
{
[DllImport("libDemo", EntryPoint = "ArrTest")]
public static extern IntPtr ArrTest();
static void Main(string[] args)
{
IntPtr ptr = ArrTest();
long a = ptr.ToInt64();
List<double> results = new List<double>();
for (int i = 0; i < 5; i++)
{
results.Add((double)Marshal.PtrToStructure((IntPtr)((long)(a + i * Marshal.SizeOf(typeof(double)))), typeof(double)));
}
foreach (var item in results)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
注意:
results.Add((double)Marshal.PtrToStructure((IntPtr)((long)(a + i * Marshal.SizeOf(typeof(double)))), typeof(double)));
(a + i * Marshal.SizeOf(typeof(double)))意为首地址位置加上基本类型的字节长度偏移。
例如首地址为0,第一个数据位置就是0,第二个数据位置应该为0+double的长度为8,直接用typeof获取长度。
本文共计300个文字,预计阅读时间需要2分钟。
原创新意,创意无限,灵感迸发。以下是对伪原创以下开头内容的简短
创意火花,点燃新篇章,思维碰撞。
内容
C#是无法直接接收C++的vector的,需要在C++中将vector转换成对应类型的指针数组,再将指针数组传递到C#中
c++
extern "C" __declspec(dllexport) double* __stdcall ArrTest();
double* __stdcall ArrTest()
{
vector vec({ 6,2,3,4,5 });
double* output = new double[vec.size()];
memcpy(output, &vec[0], vec.size() * sizeof(double));
return output;
}
c#
namespace test
{
class Program
{
[DllImport("libDemo", EntryPoint = "ArrTest")]
public static extern IntPtr ArrTest();
static void Main(string[] args)
{
IntPtr ptr = ArrTest();
long a = ptr.ToInt64();
List<double> results = new List<double>();
for (int i = 0; i < 5; i++)
{
results.Add((double)Marshal.PtrToStructure((IntPtr)((long)(a + i * Marshal.SizeOf(typeof(double)))), typeof(double)));
}
foreach (var item in results)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
注意:
results.Add((double)Marshal.PtrToStructure((IntPtr)((long)(a + i * Marshal.SizeOf(typeof(double)))), typeof(double)));
(a + i * Marshal.SizeOf(typeof(double)))意为首地址位置加上基本类型的字节长度偏移。
例如首地址为0,第一个数据位置就是0,第二个数据位置应该为0+double的长度为8,直接用typeof获取长度。

