从 Win32 RPC 调用返回一个字符串
我正在尝试进行 RPC 调用,该调用从 RPC 服务器请求 2 个数字和一个字符串,IDL 如下所示:
void GetCurrentStatus([in] handle_t hBinding, [out, ref] DWORD *dwRef1, [out, ref] DWORD *dwRef2, UINT *nLength, [out, size_is(, *nLength)] LPWSTR *pszName);
在服务器端调用中,我这样做:
// name = std::wstring
*pszName = (wchar_t*)midl_user_allocate(name.length()+1 * sizeof(wchar_t));
_tcscpy(*pszName, name.c_str());
*nLength = name.length();
但任何从客户端调用的尝试都不会产生任何结果返回错误数组边界无效。
从 RPC 调用返回字符串的正确方法是什么?
谢谢, J
I'm trying to make an RPC call which requests 2 numbers and a string from the RPC server, the IDL looks like this:
void GetCurrentStatus([in] handle_t hBinding, [out, ref] DWORD *dwRef1, [out, ref] DWORD *dwRef2, UINT *nLength, [out, size_is(, *nLength)] LPWSTR *pszName);
In the server-side call I do this:
// name = std::wstring
*pszName = (wchar_t*)midl_user_allocate(name.length()+1 * sizeof(wchar_t));
_tcscpy(*pszName, name.c_str());
*nLength = name.length();
But any attempt to call from the client-side results in nothing returned the error The array bounds are invalid.
What is the correct way to return a string from an RPC call?
Thanks,
J
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您有选择,请使用
BSTR
(即SysAllocString
)。 RPC 了解有关此数据类型的所有信息以及如何复制它并查找其长度。就
足够了,不需要单独的长度参数。
If you have a choice in the matter, use
BSTR
(i.e.SysAllocString
). RPC knows all about this data type and how to copy it and find its length.Just
is enough, no separate length parameter needed.
服务器无法将字符串值传递回客户端,因为它不知道如何编组字符串。
当您使用 BSTR 类型时,服务器知道字符串的长度。 BSTR 前面必须有一个 4 字节长度字段,并以单个空 2 字节字符结尾。
The server is not able to pass string value back to client since it doesn't know how to marshall the string..
When you use BSTR type, the server knows to the length of the string. BSTR must be preceded by a 4-byte length field and terminated by a single null 2-byte character.
你写的地方:
我相信你需要
特别是,如果你有一个空(长度为零)字符串,那么返回一个
size_is(0)
数组是不合法的——所以你必须为字符串终止 NUL (L'\0'
)。您还需要提供以字节为单位的大小,其中每个 Unicode 字符使用两个字节 - 因此您必须乘以字符大小。
Where you have written:
I believe you need
In particular, if you have an empty (length zero) string, then returning a
size_is(0)
array is not legal -- so you must add space for the string-terminating NUL (L'\0'
).You also want to supply the size in bytes, where each Unicode character uses two bytes -- therefore you must multiply by the character size.