开发者

Bind void * pointer to C++/Cli pointer of elementary type

I do some thin wrapper of some scientific library (http://root.cern.ch) from unmanaged to managed world using C++ cli.

Reading of the special file format (which is the main goal) is implemented through:

1) Once a lifetime call of SetBranchAddress(const char name, void* outputVariable) to let it know an address of your variable

2) Than you N time call GetEntry(ulong numberOfRow) wthich fills this void* outputVariable with the appropriate value;

I put this example of usage :

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
...

So in C++/CLI code the problem is with binding managed elementary types to this void * pointer;

I have tried 3 approaches:

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);
       }

       ...

All of 3 variants are compiled with no errors, goes with no exceptions... BUT fills its (void *) values with some rubbish value like 5,12331E-305. In unmanaged code all works fine.

What could be the error with such void* to C++/CLI elementary types binding?


The problem was that internally data was presented by floats inside of this library. So, when it was mapped and processed as doubles on the C# side, it gave 5,12331E-305.

Each of this 3 variats worked. And, from my point of view, using of pin_ptr pinnedEventX = &mEventX; was improper in this case, because it doesn't persist between functions execution;

What I'm not sure, why this "float" situation was handled in native C++. As I wrote before, there wasn't any problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜