找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1392|回复: 3

[分享] [转帖]resbuf与SAFEARRAY相互转换

[复制链接]
发表于 2013-5-10 22:01:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
本帖最后由 Gdlprfcu 于 2013-5-10 22:40 编辑

以下代码转自:http://adndevblog.typepad.com/autocad/2012/09/how-to-convert-a-resbuf-chain-to-a-safearray-and-vice-versa.html?cid=6a0167607c2431970b017c320cf068970b

[pcode=cpp,false]//////////////////////////////////////////////////////////////////////////////  

// constructs a resbuf filled from safearray, by Fenton Webb, DevTech
bool ResbufToSafeArrays (
        struct resbuf *rb,
        COleSafeArray &restypes,
        COleSafeArray &resvals)
{
USES_CONVERSION;
struct resbuf *ptr = rb;
// count how many entries we have
long count;
for (count=0; ptr!=NULL; ++count)
{
  ptr = ptr->rbnext;
}
// create the datatypes array
VARIANT *dataTypes = new VARIANT[count];
// create the data
VARIANT *dataVals = new VARIANT[count];
// set up the datastrings with the resbuf
for (long i=0; i<count; ++i)
{
  // setup the datatypes
  dataTypes.vt = VT_I2;
  dataVals.vt = VT_BSTR;
  // if we are dealing with a string
  if (rb->restype == RTSTR)
  {
   // make the bit minus so that we know it's a string value
   dataTypes.iVal = -rb->restype;
   // now create the bstr
   dataVals.bstrVal = CComBSTR(rb->resval.rstring);
  }
  else
  {
   dataTypes.iVal = rb->restype;
   dataVals.bstrVal =
       ::SysAllocStringByteLen ((LPCSTR)(&rb->resval), sizeof (rb->resval));
  }
  // move to the next one
  rb = rb->rbnext;
}
// create the safearrays
restypes.CreateOneDim (VT_VARIANT, count, dataTypes);
resvals. CreateOneDim (VT_VARIANT, count, dataVals);
//delete[] dataVals;
return true;
}
bool SafeArraysToResbuf (
        COleSafeArray &restypes,
        COleSafeArray &resvals,
struct resbuf **rb)
{
USES_CONVERSION;
// get the total number of restypes
long numberResTypes = restypes.GetOneDimSize();
// get the total number of resvals
long numberResVals = resvals.GetOneDimSize();
struct resbuf *resbufList = NULL, *currentPtr = NULL;
// now extract the data
for (long i=0; i<numberResVals; ++i)
{
  COleVariant dataType;
  COleVariant dataVal;
  // extract the resval from the ith level in the resval safearray
  restypes.GetElement(&i, (void*)dataType);
  // extract the resval from the ith level in the resval safearray
  resvals.GetElement(&i, (void*)dataVal);
  // create a new resbuf using the datatype received
  struct resbuf *rb = acutNewRb (abs(dataType.iVal));
  // if it is a string
  if (dataType.iVal <= 0)
  {
   // extract the string value
   _bstr_t strValue(dataVal.bstrVal);
   // allocate the memory for the string
   rb->resval.rstring = strValue.copy();
  }
  else
  {
   // convert the bstring into the binary chunk known as a resbuf
   memcpy (&rb->resval, dataVal.bstrVal, sizeof (rb->resval));
  }
  // special cases
  switch (dataType.iVal)
  {
   // xdata start
   case -3 :
   {
    rb->restype = dataType.iVal;
   };
  }
  // if this is the first time through
  if (currentPtr == NULL)
  {
   // create a new resbuf, set its restype to a default value
   resbufList = currentPtr = rb;
  }
  else
  {
   // create a new entry at the next pointer
   currentPtr->rbnext = rb;
   // now move to it
   currentPtr = currentPtr->rbnext;
  }
}
// return the resbuf
*rb = resbufList;
return true;
}
void TestSafeArrayConversion(void)
{
struct resbuf rs2;
rs2.restype = RTSTR;
rs2.resval.rstring = L"resbuf2";
rs2.rbnext = NULL;
struct resbuf rs1;
rs1.restype = RTSHORT;
rs1.resval.rint = 2008;
rs1.rbnext = &rs2;
COleSafeArray restypes;
COleSafeArray resvals;
ResbufToSafeArrays(&rs1, restypes, resvals);
struct resbuf* rb;
SafeArraysToResbuf(restypes, resvals, &rb);
}
[/pcode]




评分

参与人数 1D豆 +1 收起 理由
ScmTools + 1 很给力!分享奖!

查看全部评分

论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 1632个

财富等级: 堆金积玉

发表于 2013-5-10 22:05:49 | 显示全部楼层
代码中的空行太多,希望LZ能够将贴上来的代码整理一下,这样便于大家阅读
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-5-10 22:15:03 | 显示全部楼层
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 1632个

财富等级: 堆金积玉

发表于 2013-5-10 22:22:44 | 显示全部楼层
Gdlprfcu 发表于 2013-5-10 22:15
代码着色不好使。

你需要在这里设置,先选择语言,然后贴上你的代码就可以了
Cor.JPG
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2025-9-10 13:15 , Processed in 0.183288 second(s), 42 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表