C++ DLL:
extern "c" bool GetRectRoundex(int libid, int id, int x,int y,PPOINT *pt0, PPOINT *pt1, PPOINT *pt2, PPOINT *pt3)
{
*pt0 = (PPOINT)CoTaskMemAlloc(POINT);
*pt1 = (PPOINT)CoTaskMemAlloc(POINT);
*pt2 = (PPOINT)CoTaskMemAlloc(POINT);
*pt3 = (PPOINT)CoTaskMemAlloc(POINT); POINT pt[4];
bool FLAG = GetRectRound(libid, id, x, y, pt)//另外DLL中的函数 通过前面的参数 取出 4个POINT
(*pt0)->x = pt[0].x;
(*pt0)->y = pt[0].y;
(*pt1)->x = pt[0].x;
(*pt1)->y = pt[0].y;
(*pt2)->x = pt[0].x;
(*pt2)->y = pt[0].y;
(*pt3)->x = pt[0].x;
(*pt3)->y = pt[0].y;
return FLAG;
}c#:
class Dll
{ [DllImport("GetRectRound.DLL"),CallingConvention := CallingConvention.Cdecl,CharSet:=CharSet.Auto]
protected internal static extern GetRectRoundex(int libid, int id, int x,int y,ref IntPtr pt0,ref IntPtr pt1, ref IntPtr pt2, ref IntPtr pt3);}调用
IntPtr ppt0, ppt1, ppt2, ppt3; Dll.GetRectRoundex(libid, id, x, y, ref ppt0, ref ppt1, ref ppt2, ref ppt3 ); POINT pt00 = Marshal.PtrToStructure(ppt0, typeof(POINT));
POINT pt11 = Marshal.PtrToStructure(ppt1, typeof(POINT));
POINT pt22 = Marshal.PtrToStructure(ppt2, typeof(POINT));
POINT pt33 = Marshal.PtrToStructure(ppt3, typeof(POINT));
Marshal.FreeCoTaskMem(ppt0)
Marshal.FreeCoTaskMem(ppt1);
Marshal.FreeCoTaskMem(ppt2);
Marshal.FreeCoTaskMem(ppt3);大部分 代码如上 方法就是用的书(《精通.NET互操作P/Invoke,C++Interop和COM Interop》)上的没什么区别 这段我是凭原来印象写的 问题是 传的第一个指针 始终为0值
将此句 POINT pt00 = Marshal.PtrToStructure(ppt0, typeof(POINT));注释成
POINT pt00;// = Marshal.PtrToStructure(ppt0, typeof(POINT));后 继续调试
可得到 ppt1, ppt2, ppt3 的值跟进DLL 看 ×pt0, ×pt1, ×pt2,×pt3 均有值 并且取到 点结构体的值 为
×pt0 8, 8
×pt1 42, 8
×pt2 42,42
×pt3 8, 43C# 中得到的点值为
pt00 0, 0
pt11 8, 8
pt22 42,8
pt33 42,42请问 该如何获取 到正确的 结构值?
extern "c" bool GetRectRoundex(int libid, int id, int x,int y,PPOINT *pt0, PPOINT *pt1, PPOINT *pt2, PPOINT *pt3)
{
*pt0 = (PPOINT)CoTaskMemAlloc(POINT);
*pt1 = (PPOINT)CoTaskMemAlloc(POINT);
*pt2 = (PPOINT)CoTaskMemAlloc(POINT);
*pt3 = (PPOINT)CoTaskMemAlloc(POINT); POINT pt[4];
bool FLAG = GetRectRound(libid, id, x, y, pt)//另外DLL中的函数 通过前面的参数 取出 4个POINT
(*pt0)->x = pt[0].x;
(*pt0)->y = pt[0].y;
(*pt1)->x = pt[0].x;
(*pt1)->y = pt[0].y;
(*pt2)->x = pt[0].x;
(*pt2)->y = pt[0].y;
(*pt3)->x = pt[0].x;
(*pt3)->y = pt[0].y;
return FLAG;
}c#:
class Dll
{ [DllImport("GetRectRound.DLL"),CallingConvention := CallingConvention.Cdecl,CharSet:=CharSet.Auto]
protected internal static extern GetRectRoundex(int libid, int id, int x,int y,ref IntPtr pt0,ref IntPtr pt1, ref IntPtr pt2, ref IntPtr pt3);}调用
IntPtr ppt0, ppt1, ppt2, ppt3; Dll.GetRectRoundex(libid, id, x, y, ref ppt0, ref ppt1, ref ppt2, ref ppt3 ); POINT pt00 = Marshal.PtrToStructure(ppt0, typeof(POINT));
POINT pt11 = Marshal.PtrToStructure(ppt1, typeof(POINT));
POINT pt22 = Marshal.PtrToStructure(ppt2, typeof(POINT));
POINT pt33 = Marshal.PtrToStructure(ppt3, typeof(POINT));
Marshal.FreeCoTaskMem(ppt0)
Marshal.FreeCoTaskMem(ppt1);
Marshal.FreeCoTaskMem(ppt2);
Marshal.FreeCoTaskMem(ppt3);大部分 代码如上 方法就是用的书(《精通.NET互操作P/Invoke,C++Interop和COM Interop》)上的没什么区别 这段我是凭原来印象写的 问题是 传的第一个指针 始终为0值
将此句 POINT pt00 = Marshal.PtrToStructure(ppt0, typeof(POINT));注释成
POINT pt00;// = Marshal.PtrToStructure(ppt0, typeof(POINT));后 继续调试
可得到 ppt1, ppt2, ppt3 的值跟进DLL 看 ×pt0, ×pt1, ×pt2,×pt3 均有值 并且取到 点结构体的值 为
×pt0 8, 8
×pt1 42, 8
×pt2 42,42
×pt3 8, 43C# 中得到的点值为
pt00 0, 0
pt11 8, 8
pt22 42,8
pt33 42,42请问 该如何获取 到正确的 结构值?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货