由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Marshal C++ struct to C# struct
相关主题
C++一个string的小问题这段C++代码有啥问题
C++里面 class的数据成员的顺序是什么样的?question about structure initializationa and reference
What does the default constructor do?请教boost::any compile错误。
给几个teacherwei代码的评审意见吧effective C++里的memory pool 一问:
typedef basic_string string;why do we still use dynamic allocation?
map析构thread or process
C++ Segment Fault问个超简单的C问题
C++ | A memory allocation question关于内存泄漏
相关话题的讨论汇总
话题: struct话题: c#话题: char话题: c++话题: string
进入Programming版参与讨论
1 (共1页)
s*****w
发帖数: 215
1
最近在写一个C#调用C++ 的dll的东西
遇到这样一个struct
struct A
{
unsigned int numConnections;
char** connectionName;
char** connectionDesc;
}
请问在C#里面的对应的struct怎么写呢
s*****w
发帖数: 215
2
或者比较弱的问一下C++中
这里为什么用char**
这和string有什么区别?

【在 s*****w 的大作中提到】
: 最近在写一个C#调用C++ 的dll的东西
: 遇到这样一个struct
: struct A
: {
: unsigned int numConnections;
: char** connectionName;
: char** connectionDesc;
: }
: 请问在C#里面的对应的struct怎么写呢

t****t
发帖数: 6806
3
you should ask the original author why he used char**.
usually string is declared as char*, or const char*.
you can consider char** as pointer to string, whether that makes sense to
you is beyond my knowledge. but pointer to string is a completely different
creature from string.

【在 s*****w 的大作中提到】
: 或者比较弱的问一下C++中
: 这里为什么用char**
: 这和string有什么区别?

d****n
发帖数: 1637
4
let me try to answer you this.
This would be like, in your example, a struct manage multiple "Connections."
Struct A a;
a.numberConnections=1;
a.ConnectionsName[0]="CName1"; //mem allocation and str copy ommited
a.ConnecttionDesc[0]="first Connections";
When you have more than one connections.
then ;
a.numberConnections++;
a.Connectionsnames[a.numberConnections-1] ="secondName";
a.ConnectionDesc[a.numberConnections-1]="Second Description here";
So char ** here is for maintain multiple c-string.
You treat it as a 2-D string.
but , be careful memory allocations in each string is different size(depends
on the size of each string.)
how it works in C#, I dont know. anyone?

【在 s*****w 的大作中提到】
: 或者比较弱的问一下C++中
: 这里为什么用char**
: 这和string有什么区别?

o****e
发帖数: 916
5
here is an example:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct MyStruct
{
public static readonly int SizeOf = Marshal.SizeOf(typeof(MyStruct));
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr,
SizeConst = 256)]
public string name;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.I4)]
public int value;
}
N********n
发帖数: 8363
6

Make sure no C++ exceptions ever get thrown into the C# runtime, or
your app could mysteriously crash. One problem of mixed-mode apps.

【在 s*****w 的大作中提到】
: 最近在写一个C#调用C++ 的dll的东西
: 遇到这样一个struct
: struct A
: {
: unsigned int numConnections;
: char** connectionName;
: char** connectionDesc;
: }
: 请问在C#里面的对应的struct怎么写呢

t*****n
发帖数: 4908
7
char**是指向char*指针。

【在 s*****w 的大作中提到】
: 或者比较弱的问一下C++中
: 这里为什么用char**
: 这和string有什么区别?

1 (共1页)
进入Programming版参与讨论
相关主题
关于内存泄漏typedef basic_string string;
请问可以这样定义struct吗?map析构
[合集] more interview questionsC++ Segment Fault
What problem can occur when dynamically allocated classesC++ | A memory allocation question
C++一个string的小问题这段C++代码有啥问题
C++里面 class的数据成员的顺序是什么样的?question about structure initializationa and reference
What does the default constructor do?请教boost::any compile错误。
给几个teacherwei代码的评审意见吧effective C++里的memory pool 一问:
相关话题的讨论汇总
话题: struct话题: c#话题: char话题: c++话题: string