I'm trying to create a disc image via IMAPI2. The code is pretty straightforward:
我想通过IMAPI2创建一个光盘映像。代码非常简单:
#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.)
命令6和8以0xC0AAB101(无效参数)失败。这发生在Win XP SP3和Win 7 Pro SP1 x32上。其余的都没问题,我得到一个空的图像。我缺少什么?(上面的代码是由一个开源应用程序设计的。它可以在盒子里运行,但有时当我在我的机器上重新编译时,它就会失败。)
1 个解决方案
#1
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...
->AddTree(和大多数COM对象方法)期望一个BSTR字符串,您可以创建并需要释放,以防止内存泄漏。你可以用……
// 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.
虽然BSTR可以用来代替“宽字符字符串”,但反过来并不总是正确的。BSTR在实际字符数据前面有额外的数据,因此需要对它们进行操作。
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.
玩得开心。
#1
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...
->AddTree(和大多数COM对象方法)期望一个BSTR字符串,您可以创建并需要释放,以防止内存泄漏。你可以用……
// 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.
虽然BSTR可以用来代替“宽字符字符串”,但反过来并不总是正确的。BSTR在实际字符数据前面有额外的数据,因此需要对它们进行操作。
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.
玩得开心。