【问题标题】:How to define and marshall BSTR* types如何定义和编组 BSTR* 类型
【发布时间】:2012-08-11 11:45:56
【问题描述】:

我想知道如何将字符串类型编组为 BSTR* 类型。 详细说明一下,C++结构如下:

struct MyStruct
{
     BSTR* string;
     int a;
}

我需要在 C# 端定义一个新结构,我尝试了 string[] 和 IntPtr[] 但没有成功。

感谢您的帮助!

编辑: c++结构:

    struct HTTPTEXTRENDERERFILTERINFO {
        enum { UTC, GMT } eTimestamp;
        BOOL    bEnableCCIngest;
        LONG    lQueueSizeTreshold; 
        LONG    lSendTimeTreshold;  
        BYTE    btBroadcastUrlCount;
        BSTR*   pbstrBroadcastUrls;                                
        LONG    lInputPinsNum;      
    };

以及函数的原型:

HRESULT HTTPTextRendererFilter::SetConfig(IN const HTTPTEXTRENDERERFILTERINFO& Config)

c#结构:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 公共结构 HttpTextRenderFilterInfo { 公共枚举 TimestampType : int { UTC, GMT };

    public TimestampType Timestamp;
    public int EnableCCIngest;
    public int QueueSizeTreshold; 
    public int SendTimeTreshold;  
    public byte BroadcastUrlCount;
    [MarshalAs(UnmanagedType.BStr)]
    public string pbstrBroadcastUrls;        
    public int lInputPinsNum;     
};

原型:

    [PreserveSig]
    int SetConfig([In] ref HttpTextRenderFilterInfo config);

【问题讨论】:

    标签: c# c++ marshalling com-interop unmarshalling


    【解决方案1】:

    试试这个:

    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
    struct MyStruct
    {
       [MarshalAs(UnmanagedType.BStr)] public String myString;
       public int a;
    }
    

    【讨论】:

    • 感谢您的回复,在为字符串分配新值时,我应该做些什么不同的事情? Marshal.stringToPtr 等等..?
    • 从c#转发到c++端的整个结构都损坏了,没有有意义的值,我将编辑我的原始帖子以显示案例
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    • 1970-01-01
    • 2011-01-18
    相关资源
    最近更新 更多