IMAPI2: adding files and folders fails

0

I'm trying to create a disc image via IMAPI2. The code is pretty straightforward:

#include <windows.h>
#include <iostream>
#include <fstream>
#include <shlwapi.h>
#include <cstdio>

#include <imapi.h>
#include <imapi2.h>
#include <imapi2fs.h>
#include <imapierror.h>
#include <imapi2error.h>
#include <imapi2fserror.h>

using namespace std;

int main() {
CoInitialize(NULL);

HRESULT res = 0;
IFileSystemImage* image = NULL;
IFileSystemImageResult* result = NULL;
IFsiDirectoryItem* root = NULL;
IStream* file = NULL;
IStream* r_i = NULL;

res = CoCreateInstance(CLSID_MsftFileSystemImage, NULL, CLSCTX_ALL, __uuidof(IFileSystemImage), (void**) &image);
printf("1 %08X\n", res);
res = image -> put_FileSystemsToCreate((FsiFileSystems)(FsiFileSystemJoliet | FsiFileSystemISO9660));
printf("2 %08X\n", res);
res = image -> put_VolumeName(L"Pictures");
printf("3 %08X\n", res);
res = image -> ChooseImageDefaultsForMediaType(IMAPI_MEDIA_TYPE_CDRW);
printf("4 %08X\n", res);

res = image -> get_Root(&root);
printf("5 %08X\n", res);
res = root -> AddTree(L"C:\\pictures", VARIANT_TRUE);
printf("6 %08X\n", res);
res = SHCreateStreamOnFileEx(L"C:\\main.cpp", STGM_READ | STGM_SHARE_DENY_NONE | STGM_DELETEONRELEASE, FILE_ATTRIBUTE_NORMAL, false, NULL, &file);
printf("7 %08X\n", res);
res = root -> AddFile(L"main.cpp", file);
printf("8 %08X\n", res);

res = image -> CreateResultImage(&result);
printf("9 %08X\n", res);
res = result -> get_ImageStream(&r_i);
printf("0 %08X\n", res);

STATSTG stg;
r_i -> Stat(&stg, 1);
char* data = new char[stg.cbSize.QuadPart];
ULONG junk;
r_i -> Read(data, stg.cbSize.QuadPart, &junk);

FILE* f = fopen("image.iso", "wb");
fwrite(data, stg.cbSize.QuadPart, 1, f);
fclose(f);

delete data;
r_i -> Release();
file -> Release();
root -> Release();
result -> Release();
image -> Release();

CoUninitialize();
printf("Completed.\n");
scanf("%d", &junk);
}

Commands 6 and 8 fail with 0xC0AAB101 (invalid parameter). This happens on both Win XP SP3 and Win 7 Pro SP1 x32. The rest works fine and I get an empty image. What am I missing? (P.S. The code above was inspired by an open-source app. It works out of the box but starts to fail sometimes when I rebuild it on my machine.)

c++
com
imapi
asked on Stack Overflow Feb 23, 2014 by user2286759

1 Answer

1

->AddTree ( and most COM object methods ) expect a BSTR string which will you can create and need to be release to prevent a memory leak. You could use something such as...

// allocate a bstr string
BSTR bstrPictures = SysAllocString(L"C:\\Pictures");

// use it
res = root -> AddTree(bstrPictures, VARIANT_TRUE);
printf("6 %08X\n", res);

// free the bstr string
SysFreeString(bstrPictures);

Although BSTR can be used in place of a 'wide char string', the reverse is not always true. A BSTR has extra data in front of the actual character data, hence the functions required to manipulate them.

Try not to get into a bad habit of using them like this...

res = root -> AddTree(SysAllocString(L"C:\\Pictures"), VARIANT_TRUE);

as you will have a memory leak.

Have fun.

answered on Stack Overflow May 2, 2014 by David Camp

User contributions licensed under CC BY-SA 3.0