php
  • .net
  • com
  • 2010-09-10 60 views 4 likes 
    4

    我想在PHP中使用Variants構建一個字節數組。但是,我似乎無法使其工作。下面是一個示例代碼:使用變體在PHP中構建一個字節數組?

    $ie = new COM("InternetExplorer.Application"); 
    
    $ie->Visible = true; 
    $ie->Height = 500 ; 
    $ie->Width  = 700 ; 
    
    $post = array (ord('p'),ord('='),ord('1')) ; 
    $v = new VARIANT($post, VT_ARRAY|VT_UI1); 
    
    $ie->Navigate2("http://host/web/echo_request.php",0,'',$v) ; 
    

    的代碼產生一個錯誤:

    Fatal error: Uncaught exception 'com_exception' with message 'Variant type conversion failed: Type mismatch.type conversion failed: Type mismatch.

    我已經用各種的變異類型組合的嘗試從http://fi2.php.net/manual/en/com.constants.php

    任何幫助,不勝感激!

    回答

    2

    (使用PHP 5.3.2)
    難道不是VT_ARRAY? (或空型)

    $post = array (ord('p'),ord('='),ord('1')); 
    $v = new VARIANT($post, VT_ARRAY); 
    print variant_get_type($v); 
    

    (注:也是如此離去VT_ARRAY出它即)

    $v = new VARIANT($post); 
    

    打印出8024 8024 - 8192 = 12 12 = VT_VARIANT

    或者是我在這裏錯過了什麼?

    如果你想使用VT_UI1你必須創建單獨的變體即

    $v = new VARIANT(ord('p'), VT_UI1); 
    

    但我假設你想要的第一種方式。

    這是PHP源代碼(PHP 5.3.3)(也許會有幫助,我可能是遙遠)

    /* Only perform conversion if variant not already of type passed */
    if ((ZEND_NUM_ARGS() >= 2) && (vt != V_VT(&obj->v))) {

    /* If already an array and VT_ARRAY is passed then: 
         - if only VT_ARRAY passed then do not perform a conversion 
         - if VT_ARRAY plus other type passed then perform conversion 
         but will probably fail (origional behavior) 
        */ 
    
    +1

    感謝您的回答。但是,我仍然無法讓它工作。這會產生一個錯誤「參數不正確」,我認爲它來自實際的.NET函數,而不是PHP。我試過的代碼片段:'$ variant = new VARIANT(array());'和'$ variant = new VARIANT(array(new VARIANT(ord('p'),VT_UI1)));'。有任何想法嗎? – Tower 2010-09-11 07:32:23

    +1

    代碼$ variant = new VARIANT(array());會錯誤,因爲數組是空的,如果你想要一個空的變體,你只需要做它沒有數組()。 $ variant = new VARIANT();哪一部分給你錯誤? – 2010-09-11 18:45:26

    +0

    @Viper_Sb你會介意幫助我解決這個問題我問這裏http://stackoverflow.com/questions/42189245/how-to-pass-an-array-of-bytes-reference-to-a-com-object-方法我會很高興地感謝你的幫助。 – 2017-02-12 20:09:47

    相關問題