【问题标题】:Bind void * pointer to C++/Cli pointer of elementary type将 void * 指针绑定到基本类型的 C++/Cli 指针
【发布时间】:2011-08-28 04:05:42
【问题描述】:

我使用 C++ cli 对从非托管世界到托管世界的一些科学库 (http://root.cern.ch) 做了一些薄包装。

读取特殊文件格式(这是主要目标)通过以下方式实现:
1) 终身调用一次 SetBranchAddress(const char name, void* outputVariable) 让它知道你的变量的地址
2)比你 N 次调用 GetEntry(ulong numberOfRow) 用适当的值填充这个 void* outputVariable

我放了这个用法示例:

double myValue; //this field will be filled

//We bind myValue to the 'column' called "x" stored in the file"
TTree->SetBranchAddress("x", &myValue); 

// read first "entry" (or "row") of the file
TTree->GetEntry(0); 

// from that moment myValue is filled with value of column "x" of the first row
cout<<"First entry x = "<<myValue<<endl; 

TTree->GetEntry(100); //So myValue is filled with "x" of 101 row
...

所以在 C++/CLI 代码中,问题在于将托管基本类型绑定到这个 void * 指针;

我尝试了 3 种方法:

namespace CppLogicLibrary {
public ref class SharpToRoot
{        
       double mEventX;
       double *mEventY;
       IntPtr memEventZ;

       ///Constructor
       SharpToRoot()
       {
          mEventy = new double();
          memEventZ= Marshal::AllocHGlobal(sizeof(double));
       }

       void SetBranchAddresses()
       {
           pin_ptr<double> pinnedEventX = &mEventX;
           mTree->SetBranchAddress("ev_x", pinnedEventX);
           mTree->SetBranchAddress("ev_y", mEventY);
           mTree->SetBranchAddress("ev_z", memEventZ.ToPointer());
           ...
           //now I read some entry to test... just in place
           mTree->GetEntry(100);
           mTree->GetEntry(101);
           double x = mEventX;
           double y = *mEventY
           double z = (double)Marshal::PtrToStructure(memEventZ, Double::typeid);
       }

       ...

所有 3 个变体都编译时没有错误,也没有例外...... 但是 用一些垃圾值填充它的 (void *) 值,例如 5,12331E-305。在非托管代码中一切正常。

这种 void* 与 C++/CLI 基本类型绑定可能会出现什么错误?

【问题讨论】:

    标签: pointers c++-cli wrapper unmanaged root-framework


    【解决方案1】:

    问题是内部数据是由这个库中的浮点数提供的。因此,当它在 C# 端被映射和处理为双精度时,它给出了 5,12331E-305。

    这 3 个变量中的每一个都有效。而且,从我的角度来看,使用 pin_ptr pinnedEventX = &mEventX; 在这种情况下是不合适的,因为它不会在函数执行之间持续存在;

    我不确定,为什么这种“浮动”情况是在本机 C++ 中处理的。正如我之前写的,没有任何问题。

    【讨论】:

      猜你喜欢
      • 2021-09-20
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 2014-05-30
      • 1970-01-01
      • 2016-11-23
      相关资源
      最近更新 更多