Skip to content

Instantly share code, notes, and snippets.

@daaximus
Last active February 21, 2023 19:59
Show Gist options
  • Save daaximus/a48b0a991b31e8841b68dbbc480a0a5a to your computer and use it in GitHub Desktop.
Save daaximus/a48b0a991b31e8841b68dbbc480a0a5a to your computer and use it in GitHub Desktop.
create iso using imapi
#include <string>
#include <atlbase.h>
#include <imapi2fs.h>
void create_iso( std::wstring_view src, std::wstring_view iso_path )
{
HRESULT hr;
IFileSystemImage* fsimg;
IFsiDirectoryItem* fsdir;
IFileSystemImageResult* fsresult;
IStream* img;
IStream* filestream;
hr = CoCreateInstance( __uuidof( MsftFileSystemImage ),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof( IFileSystemImage ),
( LPVOID* ) &fsimg );
if ( SUCCEEDED( hr ) )
{
fsimg->get_Root( &fsdir );
fsdir->AddTree( CComBSTR( src.data() ), VARIANT_TRUE );
hr = fsimg->CreateResultImage( &fsresult );
if ( SUCCEEDED( hr ) )
{
fsresult->get_ImageStream( &img );
if ( SUCCEEDED( hr ) )
{
STATSTG statstg;
ULARGE_INTEGER rd;
ULARGE_INTEGER wr;
SHCreateStreamOnFileEx( iso_path.data(),
STGM_READWRITE,
FILE_ATTRIBUTE_NORMAL,
TRUE,
NULL,
&filestream );
img->Stat( &statstg, STATFLAG_DEFAULT );
img->CopyTo( filestream, statstg.cbSize, &rd, &wr );
filestream->Release();
img->Release();
}
fsresult->Release();
}
fsimg->Release();
}
}
CoInitializeEx(0);
create_iso(R("d:\in\testdir"), R("d:\out\test.iso"));
//
// OR...
//
create_iso(R("d:\in\testfile.ext"), R("d:\out\test.iso"));
CoUninitialize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment