简体   繁体   中英

Pass By Reference to COM Object in PHP

So I'm hoping someone can help and I'm sure this is probably something simple I'm missing. I'm using PHP to access a .net API for a third party software.

Based on the very minimalist documentation on the API I have a working vbsript that connects to the object, performs a login and then does a query which results in the output of the query being dumped to a message box.

Here's the vbscript sample:

'Test device status
Set xxx = CreateObject("The.API.Object.Goes.Here")
'Login
Result = Xxx.LoginToHost("xxx.xxx.xxx.xxx","8989","Administrator","")
if (Result = true) then
  MsgBox("OK")
else
  MsgBox("Error - " & Xxx.LastError)
  WScript.Quit
end if
'Get Status
Result = Xxx.GetDeviceStatus("", out)
if (Result = true) then
  MsgBox(out)
else
  MsgBox("Error - " & Xxx.LastError)
end if
'Logout
Result = Xxx.Logout()
if (Result = true) then
  MsgBox("Logout OK")
else
  MsgBox("Error - " & Xxx.LastError)
end if

The Xxx.GetDeviceStatus has two perimeters, the first being a device target or if left blank returns all devices, the second is the string variable to dump the result in.

When the script executes, the second message box contains a list of all devices as I would expect.

In PHP I have:

$obj = new DOTNET("XxxScripting, Version=1.0.XXXX.XXXXXX, Culture=neutral, PublicKeyToken=XXXXXXXXXXXXXXXX","Here.Goes.The.Api");
$obj->LoginToHost('xxx.xxx.xxx.xxx','8989','Administrator','');
$result = $obj->GetDeviceStatus('','out');
echo $result."<br />";

echoing result gives 1 because the value of result is a boolean value and GetDeviceStatus is successful. What I can't figure out is how to get the value of 'out' which is the actual query result.

Any help would be greatly appreciated.

The second parameter of GetDeviceStatus() method call according to the VBScript should pass a variable that will be populated with the output. However in the PHP example you are just passing the string 'out' which isn't equivalent to what is being done in the VBScript.

Instead try passing a PHP variable to the method and then echoing that variable to screen, like this;

$result = $obj->GetDeviceStatus('', $out);
if ($result)
  echo $out."<br />";

After a bit of digging it appears according to the PHP Reference that you need to pass By Reference variables to COM using the VARIANT data type.

Quote from ferozzahid [at] usa [dot] com on PHP - COM Functions

"To pass a parameter by reference to a COM function, you need to pass VARIANT to it. Common data types like integers and strings will not work for it."

With this in mind maybe this will work;

$out = new VARIANT;
$result = $obj->GetDeviceStatus('', $out);
if ($result)
  echo $out."<br />";

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