传递给系统调用的数据区域太小
C#调用 C DLL文件是报这个错:汉字: 传递给系统调用的数据区域太小。 (异常来自 HRESULT:0x8007007A)
其他类型(数字、字母)的不报错,一切正常!
就是汉字报此错!!
--------------------编程问答-------------------- 顶~~~~~~~~~~~~~~~~ --------------------编程问答-------------------- 再顶!!!! --------------------编程问答-------------------- 找到问题了;
问题在于:结构声明
调用 DLL 时,声明结构一定要注意,如果参数中有"字符串”传进来,或输出的话;
一定要使用 Unicode 而不能采用 Ansi
正确 :
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
--------------------编程问答-------------------- 标记 --------------------编程问答-------------------- 顶~~~帮了我的忙~~~ --------------------编程问答-------------------- 楼主,我现在有个结构体 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)]
//[StructLayout(LayoutKind.Sequential)]
public struct CAM_GROUP_INFO
{
public int nCamGroupID; //camera group id
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
public char[] szGroupName; //camera group name
public int nServerID; //belong to which server
public int state; //MP_CAM_StateXXXX
};
对应有两个方法, //从服务器得到视频组列表
[DllImport(@"E:\dll\playback_dll\debug\playback_dll.dll", EntryPoint = "CCameraMngr_RetrieveCamGroupListFromServer", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int CCameraMngr_RetrieveCamGroupListFromServer([In, Out] int Self, int nServerID, [In, Out] CAM_GROUP_INFO[] pCamGroupList, int iMaxSize);
//释放从服务器得到的视频组列表
[DllImport(@"E:\dll\playback_dll\debug\playback_dll.dll", EntryPoint = "CCameraMngr_FreeCamGroupList", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
public static extern void CCameraMngr_FreeCamGroupList([In, Out] int Self, [In, Out] CAM_GROUP_INFO[] pCamGroupList);
像你说的 如果我用CharSet = CharSet.Unicode在c#中像上面这样申明结构体,则释放不会出错,但是获取CCameraMngr_RetrieveCamGroupListFromServer这个函数 得到的结构体的数据就不对,里面只有nCamGroupID是正确的 其他都是0,字符类型的是乱码,如果改成ansi申明结构体,则得到数据都是正确的,但释放那个函数会报 “传递给系统调用的数据区域太小。 (异常来自 HRESULT:0x8007007A) ”错误,请求大虾帮忙!!
--------------------编程问答-------------------- c中结构体定义为
typedef struct _CAM_GROUP_INFO
{
int nCamGroupID; //camera group id
char szGroupName[64]; //camera group name
int nServerID; //belong to which server
int state; //MP_CAM_StateXXXX
} CAM_GROUP_INFO;
补充:.NET技术 , C#