如何将数组从C#dll返回到php [英] How to return an array from a c# dll to php
问题描述
我有以下c#类库:
using System.Runtime.InteropServices;
namespace phptest
{
public class Class1
{
public int getint()
{
return 17;
}
public string getstring()
{
return "yup";
}
public int doubleint(int x)
{
return x * 2;
}
public string doublestring(string s)
{
return s + s;
}
[return: MarshalAs(UnmanagedType.SafeArray)]
public int[] getintarray()
{
return new int[] { 1, 7, 9 };
}
}
}
和以下php脚本对其进行测试:
and the following php script to test it:
$obj = new COM("phptest.Class1");
print $obj->getint();
print "\r\n" . $obj->getstring();
print "\r\n" . $obj->doublestring("dog") . "\r\n";
print $obj->doubleint(11);
$x = $obj->getintarray();
print_r($x);
除了 getintarray();
php脚本的最后一行输出: variant Object
The last line of the php script outputs: variant Object
var_dump($ x)
带有空白.
如果我将c#更改为返回对象数组,则:
if I change the c# to return an array of objects instead:
[return: MarshalAs(UnmanagedType.SafeArray)]
public object[] getintarray()
{
return new object[] { 1, 7, 9 };
}
然后 var_dump($ x)
附带:
object(variant)#2 (0) {
}
如何获取c#dll传回的数组中的值?
How do I get at the values in the array passed back by the c# dll?
我需要对C#或php或两者同时做些什么吗?
Is there something I need to do to the c# or the php or both?
我还将需要字符串数组,因此对此的任何帮助也将非常受欢迎.
I'll also be needing string arrays, so any help on that would be most welcome too.
按照迈克尔的建议,我尝试了以下操作:
As suggested by Michael, I tried the following:
[return: MarshalAs(UnmanagedType.LPArray)]
public int[] getintarray()
...
它的返回值与我返回对象数组时的返回值相同.
It has the same returns as when I had it returning an object array.
还 count($ x)
给出了返回数组中的元素数量(在这种情况下为3)
Also count($x)
gives me the number of elements in the returned array (3 in this case)
我尝试通过以下方式访问元素:
I have tried accessing the elements in the following ways:
$x[0] outputs "variant Object"
$x->value outputs nothing
$x[0]->value outputs nothing
$x->0 outputs nothing
var_dump($ x [0])和var_dump($ x)一样
var_dump($x[0]) gives me the same as var_dump($x)
推荐答案
不是最好的方法,但请查看您是否可以在PHP中获得任何帮助.
Not the best way to do it but see if you are able to get anything with this in Php.
foreach($x as $array){
echo "Value ".$array. "<br/>";
}
这篇关于如何将数组从C#dll返回到php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!