首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Propvariant to variant C#

Propvariant to variant C#
EN

Stack Overflow用户
提问于 2012-10-19 23:46:13
回答 2查看 2.6K关注 0票数 1

我是这方面的新手。我正在尝试在C#中将propvariant转换为variant

我有一个这样的代码。

代码语言:javascript
复制
      public static object PropValueToVariant(PROPVARIANT Value)
    {
        object result;
        DateTime stTime;
        DateTime ftTime;
        object __Null;
        // Convert the prop variant to variant data type

        switch(Value.vt)
        {
            //@ Undeclared identifier(3): 'VT_I1'
            case VT_I1:
                //@ Unsupported property or method(C): 'bVal'
                result = Value.bVal;
                break;
            //@ Undeclared identifier(3): 'VT_UI1'
            case VT_UI1:
                //@ Unsupported property or method(C): 'bVal'
                result = Value.bVal;
                break;
            //@ Undeclared identifier(3): 'VT_I2'
            case VT_I2:
                //@ Unsupported property or method(C): 'iVal'
                result = Value.iVal;
                break;
            //@ Undeclared identifier(3): 'VT_UI2'
            case VT_UI2:
                //@ Unsupported property or method(C): 'uiVal'
                result = Value.uiVal;
                break;
            //@ Undeclared identifier(3): 'VT_I4'
            case VT_I4:
                //@ Unsupported property or method(C): 'lVal'
                result = Value.lVal;
                break;
            //@ Undeclared identifier(3): 'VT_UI4'
            case VT_UI4:
                //@ Unsupported property or method(C): 'ulVal'
                result = ((int)Value.ulVal);
                break;
            //@ Undeclared identifier(3): 'VT_I8'
            case VT_I8:
                //@ Unsupported property or method(C): 'hVal'
                //@ Unsupported property or method(D): 'LowPart'
                result = ((int)Value.hVal.LowPart);
                break;
            //@ Undeclared identifier(3): 'VT_UI8'
            case VT_UI8:
                //@ Unsupported property or method(C): 'uhVal'
                //@ Unsupported property or method(D): 'LowPart'
                result = ((int)Value.uhVal.LowPart);
                break;
            //@ Undeclared identifier(3): 'VT_BOOL'
            case VT_BOOL:
                //@ Unsupported property or method(C): 'boolVal'
                result = Value.boolVal;
                break;
            //@ Undeclared identifier(3): 'VT_R4'
            case VT_R4:
                //@ Unsupported property or method(C): 'fltVal'
                result = Value.fltVal;
                break;
            //@ Undeclared identifier(3): 'VT_R8'
            case VT_R8:
                //@ Unsupported property or method(C): 'dblVal'
                result = Value.dblVal;
                break;
            //@ Undeclared identifier(3): 'VT_CY'
            case VT_CY:
                //@ Unsupported property or method(C): 'cyVal'
                result = Value.cyVal;
                break;
            //@ Undeclared identifier(3): 'VT_ERROR'
            case VT_ERROR:
                //@ Unsupported property or method(C): 'scode'
                result = Value.scode;
                break;
            //@ Undeclared identifier(3): 'VT_DATE'
            case VT_DATE:
                //@ Unsupported property or method(C): 'date'
                result = Value.date;
                break;
            //@ Undeclared identifier(3): 'VT_FILETIME'
            case VT_FILETIME:
                //@ Unsupported property or method(C): 'filetime'
                //@ Unsupported function or procedure: 'FileTimeToLocalFileTime'
                FileTimeToLocalFileTime(Value.filetime, ftTime);
                //@ Undeclared identifier(3): 'FileTimeToSystemTime'
                FileTimeToSystemTime(ftTime, stTime);
                result = stTime;
                break;
            //@ Undeclared identifier(3): 'VT_CLSID'
            case VT_CLSID:
                //@ Unsupported property or method(C): 'puuid'
                result = Value.puuid.ToString();
                break;
            //@ Undeclared identifier(3): 'VT_BSTR'
            case VT_BSTR:
                //@ Unsupported property or method(C): 'bstrVal'
                result = Value.bstrVal;
                break;
            //@ Undeclared identifier(3): 'VT_LPSTR'
            case VT_LPSTR:
                //@ Unsupported property or method(C): 'pszVal'
                result = (Value.pszVal as string);
                break;
            //@ Undeclared identifier(3): 'VT_LPWSTR'
            case VT_LPWSTR:

                result = WideCharToString(Value.pwszVal);
                break;
            default:
                result = __Null;
                break;
        }
        return result;
    }

为什么我会得到案例名称的错误?此外,它不理解ulval、lval等。

非常感谢你的帮助

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-20 00:08:40

您不需要这个函数,下面是已经在.net框架中实现的函数:http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.getobjectfornativevariant.aspx

票数 -1
EN

Stack Overflow用户

发布于 2016-09-15 22:30:48

您应该使用Marshal类和几个P/Invoke调用:

Invoke

  • 分配16字节数组(例如,通过AllocCoTaskMem) (编辑:通过对VariantInit

  • Then的P/Invoke调用将其设为24字节,通过P/Invoke调用PropVariantClear

  • Another

  • 来填充这个缓冲区,你现在可以通过Marshal.GetObjectForNativeVariant

  • And获取你的对象不要忘了清理:
  • VariantClear
  • And的P/Invoke调用,然后用Marshal.FreeCoTaskMem.

把非托管内存还回来

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12977843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档