我使用C++ cli从非托管环境向托管环境做了一些科学库(http://root.cern.ch)的简单包装。绑定void *指向C++/Cli基本类型指针的指针
读特殊文件格式(这是主要目标)是通过实现:
1)一旦SetBranchAddress一生的调用(为const char名,无效* outputVariable),让它知道的地址你变量
2)比你N时间呼叫GetEntry(ulong numberOfRow) wthich填充此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代码的问题是与结合管理的基本类型,以该空隙*指针;
我已经试过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个变种没有错误的编译,去没有例外...... 但填补其(无效*)值与像5一些垃圾值,12331E-305。在非托管代码中,所有工作正常。
对于C++/CLI基本类型绑定,这种void *会出现什么错误?