[Lazarus] Reading std::ostream from dll

Leonardo M. Ramé l.rame at griensu.com
Thu Nov 25 13:21:44 CET 2010


On 2010-11-25 12:43:08 +0100, Marco van de Voort wrote:
> On Tue, Nov 23, 2010 at 05:18:38PM -0300, Leonardo M. Ram? wrote:
> > Hi, I'm working with a dll/so created with C++, that has a function which
> > fills an std::ostream, and I need to read it from a FreePascal app using
> > a buffer or TStream.
> > 
> > The C++ method is similar to this:
> > 
> > void myClass::getOutputData(ostream &out);
> > 
> > I wrapped it to be readable from FPC as this:
> > ...
> > extern "C"{
> >   void getMyOutputData(void * myInstance, void * &stream){
> >     myClass * lInstance = (myClass *)myInstance;
> >     lInstance->getOutputData(stream);
> >   }
> > }
> > ...
> > 
> > Is there a way to read std::ostream from FPC?
> 
> No. This kind of stuff doesn't even work between two different C++ compilers.
> 
> You probably need to expose this on C level as a proceure that writes "n"
> bytes to a handle. Then the implementation (in C++) should write that block
> of mem to the stream.
> 

Thanks Marco, actually the stream contained ASCII data, it was something
lika an ObjectPascal's TStringList, so I fill a char* with that data and
return it to my program:

//---------- C++ ------------
...
void getTags(void * imagen, char * &aString)
{
    DicomImageHandler * imgHandler = (DicomImageHandler *)imagen;
    std::stringbuf sb;
    std::ostream out(&sb);
    // this fills the "out" stream with data
    imgHandler->getDataset()->print(out);
    aString = new char[strlen(sb.str().c_str()) + 1];
    std::strcpy(aString, sb.str().c_str());
}
...
//---------- End C++ ----------


extern "C"{
  void getDicomTags(void * imagen, char * &aString)
  {
      getTags(imagen, aString);
  }
}

-- 
Leonardo M. Ramé
http://leonardorame.blogspot.com




More information about the Lazarus mailing list