Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2010-11

Reading std::ostream from dll

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Leonardo M. Ramé

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?

thanks in advance,

Reply by email

Marco van de Voort
to Leonardo M. Ramé
Leonardo M. Ram? wrote 19 lines
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.

Reply by email

Leonardo M. Ramé
to Marco van de Voort
Marco van de Voort wrote 27 lines
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);
  }

}

Reply by email