C#调用C++写的dll如何获得结构体的指针

chen_hong_wei 2011-03-23 09:33:57
现在有两个结构体:
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct GroupProfile
{
/// Profile*
public IntPtr profile;

/// int
public int profileNum;
}

[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct tagProfile
{

/// char[512]
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 512)]
public string fileName;

/// char[512]
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 512)]
public string name;

/// int
public int langCount;

/// char*[10]
/// 不同语言的名字
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 10, ArraySubType = UnmanagedType.SysUInt)]
public System.IntPtr[] langName;

/// char*[10]
/// Profile 的显示名
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 10, ArraySubType = UnmanagedType.SysUInt)]
public System.IntPtr[] displayName;

/// char*[10]
/// 对Profile的描述
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 10, ArraySubType = UnmanagedType.SysUInt)]
public System.IntPtr[] desc;

/// char[128]
/// 输出格式
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 128)]
public string format;

/// char[128]
/// 输出格式的扩展名
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 128)]
public string extName;

/// int
/// 是否为系统定义
public int isSysProfile;

/// int
public int type;

/// int
/// 设备类型
public int device;

/// int
public int groupId;

/// char*[10]
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 10, ArraySubType = UnmanagedType.SysUInt)]
public System.IntPtr[] groupName;

/// ConfigItem*
public System.IntPtr configItems;

/// int
public int configItemsNum;
}

[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct tagConfigItem
{

/// char[512]
/// configItem的名字,也是唯一标示
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 512)]
public string name;

/// char[128]
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 128)]
public string group;

/// int
public int readyOnly;


/// int
/// configItem的控件类型
public int displayType;

/// int
public int valueType;

/// char*[10]
///
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 10, ArraySubType = UnmanagedType.SysUInt)]
public System.IntPtr[] label;

/// char*[10]
/// 对configItem的描述
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 10, ArraySubType = UnmanagedType.SysUInt)]
public System.IntPtr[] desc;

/// ListItem*
public System.IntPtr listItems;

/// int
public int listItemNum;

/// int
public int defaultListIndex;

/// char[512]
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 512)]
public string defaultValue;

/// Profile*
public System.IntPtr profile;
}

C++函数为DLL_DECLEARTYPE_PRASEXML int DLL_CALLTYPE praseXMLFolder(char *xmlFolder,GroupProfile *groupProfile);

这个函数的主要作用是,循环便利XmlFolder路径下的所有xml,我这里的文件下有187个xml目录,所以这个函数执行成功后,返回的groupProfile的profileNum是187,groupProfile的profile是个数组,存的是187个xml的结构体tabProfile(上面定义的)

我在C#中这样定义的函数
[DllImport("ttsProfileDll.dll", EntryPoint = "_praseXMLFolder@8", ExactSpelling = false, SetLastError = true)]
public static extern int praseXMLFolder(string xmlFolder, ref UtilityHelper.GroupProfile gprofile);

不知道我这样定义是否正确,,我现在照这种方法能取出一定的值
UtilityHelper.GroupProfile gProfile = new UtilityHelper.GroupProfile();
int s = XmlHelper.praseXMLFolder(@"..\..\profile", ref gProfile);
这样取出的profileNum是187,但是groupProfile的profile确只有一个,我像知道如何取得这个groupProfile的profile数组,也就是结构体数组的值,,,在网上找了很多的资料,都有点不是很明白,我没有做过指针方面的东西,希望各位高手指点指点。。。。
...全文
133 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
lycheeCX 2011-03-23
  • 打赏
  • 举报
回复
C++函数中的GroupProfile *groupProfile参数用双指针试试。。

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧