<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Hi,</p>
    <p>size_t is an incorrect size for DataLength (if you run on 32 bit
      architecture). You should use vsi_l_offset.</p>
    <p>Besides that the following standalone inspired from your code
      works fine for me:<br>
    </p>
    <p>#include <stdio.h><br>
      #include "gdal_priv.h"<br>
      <br>
      int main()<br>
      {<br>
          GDALAllRegister();<br>
          GDALDataset* Dataset
      =GDALDataset::FromHandle(GDALOpenEx("poly.shp", GDAL_OF_VECTOR |
      GDAL_OF_VERBOSE_ERROR, nullptr, nullptr, nullptr));<br>
          GDALDataset* Copy =
      Dataset->GetDriver()->CreateCopy("/vsimem/foo.shp", Dataset,
      false, nullptr, nullptr, nullptr); //appears to produce a valid
      Dataset containing the same layers & features as its source.<br>
          GDALClose(Dataset);<br>
          GDALClose(Copy);<br>
          vsi_l_offset DataLength = 0;<br>
          GByte* Buffer = VSIGetMemFileBuffer("/vsimem/foo.shp",
      &DataLength, true);<br>
          printf("%p %d\n", Buffer, int(DataLength)); // buffer not null
      and DataLength > 0<br>
          CPLFree(Buffer);<br>
          VSIUnlink("/vsimem/foo.dbf");<br>
          VSIUnlink("/vsimem/foo.shx");<br>
          return 0;<br>
      }</p>
    <p>Even<br>
    </p>
    <div class="moz-cite-prefix">Le 04/10/2023 à 06:26, Soren Saville
      Scott via gdal-dev a écrit :<br>
    </div>
    <blockquote type="cite"
cite="mid:CALK_jWa8DfcC4R5KQF+poU+Df8nf=mo5dOTmv=O-G7_8mTJPuA@mail.gmail.com">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <div dir="ltr">Hi,
        <div><br>
        </div>
        <div>I'm integrating GDAL in C++ with a system that defines its
          own asset types stored on disk, and am writing a wrapper for
          datasets using this asset system. To 'import' a dataset I call
          GDALOpenEx on it, CreateCopy() it to some "/vsimem/foo.shp"
          path, GDALClose() the datasets to flush it, and call
          VSIGetMemFileBuffer with bUnlinkAndSeize=true to take
          ownership of the underlying memory buffer. </div>
        <div><br>
        </div>
        <div>GDALDataset* Dataset = GDALOpenEx("foo.shp", GDAL_OF_VECTOR
          | GDAL_OF_VERBOSE_ERROR, nullptr, nullptr, nullptr);</div>
        <div>GDALDataset* Copy =
          Dataset->GetDriver()->CreateCopy("/vsimem/foo.shp",
          Dataset, false, nullptr, nullptr, nullptr); //appears to
          produce a valid Dataset containing the same layers &
          features as its source.</div>
        <div>GDALClose(Dataset);</div>
        <div>GDALClose(Copy);</div>
        <div>size_t DataLength;</div>
        <div>GByte* Buffer = VSIGetMemFileBuffer("/vsimem/foo.shp",
          &DataLength, true); //<-- returns NULL</div>
        <div><br>
        </div>
        <div>For some reason VSIGetMemFileBuffer always fails, returning
          a nullptr. This is basically identical to a similar problem I
          had, described here:<a
href="https://stackoverflow.com/questions/73644157/get-raw-buffer-for-in-memory-dataset-in-gdal-c-api"
            target="_blank" moz-do-not-send="true"> https://stackoverflow.com/questions/73644157/get-raw-buffer-for-in-memory-dataset-in-gdal-c-api </a> which
          I never really found a solution to. I have similar issues with
          other VSI functions, e.g. calling GDALOpenEx on a path created
          by VSIFileFromMemBuffer also fails.</div>
        <div><br>
        </div>
        <div>Any idea as to what the problem is here?</div>
        <div><br>
        </div>
        <div>Alternatively, maybe there's a better way to 'emplace'
          datasets inside some other file?</div>
        <div><br>
        </div>
        <div>Thanks,</div>
        <div>Soren Saville Scott</div>
      </div>
      <br>
      <fieldset class="moz-mime-attachment-header"></fieldset>
      <pre class="moz-quote-pre" wrap="">_______________________________________________
gdal-dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:gdal-dev@lists.osgeo.org">gdal-dev@lists.osgeo.org</a>
<a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/gdal-dev">https://lists.osgeo.org/mailman/listinfo/gdal-dev</a>
</pre>
    </blockquote>
    <pre class="moz-signature" cols="72">-- 
<a class="moz-txt-link-freetext" href="http://www.spatialys.com">http://www.spatialys.com</a>
My software is free, but my time generally not.</pre>
  </body>
</html>