2012-10-19 90 views
1

我是這個新手。我試圖將propvariant轉換爲C#中的變體#支持變體C#

我有這樣的代碼。

 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中,等....

非常感謝你的幫助

+2

這聽起來像「這是幹什麼?」。在問這裏之前,先查看如何使用目標語言。 – Azulflame

+0

我正致力於將delphi代碼轉換爲C#,並自動將工具轉換爲此工具。我從來沒有在C#之前工作過 –

+0

「案例名稱錯誤」,你究竟是什麼意思? – user287107

回答

0

您應該使用Marshal類和一對夫婦的P/Invoke的呼籲:

  • 分配一個16字節數組(通過,比如,AllocCoTaskMem)(編輯:中號AKE即24個字節爲64位進程)
  • 零出來有P /調用調用VariantInit
  • 然後執行實際的轉換,與P /調用調用PropVariantToVariant來填補這個緩衝器具有變體數據
  • 您現在可以通過Marshal.GetObjectForNativeVariant
  • 讓你的對象,並且不要忘了清理:
    • AP/Invoke的調用PropVariantClear
    • 另一個以VariantClear
    • 然後用Marshal.FreeCoTaskMem返回非託管內存。