将对象传递给COM方法

时间:2014-01-13 23:10:11

标签: c# com

我有一个api用于我正在使用的第三方应用程序。在api中,他们说需要传入“VARIANTARG”类型的输入参数。

所以我在网上找到了这个,因为在C#中使用VARIANTARG: http://www.pinvoke.net/default.aspx/Structures/VARIANTARG.html

这是我的问题。这个VARIANTARG对象有两个属性,我不知道如何设置:

public unsafe ushort* bstrVal;
public unsafe ushort** pbstrVal; // Note the double pointer on this one

以下是我的尝试:

VARIANTARG v = new VARIANTARG();

//---------------------------------
//??? What goes here??? ???
string myString = "aaa";
v.bstrVal = &myString;
//  public unsafe ushort* bstrVal;
//??? and here:
v.pbstrVal = "???";
//  public unsafe ushort** pbstrVal;
//---------------------------------

MyCOMObj_App.SendTag(v);

你能告诉我我应该如何将这两个属性设置为某个字符串值,以及如何将其传递给COM方法?

仅供参考....这里是VARIANTARG结构定义:

StructLayout(LayoutKind.Explicit, Size=16)]
struct VARIANTARG {
    [FieldOffset(0)]
    public ushort vt;

    [FieldOffset(2)]
    public ushort wReserved1;

    [FieldOffset(4)]
    public ushort wReserved2;

    [FieldOffset(6)]
    public ushort wReserved3;

    [FieldOffset(8)]
    public long llVal;

    [FieldOffset(8)]
    public int lVal;

    [FieldOffset(8)]
    public byte bVal;

    [FieldOffset(8)]
    public short iVal;

    [FieldOffset(8)]
    public float fltVal;

    [FieldOffset(8)]
    public double dblVal;

    [FieldOffset(8)]
    public short boolVal;

    [FieldOffset(8)]
    public int scode;

    //[FieldOffset(8)]
    //public CY cyVal;

    [FieldOffset(8)]
    public double date;

    [FieldOffset(8)]
    public unsafe ushort* bstrVal;

    //[FieldOffset(8)]
    //public unsafe IUnknown* punkVal;

    //[FieldOffset(8)]
    //public unsafe IDispatch* pdispVal;

    //[FieldOffset(8)]
    //public unsafe SAFEARRAY* parray;

    [FieldOffset(8)]
    public unsafe byte* pbVal;

    [FieldOffset(8)]
    public unsafe short* piVal;

    [FieldOffset(8)]
    public unsafe int* plVal;

    [FieldOffset(8)]
    public unsafe long* pllVal;

    [FieldOffset(8)]
    public unsafe float* pfltVal;

    [FieldOffset(8)]
    public unsafe double* pdblVal;

    [FieldOffset(8)]
    public unsafe short* pboolVal;

    [FieldOffset(8)]
    public unsafe int* pscode;

    //[FieldOffset(8)]
    //public unsafe CY* pcyVal;

    [FieldOffset(8)]
    public unsafe double* pdate;

    [FieldOffset(8)]
    public unsafe ushort** pbstrVal;

    //[FieldOffset(8)]
    //public unsafe IUnknown** ppunkVal;

    //[FieldOffset(8)]
    //public unsafe IDispatch** ppdispVal;

    //[FieldOffset(8)]
    //public unsafe SAFEARRAY** pparray;

    [FieldOffset(8)]
    public unsafe VARIANTARG* pvarVal;

    [FieldOffset(8)]
    public unsafe void* byref;

    [FieldOffset(8)]
    public sbyte cVal;

    [FieldOffset(8)]
    public ushort uiVal;

    [FieldOffset(8)]
    public uint ulVal;

    [FieldOffset(8)]
    public ulong ullVal;

    [FieldOffset(8)]
    public int intVal;

    [FieldOffset(8)]
    public uint uintVal;

    //[FieldOffset(8)]
    //public unsafe DECIMAL* pdecVal;

    [FieldOffset(8)]
    public unsafe sbyte* pcVal;

    [FieldOffset(8)]
    public unsafe ushort* puiVal;

    [FieldOffset(8)]
    public unsafe uint* pulVal;

    [FieldOffset(8)]
    public unsafe ulong* pullVal;

    [FieldOffset(8)]
    public unsafe int* pintVal;

    [FieldOffset(8)]
    public unsafe uint* puintVal;

    [FieldOffset(8)]
    public unsafe void* pvRecord;

    //[FieldOffset(12)]
    //public unsafe IRecordInfo* pRecInfo;

    //[FieldOffset(0)]
    //public DECIMAL decVal;
}

1 个答案:

答案 0 :(得分:0)

该结构代表一个Variant,它在这里实现为C ++联合。有很多字段重叠,例如bool,float,long等。您不需要设置所有字段,只需要设置一个字段和类型。 例如,请参阅:http://msdn.microsoft.com/en-us/library/ms891678.aspx 哪一个取决于你打电话的功能;它应该告诉你它期望的输入类型。