问题描述
我正在研究与COM对象进行通信的Excel插件(XLL).因此,我必须在XLOPER和VARIANT之间进行编组.我已经完成了大部分工作,但是数组绝对是一件痛苦的事.我需要支持一维和二维数组.
I'm working on an Excel plugin (XLL), which communicates with COM objects. So, I have to marshall between XLOPER and VARIANT. I've got most of this working, but arrays are definitely a pain. I need to support 1- and 2D arrays.
我想以前有人已经处理过这个问题.简化处理VARIANT,SAFEARRAY和XLOPER(和XLOPER12)的最佳方法是什么?
I imagine someone has already had to deal with this before. What's the best way to simplify dealing with VARIANT, SAFEARRAY, and XLOPER (and XLOPER12)?
推荐答案
为此,我不得不手动编写自己的编组代码.没有可免费使用的库来处理此问题. XLW用于包装您的整个插件-这对我来说不是一个选择.
I had to hand-roll my own marshalling code for this. There were no freely available libs to handle this. XLW is meant for wrapping your entire plugin - this was not an option for me.
最后,只花了很多时间,浏览了xloper和variant的文档,并弄清楚了如何将它们相互映射.
In the end, it just took a lot of time, looking through the documentation for xloper and variant, and figuring out how to map them to each other.
出于好奇:
xloper(12)中的数组:
arrays in xloper(12):
- .xltype是xltypeMulti
- .val.array是指向xlopers数组的指针.
数组的变体:
- .vt是VT_ARRAY | VT_VARIANT
- .parray是SafeArray的变体形式
此编组的其他技巧:
- xltypeRef和xltypeSRef:使用xlCoerce查找实际的单元格值.
- 整数值存在溢出风险.
boost::numeric_cast
为此提供了帮助. - 字符串编组很烦人. xloper使用char,xloper12使用wchar,变体使用bstr.
- 记住要释放为新的xloper字符串分配的缓冲区.相应地使用xlbitDLLFree和xlAutoFree(12).
- 模板编程对于xloper/xloper12问题很有用.助推器有帮助. _bstr_t也有帮助.
- xltypeRef and xltypeSRef: use xlCoerce to look up actual cell values.
- integer values run the risk of overflow.
boost::numeric_cast
helps with that. - string marshalling is annoying. xloper uses char, xloper12 uses wchar, variant uses bstr.
- Remember to free the buffers you allocate for your new xloper strings. Use xlbitDLLFree and xlAutoFree(12) accordingly.
- Template programming is useful for the xloper/xloper12 issue. Boost helps. _bstr_t also helps.
这篇关于如何在XLOPER和VARIANT之间进行编组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!