如何将 SAFEARRAY(字节数组)放入 HTML 隐藏字段
我想从 active-x 组件获取字节数组,将其存储在 html 表单输入隐藏字段中,然后通过表单提交将其传递到服务器。我怎样才能做到这一点?
MIDL:
HRESULT Data([out, retval] SAFEARRAY(VARIANT) *pArray);
C++/ATL
STDMETHODIMP MyActiveX::get_Data(SAFEARRAY **pArray)
{
CComSafeArray<BYTE> arr;
for (int i = 0; i < 10; i++)
{
CComVariant a;
a = (BYTE)i;
arr.Add(a);
}
arr.CopyTo(pArray);
return S_OK;
}
Javascript:
$("#hiddenField").val(myActiveX.Data);
浏览器告诉我:类型不匹配
I'd like to get array of bytes from active-x component, store that in html-form input hidden field and then pass it to server via form-submit. How can I do that?
MIDL:
HRESULT Data([out, retval] SAFEARRAY(VARIANT) *pArray);
C++/ATL
STDMETHODIMP MyActiveX::get_Data(SAFEARRAY **pArray)
{
CComSafeArray<BYTE> arr;
for (int i = 0; i < 10; i++)
{
CComVariant a;
a = (BYTE)i;
arr.Add(a);
}
arr.CopyTo(pArray);
return S_OK;
}
Javascript:
$("#hiddenField").val(myActiveX.Data);
Browser tells me: type mismatch
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然我不熟悉你的具体情况,但我以前也见过一些类似的情况。
您使用
$('#hiddenField')
将数据放入字段中是正确的。如果您在该字段上添加了name
属性,使其成为 HTTP 提交的一部分,那么该部分就很好。至于
myActiveX.Data
,我想这是某种JavaScript对象。请记住,只能将字符串放入 HTML 输入中;它不保存二进制数据。我要做的就是在
$("#hiddenField").val(myActiveX.Data); 之前放置一个断点
。如果您不熟悉debugger
关键字,请使用它。在调试器中运行代码并查看myActiveX.Data
值的结构。它可能有某种包装字段。或者,如果您无法使用良好的 JavaScript 调试器,请尝试以下操作”
Although I am not familiar with your exact situation, I have seen some similar situations before.
You are correct to put your data in a field using
$('#hiddenField')
. If you've put aname
attribute on that field so that it becomes part of the HTTP submit, that part is good.As for
myActiveX.Data
, I imagine that this is some sort of JavaScript object. Remember that only a string can be put into an HTML input; it does not hold binary data.What I would do is put a breakpoint before
$("#hiddenField").val(myActiveX.Data);
. Use thedebugger
keyword if you're not familiar with it. Run the code in your debugger and look at the structure of the value ofmyActiveX.Data
. It probably has some sort of wrapper field.Alternatively, if you don't have access to a good JavaScript debugger, try the following"
我假设 C++ 代码是服务器端代码。
处理此问题的最佳方法是序列化 SAFEARRAY。从那里您可以通过两种方式处理它。
首先,系列化。我查看了 MSDN,我认为使用 LPSAFEARRAY_Marshal 和 LPSAFEARRAY_Unmarshal (使用可选的
IDispatch
或IUnknown
IID 来指定类型,但文档没有说明如何使用)或LPSAFEARRAY_UserMarshal 和 LPSAFEARRAY_UserUnmarshal 进行转换SAFEARRAY 与序列化格式之间的转换。其次,处理数据传输。
无论哪种方式,当您需要取回数据时,只需使用匹配函数对其进行反序列化即可。
I'm assuming the C++ code is the server side code.
The best way to handle this is to serialise the SAFEARRAY. From there you can handle it in two ways.
Firstly, the serialisation. I've looked at MSDN and I think using LPSAFEARRAY_Marshal and LPSAFEARRAY_Unmarshal (with an optional
IDispatch
orIUnknown
IID to specify the type, but the documentation doesn't say how it's used) or LPSAFEARRAY_UserMarshal and LPSAFEARRAY_UserUnmarshal to convert the SAFEARRAY to/from a serialised format.Secondly, handling the data transfer.
Either way, when you need to get the data back, just de-serialise it with the matching function.