# 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;
}

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://malwaresourcecode.com/home/component-object-model/cocreateisoformounting.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
