这是我从类型库中获得的 IDispatch 接口:
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);
这是我的java代码:
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 ");
}
输出是:
1
Failed to call method
可执行文件打开,但它没有做我想要的,你可以看到它失败了,我认为它与name
and rgdispidNamedArgs
.
这是一个有效的 Perl 等价物:
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
我第一次在 Java 中使用 OleAutomation 并且没有太多经验,也可以在互联网上找到很少的示例,我尝试转换我为我的项目找到的示例,但示例是BSTR而不是BSTR*,在我看来,这是唯一的区别,它不起作用,我不知道为什么。CallSet 方法用于各种设置/调用,例如 20-30,但我对"Call_setCallDistance"特别感兴趣。如果你能伸出援手,我会很高兴!