Home  >  Article  >  Backend Development  >  PHP怎么从COM组件中获取返回的字符串

PHP怎么从COM组件中获取返回的字符串

WBOY
WBOYOriginal
2016-06-13 13:26:57883browse

PHP如何从COM组件中获取返回的字符串?
COM组件中定义一个方法
HRESULT GetStr([in,out] BSTR* vStr, [in,out] LONG* vLen, [out,retval] LONG* vError);

方法实现为:

C/C++ code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
STDMETHODIMP MC_MyClass::GetStr(BSTR* vStr, LONG* vLen, LONG* vError)
{
    *vError = 0;
    if(0 == vStr)    return S_FALSE;
    if(0 == vLen)    return S_FALSE;

    wchar_t tStr[] = L"This is a string from com!";
    memcpy(*vStr, tStr, wcslen(tStr));
    *vLen = wcslen(tStr);

    *vError = 1;
    return S_OK;
}



PHP调用该方法
C/C++ code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
$tStr = "";
$tLen = 100;
$tStr = str_pad($tStr, $tLen, "0");
$tRes = $tCom->GetStr(&$tStr, &$tLen);



 结果
$tRes = 1;
$tStr = "";
$tLen = 26;

求解:为什么$tStr为空?

------解决方案--------------------
可能要用 vsscanf 函数取回
由于没有测试条件,无法给出进一步的建议
------解决方案--------------------
memcpy(*vStr, tStr, wcslen(tStr));

wcslen是求UNICODE字符个数, memcpy是拷贝字节个数, 很明显应该wcslen(tStr)*2,另外也不是*vStr,直接是vStr.
------解决方案--------------------
探讨

memcpy(*vStr, tStr, wcslen(tStr));

wcslen是求UNICODE字符个数, memcpy是拷贝字节个数, 很明显应该wcslen(tStr)*2,另外也不是*vStr,直接是vStr.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn