简体   繁体   中英

How to invoke an OleAutomation Object method for BSTR*?

Here is the IDispatch interface I got from the type library:

interface IMYPhoneCmd : IDispatch {
    [id(0x00000001), helpstring("method CallSet")]
    HRESULT CallSet(
                    [in] BSTR* name, 
                    [in, optional, defaultvalue(0)] VARIANT par1, 
                    [in, optional, defaultvalue(0)] VARIANT par2, 
                    [in, optional, defaultvalue(0)] VARIANT par3, 
                    [in, optional, defaultvalue(0)] VARIANT par4, 
                    [in, optional, defaultvalue(0)] VARIANT par5, 
                    [in, optional, defaultvalue(0)] VARIANT par6, 
                    [in, optional, defaultvalue(0)] VARIANT par7, 
                    [in, optional, defaultvalue(0)] VARIANT par8, 
                    [in, optional, defaultvalue(0)] VARIANT par9, 
                    [in, optional, defaultvalue(0)] VARIANT par10, 
                    [in, optional, defaultvalue(0)] VARIANT par11, 
                    [in, optional, defaultvalue(0)] VARIANT par12, 
                    [in, optional, defaultvalue(0)] VARIANT par13, 
                    [in, optional, defaultvalue(0)] VARIANT par14, 
                    [in, optional, defaultvalue(0)] VARIANT par15, 
                    [in, optional, defaultvalue(0)] VARIANT par16, 
                    [out, retval] long* retval);

Here is my java code:

    OleAutomation automation = new OleAutomation("PhoneScript.MYPhoneCmd");

    int[] ids = automation.getIDsOfNames(new String[] { "CallSet", "name" });
    int dispIdMember = ids[0];
    int[] rgdispidNamedArgs = new int[] {ids[1]};

        Variant[] rgvarg = new Variant[3];
        String name = "Call_setCallDistance";
        rgvarg[0] = new Variant(name);
        rgvarg[1] = new Variant("Newyork");
        rgvarg[2] = new Variant("2000");

        System.out.println(dispIdMember);

   // Call the method
       Variant pVarResult = automation.invoke(dispIdMember, rgvarg, rgdispidNamedArgs );

    // Check the return value
   if (pVarResult == null ){
     System.out.println("Failed to call method ");
    }

Output is:

1
Failed to call method 

The executable file opens but it doesn't do what I want, as you can see it fails, and I assume it has to do with the name and rgdispidNamedArgs .

Here is a working Perl equivalent:

    my $Callset = "Call_setCallDistance";                   # Callset name
    my $param1 = Newyork;                                   # Callset parameter 1
    my $param2 = 2000;                                      # Callset parameter 2
    Win32::OLE::CreateObject('PhoneScript.MYPhoneCmd', $PhoneClient) || die "can't connnect to PhoneClient: $!\n";
    $Result = $PhoneClient->CallSet($Callset, $param1, $param2); # This is the exact line I need in java

I'm working first time with OleAutomation in java and dont have much experience with it also could find very few examples on the internet for it and I tried to convert the example I found for my project but the example was with BSTR not BSTR* , it seems to me this is the only difference and it doesn't work, I dont know why. CallSet method is used for various settings/invokes like 20-30 but Im specifically interested in "Call_setCallDistance" . I would be glad if you can give a hand!

I think that the problem is rgdispidNamedArgs , which may not be required in this case and you have it filled with 1´s. We don´t initialize it, so it would be equivalent to have it filled with zeros.

I don´t have much experience either, even less in Java. You should check how automation.invoke builds the DISPARAMS structure with its arguments.

For now, I woudl suggest just removing the rgdispidNamedArgs or filling it with zero (DISP_ID_VALUE) if that is not possible

Sorry, I haven't paid enough attention to the signature. It is so unusual to have a BSTR * ( [in] BSTR* name ) instead of a VT_BSTR (variant on BSTR). Did you try using directly name instead of Variant(name)? Besides, second parameter in your Ruby example is 200 (number) but you use two strings, "Newyork" and "2000", to build variants in second and third arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM