Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
封送一个C#结构数组并在COM中使用它正确的封送属性是什么_C#_.net_Com_Interop_Marshalling - Fatal编程技术网

封送一个C#结构数组并在COM中使用它正确的封送属性是什么

封送一个C#结构数组并在COM中使用它正确的封送属性是什么,c#,.net,com,interop,marshalling,C#,.net,Com,Interop,Marshalling,我几乎一周都在面临以下问题:我有一个c#结构数组,需要发送到COM应用程序。但是当我调用COM方法时,我得到了以下结果 错误:System.Runtime.InteropServices.MarshalDirectiveException' 发生 其他信息:无法封送“参数#5”:无效 托管/非托管类型组合(Int/UInt必须与 SysInt或SysUInt) IDL文件通过C#接口进行扩展/派生 以下是IDL中定义的方法: [helpstring("Method MyCallbackMehto

我几乎一周都在面临以下问题:我有一个c#结构数组,需要发送到COM应用程序。但是当我调用COM方法时,我得到了以下结果

错误:System.Runtime.InteropServices.MarshalDirectiveException' 发生

其他信息:无法封送“参数#5”:无效 托管/非托管类型组合(Int/UInt必须与 SysInt或SysUInt)

IDL文件通过C#接口进行扩展/派生

以下是IDL中定义的方法:

[helpstring("Method MyCallbackMehtod")]
    HRESULT MyRequestFinished(
        [in] long    callId,
        [in] unsigned int nrElemArray1,
        [in, size_is(nrElemArray1)] MyStruct ElemArray1[],
        [in] unsigned int nrElemArray2,
        [in, size_is(nrElemArray2)] MyStruct ElemArray2[]
);
c#接口:

 [ComImport, Guid("xxxxxxxx-xxxx-xxxx-xxx-xxxxxxxxxx")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IMyInterface
    {

        void MyRequestFinished(
            [In] 
            long callId,

            [In] 
            uint nrElemArray1,

            [In,MarshalAs(UnmanagedType.LPArray,SizeParamIndex=1)] 
            IntPtr ElemArray1,

            [In]
            uint nrElemArray2,

            [In, MarshalAs(UnmanagedType.LPArray,SizeParamIndex = 3)]  
            IntPtr ElemArray2);
}
MyStruct的定义是:

[StructLayout(LayoutKind.Sequential,Pack=1)]
    public struct MyStruct
    {
        public double setValue;
        public double actualValue;
        [MarshalAs(UnmanagedType.I4)]
        public MyEnum myResult;
    }

    [ComVisible(true)]
    public enum MyEnum 
    {
        Val1,

        Val2,

        Val3
    }
调用COM方法的代码段:

IntPtr pMyElemArray1  = IntPtr.Zero;
IntPtr pMyElemArray2 = IntPtr.Zero;

MyStruct[] MyElemArray1= GetArray1();
MyStruct[] MyElemArray2= GetArray2();

int lengthElemArray1= MyElemArray1.Length;
int lengthElemArray2= MyElemArray2.Length;

pMyElemArray1 = Marshal.AllocCoTaskMem(Marshal.SizeOf(MyElemArray1[0]) * lengthElemArray1);
pMyElem2 = Marshal.AllocCoTaskMem(Marshal.SizeOf(MyElemArray2[0]) * lengthElemArray2);

int rundef = (int)pMyElemArray1 ;
for (int i = 0; i < lengthElemArray1; i++)
{
     Marshal.StructureToPtr(MyElemArray1 [i], (IntPtr)rundef, false);
     rundef += Marshal.SizeOf(MyElemArray1[i]);
}

rundef = (int)pMyElemArray2;
for (int i = 0; i < lengthElemArray2; i++)
{
    Marshal.StructureToPtr(MyElemArray2[i], (IntPtr)rundef, false);
    rundef += Marshal.SizeOf(MyElemArray2[i]);
}

// Notify COM component
//here i get the error
  myComObject.MyRequestFinished(callId,
                       ((uint)lengthElemArray1),
                       pMyElemArray1,
                       ((uint)lengthElemArray2),
                       pMyElemArray2);
//....
}
IntPtr pMyElemArray1=IntPtr.Zero;
IntPtr pMyElemArray2=IntPtr.Zero;
MyStruct[]MyElemArray1=GetArray1();
MyStruct[]MyElemArray2=GetArray2();
int lengthElemArray1=Mylemarray1.长度;
int lengthElemArray2=MyElemArray2.长度;
pMyElemArray1=Marshal.alloctaskmem(Marshal.SizeOf(MyElemArray1[0])*lengthelarray1);
pMyElem2=Marshal.alloctaskmem(Marshal.SizeOf(MyElemArray2[0])*lengthelarray2);
int rundef=(int)pMyElemArray1;
对于(int i=0;i
LPArray
不适用于
IntPtr
类型的参数。尝试将参数类型更改为数组或删除
marshallas
属性

void MyRequestFinished(
    [In]
    long callId,

    [In]
    uint nrElemArray1,

    [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]
    MyStruct[] ElemArray1,

    [In]
    uint nrElemArray2,

    [In]  
    IntPtr ElemArray2);
}
就我个人而言,我更喜欢传入数组而不是IntPtr,因为它会在调用站点产生更简单的逻辑