我使用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 *會出現什麼錯誤?