# CoCreateIsoForMounting

Creates an ISO file and inserts a file into it.

Example:

```
HRESULT Result = S_OK;

Result = CoCreateIsoForMounting((PWCHAR)L"MyFile.txt", //test.txt name inside of iso
                               (PWCHAR)L"C:\\Users\\User\\Desktop\\test.txt", 
                               (PWCHAR)L"C:\\Users\\User\\Desktop\\MyIso.iso", 
                               (PWCHAR)L"NewVolume");
```

```
#include <imapi2fs.h>
#include <shlwapi.h>

#pragma comment(lib, "shlwapi.lib")

HRESULT CoCreateIsoForMounting(_In_ PWCHAR FileDisplayName, _In_ PWCHAR FullPathToFile ,_In_ PWCHAR FullIsoPath, _In_ PWCHAR VolumeName)
{
    HRESULT Result = S_OK;

    IFileSystemImage* Fsi = NULL;
    IFsiDirectoryItem* Root = NULL;
    IFileSystemImageResult* ImageResult = NULL;
    IStream* Stream = NULL;
    IStream* ImageStream = NULL;

    BSTR bpFileDisplayName = NULL;
    BSTR bpFullIsoPath = NULL;
    BSTR bpVolumeName = NULL;

    HANDLE hHandle = INVALID_HANDLE_VALUE;
    ULARGE_INTEGER LargeInteger = { 0 };
    DWORD BytesWritten = 0;
    BYTE Buffer[4096] = { 0 };
    ULONG BytesRead = 0;

    STATSTG Stat = { 0 };

    Result = CoInitialize(NULL);
    if (!SUCCEEDED(Result))
        goto EXIT_ROUTINE;

    Result = CoCreateInstance(__uuidof(MsftFileSystemImage), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&Fsi));
    if (!SUCCEEDED(Result))
        goto EXIT_ROUTINE;

    Result = Fsi->put_FileSystemsToCreate(FsiFileSystemISO9660);
    if (!SUCCEEDED(Result))
        goto EXIT_ROUTINE;

    bpVolumeName = SysAllocString(VolumeName);
    if (bpVolumeName == NULL)
        goto EXIT_ROUTINE;

    Result = Fsi->put_VolumeName(bpVolumeName);
    if (!SUCCEEDED(Result))
        goto EXIT_ROUTINE;

    Result = Fsi->get_Root(&Root);
    if (!SUCCEEDED(Result))
        goto EXIT_ROUTINE;

    bpFileDisplayName = SysAllocString(FileDisplayName);
    if(bpFileDisplayName == NULL)
        goto EXIT_ROUTINE;

    Result = SHCreateStreamOnFileEx(FullPathToFile, STGM_READ | STGM_SHARE_DENY_WRITE, FILE_ATTRIBUTE_NORMAL, FALSE, NULL, &Stream);
    if (!SUCCEEDED(Result))
        goto EXIT_ROUTINE;

    Result = Root->AddFile(bpFileDisplayName, Stream);
    if (!SUCCEEDED(Result))
        goto EXIT_ROUTINE;

    Result = Fsi->CreateResultImage(&ImageResult);
    if (!SUCCEEDED(Result))
        goto EXIT_ROUTINE;

    Result = ImageResult->get_ImageStream(&ImageStream);
    if (!SUCCEEDED(Result))
        goto EXIT_ROUTINE;

    hHandle = CreateFileW(FullIsoPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hHandle == INVALID_HANDLE_VALUE)
        goto EXIT_ROUTINE;

    Result = ImageStream->Stat(&Stat, STATFLAG_NONAME);
    if (!SUCCEEDED(Result))
        goto EXIT_ROUTINE;

    Result = ImageStream->Seek({ 0 }, STREAM_SEEK_END, &LargeInteger);
    if (!SUCCEEDED(Result))
        goto EXIT_ROUTINE;

    Result = ImageStream->Seek({ 0 }, STREAM_SEEK_SET, NULL);
    if (!SUCCEEDED(Result))
        goto EXIT_ROUTINE;

    while (ImageStream->Read(Buffer, sizeof(Buffer), &BytesRead) == S_OK && BytesRead > 0)
    {
        if (!WriteFile(hHandle, Buffer, BytesRead, &BytesWritten, NULL))
                goto EXIT_ROUTINE;
    }

EXIT_ROUTINE:

    if (bpVolumeName)
        SysFreeString(bpVolumeName);

    if (bpFileDisplayName)
        SysFreeString(bpFileDisplayName);

    if (hHandle)
        CloseHandle(hHandle);

    if (Stream)
        Stream->Release();

    if (ImageStream)
        ImageStream->Release();

    if (ImageResult)
        ImageResult->Release();

    if (Root)
        Root->Release();

    if (Fsi)
        Fsi->Release();

    CoUninitialize();

    return Result;
}

```

