Re: Need an STL (std:string) API call example




"Jeff F" <not@xxxxxxxxxxxx> wrote in message
news:%23qfhQGfNFHA.1500@xxxxxxxxxxxxxxxxxxxxxxx
>
> "Olaf van der Spek" <OlafvdSpek@xxxxxxxxx> wrote in message
> news:eh2qMCeNFHA.3760@xxxxxxxxxxxxxxxxxxxxxxx
>> Joseph M. Newcomer wrote:
>>> I'm writing a little utility and thought I would include in it the code
>>> necessary to
>>> allocate a buffer and fill it up with an API call. The API call is one
>>> of those that takes
>>> a NULL, 0 pair and returns the length. For example, in MFC, I would do
>>>
>>> CString s;
>>> int length = SomeAPICall(..., NULL, 0);
>>> LPTSTR data = s.GetBuffer(length);
>>> SomeAPICall(...,data, length);
>>> s.ReleaseBuffer();
>>>
>>> what I want to write is
>>>
>>> std::string s;
>>> int length = SomeAPICall(..., NULL, 0);
>>> ...and then I'm stuck
>>
>> You can't do that with string. You could do it with vector though.
>
> Specifically(untested):
>
> std::vector<char> data( SomeAPICall( ..., NULL, 0 ) );
>
> SomeAPICall( ..., &data[0], data.size() );
>
> std::string stringdata( data.begin(), data.end() );
>
>
> Which, using boost could be encapsulated into a function:
>
> std::string StringFrom( const boost::function2<std::string,char*,size_t>&
> aFnc )
> {
> std::vector<char> data( aFnc( NULL, 0 ) );
>
> aFnc( &data[0], data.size() );
>
> return std::string stringdata( data.begin(), data.end() );

oops - cut and paste error:

return std::string( data.begin(), data.end() );

> }
>
> which could be called:
>
> std::string mystring = StringFrom( boost::bind( SomeApiCall, ..., _1,
> _2 ) );
>
> Jeff Flinn
>


.



Relevant Pages

  • Re: [PATCH] Export shmem_file_setup and shmem_getpage for DRM-GEM
    ... We need to allocate objects from kernel mode to get the console running. ... Another alternative is to improve the existing shmem API to better ...
    (Linux-Kernel)
  • Re: How to retrieve data from Type?
    ... has no native way to allocate such a structure, other than a raw byte array. ... I don't know what's going on behind the API scenes, ... buffers are just being build dynamically in many cases. ... You could just pass a byte array, ...
    (microsoft.public.vb.general.discussion)
  • Re: Which to use, AllocCoTaskMem or AllocHGlobal
    ... Looking at the DocumentProperties API I do not see that any of its ... parameters require the COM allocator, ergo I would not use AllocCoTaskMem, I ... MAPI has its own API calls for memory that I ... My understanding is that AllocCoTaskMem will allocate ...
    (microsoft.public.dotnet.languages.vb)
  • Re: CE6.0 Driver Pointer Marshalling - passing pointers out only?
    ... VirtualAllocEx can be used to allocate memory in a specified process. ... Read/WriteProcessMemory can be used to access this memory. ... structures that relate to API parameters. ... Use VirtualCopyEx to map a region of kernel memory to the client's ...
    (microsoft.public.windowsce.platbuilder)
  • Re: Help Regarding Dual Core/ HT Processors
    ... He is writing to the OS API ... ... not writing his own "OS" on top of intel CPU so your points are not really aligned to his original question. ... Snip from MSDN ... They are not limited by the Windows API. ...
    (microsoft.public.win32.programmer.kernel)