Pointers 将void*指针绑定到基本类型的C++/Cli指针
我做了一些科学图书馆的薄包装http://root.cern.ch 使用C++ CLI。p> 读取特殊文件格式是主要目标,通过以下方式实现: 1一生调用一次SetBranchAddressconst char name,void*outputVariable,让它知道变量的地址 2,然后调用GetEntryulong numberOfRow,用适当的值填充此void*outputVariable 我举了一个用法的例子:Pointers 将void*指针绑定到基本类型的C++/Cli指针,pointers,c++-cli,wrapper,unmanaged,root-framework,Pointers,C++ Cli,Wrapper,Unmanaged,Root Framework,我做了一些科学图书馆的薄包装http://root.cern.ch 使用C++ CLI。p> 读取特殊文件格式是主要目标,通过以下方式实现: 1一生调用一次SetBranchAddressconst char name,void*outputVariable,让它知道变量的地址 2,然后调用GetEntryulong numberOfRow,用适当的值填充此void*outputVariable 我举了一个用法的例子: double myValue; //this field will be f
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个变量编译时没有错误,没有异常。。。但用一些垃圾值(如512331E-305)填充其空*值。在非托管代码中,所有这些都可以正常工作
将void*绑定到C++/CLI基本类型可能会有什么错误?问题在于,内部数据是由该库中的浮点表示的。因此,当它在C端被映射和处理为双精度时,它给出了512331E-305 这3个变量中的每一个都起作用。在我看来,使用 pin_ptr pinnedEventX=&mEventX; 在这种情况下是不合适的,因为它不会在函数执行之间持久存在
我不确定,为什么这个浮点的情况是用C++语言处理的。正如我以前写的,没有任何问题