本文整理汇总了C++中safe_closehandle函数的典型用法代码示例。如果您正苦于以下问题:C++ safe_closehandle函数的具体用法?C++ safe_closehandle怎么用?C++ safe_closehandle使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了safe_closehandle函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: DeletePartitions
/* Delete the disk partition table */
BOOL DeletePartitions(HANDLE hDrive)
{
BOOL r;
DWORD size;
CREATE_DISK CreateDisk = {PARTITION_STYLE_RAW, {{0}}};
PrintInfoDebug(0, MSG_239);
size = sizeof(CreateDisk);
r = DeviceIoControl(hDrive, IOCTL_DISK_CREATE_DISK,
(BYTE*)&CreateDisk, size, NULL, 0, &size, NULL );
if (!r) {
uprintf("Could not delete drive layout: %s\n", WindowsErrorString());
safe_closehandle(hDrive);
return FALSE;
}
r = DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &size, NULL );
if (!r) {
uprintf("Could not refresh drive layout: %s\n", WindowsErrorString());
safe_closehandle(hDrive);
return FALSE;
}
return TRUE;
}
示例2: SearchProcessAlt
/**
* Alternative search for processes keeping a handle on a specific disk or volume
* Note that this search requires opening the disk or volume, which may not always
* be convenient for our usage (since we might be looking for processes preventing
* us to open said target in exclusive mode).
*
* \param HandleName The name of the handle to look for.
*
* \return TRUE if processes were found, FALSE otherwise.
*/
BOOL SearchProcessAlt(char* HandleName)
{
NTSTATUS status = STATUS_SUCCESS;
ULONG i;
HANDLE searchHandle = NULL;
BOOLEAN bFound = FALSE;
PFILE_PROCESS_IDS_USING_FILE_INFORMATION info = NULL;
status = PhCreateHeap();
if (!NT_SUCCESS(status))
goto out;
// Note that the access rights being used with CreateFile() might matter...
searchHandle = CreateFileA(HandleName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
status = PhQueryProcessesUsingVolumeOrFile(searchHandle, &info);
if (NT_SUCCESS(status) && (info->NumberOfProcessIdsInList > 0)) {
bFound = TRUE;
uprintf("WARNING: The following process(es) or service(s) are accessing %s:", HandleName);
for (i = 0; i < info->NumberOfProcessIdsInList; i++) {
uprintf("o Process with PID %ld", info->ProcessIdList[i]);
}
}
out:
safe_closehandle(searchHandle);
PhFree(info);
PhDestroyHeap();
if (!NT_SUCCESS(status))
uprintf("SearchProcessAlt('%s') failed: %s", HandleName, NtStatusError(status));
return bFound;
}
示例3: GetHandle
/*
* Open a drive or volume with optional write and lock access
* Return INVALID_HANDLE_VALUE (/!\ which is DIFFERENT from NULL /!\) on failure.
*/
static HANDLE GetHandle(char* Path, BOOL bWriteAccess, BOOL bLockDrive)
{
int i;
DWORD size;
HANDLE hDrive = INVALID_HANDLE_VALUE;
if (Path == NULL)
goto out;
hDrive = CreateFileA(Path, GENERIC_READ|(bWriteAccess?GENERIC_WRITE:0),
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
if (hDrive == INVALID_HANDLE_VALUE) {
uprintf("Could not open drive %s: %s\n", Path, WindowsErrorString());
goto out;
}
if (bWriteAccess) {
uprintf("Caution: Opened drive %s for write access\n", Path);
}
if (bLockDrive) {
for (i = 0; i < DRIVE_ACCESS_RETRIES; i++) {
if (DeviceIoControl(hDrive, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &size, NULL))
goto out;
if (IS_ERROR(FormatStatus)) // User cancel
break;
Sleep(DRIVE_ACCESS_TIMEOUT/DRIVE_ACCESS_RETRIES);
}
// If we reached this section, either we didn't manage to get a lock or the user cancelled
uprintf("Could not get exclusive access to device %s: %s\n", Path, WindowsErrorString());
safe_closehandle(hDrive);
}
out:
return hDrive;
}
示例4: GetUSBProperties
/*
* Get the VID, PID and current device speed
*/
static void GetUSBProperties(char* parent_path, char* device_id, usb_device_props* props)
{
HANDLE handle = INVALID_HANDLE_VALUE;
DWORD size;
DEVINST device_inst;
USB_NODE_CONNECTION_INFORMATION_EX conn_info;
USB_NODE_CONNECTION_INFORMATION_EX_V2 conn_info_v2;
PF_INIT(CM_Get_DevNode_Registry_PropertyA, Cfgmgr32);
if ((parent_path == NULL) || (device_id == NULL) || (props == NULL)) {
return;
}
props->port = 0;
size = sizeof(props->port);
if ( (pfCM_Get_DevNode_Registry_PropertyA != NULL) &&
(CM_Locate_DevNodeA(&device_inst, device_id, 0) == CR_SUCCESS) ) {
pfCM_Get_DevNode_Registry_PropertyA(device_inst, CM_DRP_ADDRESS, NULL, (PVOID)&props->port, &size, 0);
}
handle = CreateFileA(parent_path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (handle == INVALID_HANDLE_VALUE) {
uprintf("Could not open hub %s: %s", parent_path, WindowsErrorString());
goto out;
}
memset(&conn_info, 0, sizeof(conn_info));
size = sizeof(conn_info);
conn_info.ConnectionIndex = (ULONG)props->port;
// coverity[tainted_data_argument]
if (!DeviceIoControl(handle, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX, &conn_info, size, &conn_info, size, &size, NULL)) {
uprintf("Could not get node connection information for '%s': %s", device_id, WindowsErrorString());
goto out;
}
props->vid = conn_info.DeviceDescriptor.idVendor;
props->pid = conn_info.DeviceDescriptor.idProduct;
props->speed = conn_info.Speed + 1;
// In their great wisdom, Microsoft decided to BREAK the USB speed report between Windows 7 and Windows 8
if (nWindowsVersion >= WINDOWS_8) {
memset(&conn_info_v2, 0, sizeof(conn_info_v2));
size = sizeof(conn_info_v2);
conn_info_v2.ConnectionIndex = (ULONG)props->port;
conn_info_v2.Length = size;
conn_info_v2.SupportedUsbProtocols.Usb300 = 1;
if (!DeviceIoControl(handle, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX_V2, &conn_info_v2, size, &conn_info_v2, size, &size, NULL)) {
uprintf("Could not get node connection information (V2) for device '%s': %s", device_id, WindowsErrorString());
} else if (conn_info_v2.Flags.DeviceIsOperatingAtSuperSpeedOrHigher) {
props->speed = USB_SPEED_SUPER_OR_LATER;
} else if (conn_info_v2.Flags.DeviceIsSuperSpeedCapableOrHigher) {
props->is_LowerSpeed = TRUE;
}
}
out:
safe_closehandle(handle);
}
示例5: GetHandle
/*
* Open a drive or volume with optional write and lock access
* Return INVALID_HANDLE_VALUE (/!\ which is DIFFERENT from NULL /!\) on failure.
*/
static HANDLE GetHandle(char* Path, BOOL bWriteAccess, BOOL bLockDrive)
{
DWORD size;
HANDLE hDrive = INVALID_HANDLE_VALUE;
if (Path == NULL)
goto out;
hDrive = CreateFileA(Path, GENERIC_READ|(bWriteAccess?GENERIC_WRITE:0),
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
if (hDrive == INVALID_HANDLE_VALUE) {
uprintf("Could not open drive %s: %s\n", Path, WindowsErrorString());
goto out;
}
if (bWriteAccess) {
uprintf("Caution: Opened drive %s for write access\n", Path);
}
if ((bLockDrive) && (!DeviceIoControl(hDrive, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &size, NULL))) {
uprintf("Could not get exclusive access to device %s: %s\n", Path, WindowsErrorString());
safe_closehandle(hDrive);
goto out;
}
out:
return hDrive;
}
示例6: set_directory_timestamp
// Helper function to restore the timestamp on a directory
static void __inline set_directory_timestamp(char* path, LPFILETIME creation, LPFILETIME last_access, LPFILETIME modify)
{
HANDLE dir_handle = CreateFileU(path, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if ((dir_handle == INVALID_HANDLE_VALUE) || (!SetFileTime(dir_handle, creation, last_access, modify)))
uprintf(" Could not set timestamp for directory '%s': %s", path, WindowsErrorString());
safe_closehandle(dir_handle);
}
示例7: GetDriveLabel
/*
* Return the drive letter and volume label
*/
BOOL GetDriveLabel(DWORD DriveIndex, char* letter, char** label)
{
HANDLE hDrive, hPhysical;
DWORD size;
char AutorunPath[] = "#:\\autorun.inf", *AutorunLabel = NULL;
wchar_t wDrivePath[] = L"#:\\";
wchar_t wVolumeLabel[MAX_PATH+1];
static char VolumeLabel[MAX_PATH+1];
*label = STR_NO_LABEL;
hDrive = GetDriveHandle(DriveIndex, letter, FALSE, FALSE);
if (hDrive == INVALID_HANDLE_VALUE)
return FALSE;
safe_closehandle(hDrive);
AutorunPath[0] = *letter;
wDrivePath[0] = *letter;
// Try to read an extended label from autorun first. Fallback to regular label if not found.
// In the case of card readers with no card, users can get an annoying popup asking them
// to insert media. Use IOCTL_STORAGE_CHECK_VERIFY to prevent this
hPhysical = GetDriveHandle(DriveIndex, NULL, FALSE, FALSE);
if (DeviceIoControl(hPhysical, IOCTL_STORAGE_CHECK_VERIFY, NULL, 0, NULL, 0, &size, NULL))
AutorunLabel = get_token_data_file("label", AutorunPath);
else if (GetLastError() == ERROR_NOT_READY)
uprintf("Ignoring autorun.inf label for drive %c: %s\n", *letter,
(HRESULT_CODE(GetLastError()) == ERROR_NOT_READY)?"No media":WindowsErrorString());
safe_closehandle(hPhysical);
if (AutorunLabel != NULL) {
uprintf("Using autorun.inf label for drive %c: '%s'\n", *letter, AutorunLabel);
strncpy(VolumeLabel, AutorunLabel, sizeof(VolumeLabel));
safe_free(AutorunLabel);
*label = VolumeLabel;
} else if (GetVolumeInformationW(wDrivePath, wVolumeLabel, sizeof(wVolumeLabel),
NULL, NULL, NULL, NULL, 0) && *wVolumeLabel) {
wchar_to_utf8_no_alloc(wVolumeLabel, VolumeLabel, sizeof(VolumeLabel));
*label = VolumeLabel;
}
return TRUE;
}
示例8: UnMountISO
void UnMountISO(void)
{
PF_INIT_OR_OUT(DetachVirtualDisk, VirtDisk);
if ((mounted_handle == NULL) || (mounted_handle == INVALID_HANDLE_VALUE))
goto out;
pfDetachVirtualDisk(mounted_handle, DETACH_VIRTUAL_DISK_FLAG_NONE, 0);
safe_closehandle(mounted_handle);
out:
physical_path[0] = 0;
}
示例9: IsMediaPresent
/*
* GET_DRIVE_GEOMETRY is used to tell if there is an actual media
*/
BOOL IsMediaPresent(DWORD DriveIndex)
{
BOOL r;
HANDLE hPhysical;
DWORD size;
BYTE geometry[128];
hPhysical = GetPhysicalHandle(DriveIndex, FALSE, FALSE);
r = DeviceIoControl(hPhysical, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
NULL, 0, geometry, sizeof(geometry), &size, NULL) && (size > 0);
safe_closehandle(hPhysical);
return r;
}
示例10: ResetDevice
/*
* Cycle port (reset) the selected device
*/
BOOL ResetDevice(int index)
{
static uint64_t LastReset = 0;
BOOL r = FALSE;
HANDLE handle = INVALID_HANDLE_VALUE;
DWORD size;
USB_CYCLE_PORT_PARAMS cycle_port;
// Wait at least 10 secs between resets
if (GetTickCount64() < LastReset + 10000ULL) {
uprintf("You must wait at least 10 seconds before trying to reset a device");
return FALSE;
}
if (DriveHub.String[index] == NULL) {
uprintf("The device you are trying to reset does not appear to be a USB device...");
return FALSE;
}
LastReset = GetTickCount64();
handle = CreateFileA(DriveHub.String[index], GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (handle == INVALID_HANDLE_VALUE) {
uprintf("Could not open %s: %s", DriveHub.String[index], WindowsErrorString());
goto out;
}
size = sizeof(cycle_port);
memset(&cycle_port, 0, size);
cycle_port.ConnectionIndex = DrivePort[index];
uprintf("Cycling port %d (reset) on %s", DrivePort[index], DriveHub.String[index]);
// As per https://msdn.microsoft.com/en-us/library/windows/hardware/ff537340.aspx
// IOCTL_USB_HUB_CYCLE_PORT is not supported on Windows 7, Windows Vista, and Windows Server 2008
if (!DeviceIoControl(handle, IOCTL_USB_HUB_CYCLE_PORT, &cycle_port, size, &cycle_port, size, &size, NULL)) {
uprintf(" Failed to cycle port: %s", WindowsErrorString());
goto out;
}
uprintf("Please wait for the device to re-appear...");
r = TRUE;
out:
safe_closehandle(handle);
return r;
}
示例11: GetDriveLabel
/*
* Return the drive letter and volume label
* If the drive doesn't have a volume assigned, space is returned for the letter
*/
BOOL GetDriveLabel(DWORD DriveIndex, char* letters, char** label)
{
HANDLE hPhysical;
DWORD size;
char AutorunPath[] = "#:\\autorun.inf", *AutorunLabel = NULL;
wchar_t wDrivePath[] = L"#:\\";
wchar_t wVolumeLabel[MAX_PATH+1];
static char VolumeLabel[MAX_PATH+1];
*label = STR_NO_LABEL;
if (!GetDriveLetters(DriveIndex, letters))
return FALSE;
if (letters[0] == 0) {
// Drive without volume assigned - always enabled
return TRUE;
}
// We only care about an autorun.inf if we have a single volume
AutorunPath[0] = letters[0];
wDrivePath[0] = letters[0];
// Try to read an extended label from autorun first. Fallback to regular label if not found.
// In the case of card readers with no card, users can get an annoying popup asking them
// to insert media. Use IOCTL_STORAGE_CHECK_VERIFY to prevent this
hPhysical = GetPhysicalHandle(DriveIndex, FALSE, FALSE);
if (DeviceIoControl(hPhysical, IOCTL_STORAGE_CHECK_VERIFY, NULL, 0, NULL, 0, &size, NULL))
AutorunLabel = get_token_data_file("label", AutorunPath);
else if (GetLastError() == ERROR_NOT_READY)
uprintf("Ignoring autorun.inf label for drive %c: %s\n", letters[0],
(HRESULT_CODE(GetLastError()) == ERROR_NOT_READY)?"No media":WindowsErrorString());
safe_closehandle(hPhysical);
if (AutorunLabel != NULL) {
uprintf("Using autorun.inf label for drive %c: '%s'\n", letters[0], AutorunLabel);
safe_strcpy(VolumeLabel, sizeof(VolumeLabel), AutorunLabel);
safe_free(AutorunLabel);
*label = VolumeLabel;
} else if (GetVolumeInformationW(wDrivePath, wVolumeLabel, ARRAYSIZE(wVolumeLabel),
NULL, NULL, NULL, NULL, 0) && *wVolumeLabel) {
wchar_to_utf8_no_alloc(wVolumeLabel, VolumeLabel, sizeof(VolumeLabel));
*label = VolumeLabel;
}
return TRUE;
}
示例12: GetDriveSize
/*
* Return the drive size
*/
uint64_t GetDriveSize(DWORD DriveIndex)
{
BOOL r;
HANDLE hPhysical;
DWORD size;
BYTE geometry[256];
PDISK_GEOMETRY_EX DiskGeometry = (PDISK_GEOMETRY_EX)(void*)geometry;
hPhysical = GetPhysicalHandle(DriveIndex, FALSE, FALSE);
if (hPhysical == INVALID_HANDLE_VALUE)
return FALSE;
r = DeviceIoControl(hPhysical, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
NULL, 0, geometry, sizeof(geometry), &size, NULL);
safe_closehandle(hPhysical);
if (!r || size <= 0)
return 0;
return DiskGeometry->DiskSize.QuadPart;
}
示例13: FlushDrive
/*
* Flush file data
*/
static BOOL FlushDrive(char drive_letter)
{
HANDLE hDrive = INVALID_HANDLE_VALUE;
BOOL r = FALSE;
char logical_drive[] = "\\\\.\\#:";
logical_drive[4] = drive_letter;
hDrive = CreateFileA(logical_drive, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hDrive == INVALID_HANDLE_VALUE) {
uprintf("Failed to open %c: for flushing: %s\n", drive_letter, WindowsErrorString());
goto out;
}
r = FlushFileBuffers(hDrive);
if (r == FALSE)
uprintf("Failed to flush %c: %s\n", drive_letter, WindowsErrorString());
out:
safe_closehandle(hDrive);
return r;
}
示例14: GetOpticalMedia
BOOL GetOpticalMedia(IMG_SAVE* img_save)
{
static char str[MAX_PATH];
static char label[33];
int k;
BYTE geometry[256], *buffer = NULL;
PDISK_GEOMETRY_EX DiskGeometry = (PDISK_GEOMETRY_EX)(void*)geometry;
DWORD i, j, size, datatype;
HDEVINFO dev_info = NULL;
SP_DEVINFO_DATA dev_info_data;
SP_DEVICE_INTERFACE_DATA devint_data;
PSP_DEVICE_INTERFACE_DETAIL_DATA_A devint_detail_data;
HANDLE hDrive = INVALID_HANDLE_VALUE;
LARGE_INTEGER li;
dev_info = SetupDiGetClassDevsA(&_GUID_DEVINTERFACE_CDROM, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (dev_info == INVALID_HANDLE_VALUE) {
uprintf("SetupDiGetClassDevs (Interface) failed: %s\n", WindowsErrorString());
return FALSE;
}
dev_info_data.cbSize = sizeof(dev_info_data);
for (i = 0; SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data); i++) {
memset(str, 0, sizeof(str));
if (!SetupDiGetDeviceRegistryPropertyU(dev_info, &dev_info_data, SPDRP_FRIENDLYNAME,
&datatype, (LPBYTE)str, sizeof(str), &size)) {
uprintf("SetupDiGetDeviceRegistryProperty (Friendly Name) failed: %s\n", WindowsErrorString());
static_strcpy(str, "Generic Optical Drive");
}
uprintf("Found '%s' optical device", str);
devint_data.cbSize = sizeof(devint_data);
devint_detail_data = NULL;
for (j = 0; ; j++) {
safe_closehandle(hDrive);
safe_free(devint_detail_data);
safe_free(buffer);
if (!SetupDiEnumDeviceInterfaces(dev_info, &dev_info_data, &_GUID_DEVINTERFACE_CDROM, j, &devint_data)) {
if (GetLastError() != ERROR_NO_MORE_ITEMS) {
uprintf("SetupDiEnumDeviceInterfaces failed: %s\n", WindowsErrorString());
}
break;
}
if (!SetupDiGetDeviceInterfaceDetailA(dev_info, &devint_data, NULL, 0, &size, NULL)) {
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
devint_detail_data = (PSP_DEVICE_INTERFACE_DETAIL_DATA_A)calloc(1, size);
if (devint_detail_data == NULL) {
uprintf("Unable to allocate data for SP_DEVICE_INTERFACE_DETAIL_DATA\n");
continue;
}
devint_detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A);
} else {
uprintf("SetupDiGetDeviceInterfaceDetail (dummy) failed: %s\n", WindowsErrorString());
continue;
}
}
if (devint_detail_data == NULL) {
uprintf("SetupDiGetDeviceInterfaceDetail (dummy) - no data was allocated\n");
continue;
}
if (!SetupDiGetDeviceInterfaceDetailA(dev_info, &devint_data, devint_detail_data, size, &size, NULL)) {
uprintf("SetupDiGetDeviceInterfaceDetail (actual) failed: %s\n", WindowsErrorString());
continue;
}
// Get the size of the inserted media (if any)
hDrive = CreateFileA(devint_detail_data->DevicePath, GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (hDrive == INVALID_HANDLE_VALUE)
continue;
if (!DeviceIoControl(hDrive, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
NULL, 0, geometry, sizeof(geometry), &size, NULL))
continue;
// Rewritable media usually has a one sector
if (DiskGeometry->DiskSize.QuadPart <= 4096)
continue;
// Read the label directly, since it's a massive PITA to get it from Windows
li.QuadPart = 0x8000LL;
buffer = malloc(2048);
if ((buffer != NULL) && (SetFilePointerEx(hDrive, li, NULL, FILE_BEGIN)) &&
ReadFile(hDrive, buffer, 2048, &size, NULL) && (size == 2048)) {
memcpy(label, &buffer[0x28], sizeof(label) - 1);
label[sizeof(label) - 1] = 0;
for (k = (int)strlen(label) - 1; (k >= 0) && (label[k] == 0x20); k--)
label[k] = 0;
img_save->Label = label;
}
static_strcpy(str, devint_detail_data->DevicePath);
img_save->DevicePath = str;
img_save->DeviceSize = DiskGeometry->DiskSize.QuadPart;
safe_closehandle(hDrive);
safe_free(devint_detail_data);
safe_free(buffer);
return TRUE;
}
}
return FALSE;
}
示例15: SaveIcon
/*
* Extract an icon set from the exe and save it as .ico
*/
static BOOL SaveIcon(const char* filename)
{
HGLOBAL res_handle;
HRSRC res;
WORD i;
BYTE* res_data;
DWORD res_size, Size, offset;
HANDLE hFile = INVALID_HANDLE_VALUE;
BOOL r = FALSE;
GRPICONDIR* icondir;
icondir = (GRPICONDIR*)GetResource(hMainInstance, MAKEINTRESOURCEA(IDI_ICON), _RT_GROUP_ICON, "icon", &res_size, FALSE);
hFile = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, CREATE_NEW, 0, 0);
if (hFile == INVALID_HANDLE_VALUE) {
uprintf("Unable to create icon '%s': %s.\n", filename, WindowsErrorString());
goto out;
}
// Write .ico header
if ((!WriteFile(hFile, icondir, 3*sizeof(WORD), &Size, NULL)) || (Size != 3*sizeof(WORD))) {
uprintf("Couldn't write icon header: %s.\n", WindowsErrorString());
goto out;
}
// Write icon data
offset = 3*sizeof(WORD) + icondir->idCount*sizeof(ICONDIRENTRY);
for (i=0; i<icondir->idCount; i++) {
// Write the common part of ICONDIRENTRY
if ( (!WriteFile(hFile, &icondir->idEntries[i], sizeof(GRPICONDIRENTRY)-sizeof(WORD), &Size, NULL))
|| (Size != sizeof(GRPICONDIRENTRY)-sizeof(WORD)) ) {
uprintf("Couldn't write ICONDIRENTRY[%d]: %s.\n", i, WindowsErrorString());
goto out;
}
res = FindResourceA(hMainInstance, MAKEINTRESOURCEA(icondir->idEntries[i].nID), _RT_ICON);
// Write the DWORD offset
if ( (!WriteFile(hFile, &offset, sizeof(offset), &Size, NULL)) || (Size != sizeof(offset)) ) {
uprintf("Couldn't write ICONDIRENTRY[%d] offset: %s.\n", i, WindowsErrorString());
goto out;
}
offset += SizeofResource(NULL, res);
}
for (i=0; i<icondir->idCount; i++) {
// Write icon data
res = FindResourceA(hMainInstance, MAKEINTRESOURCEA(icondir->idEntries[i].nID), _RT_ICON);
res_handle = LoadResource(NULL, res);
res_data = (BYTE*)LockResource(res_handle);
res_size = SizeofResource(NULL, res);
if ( (!WriteFile(hFile, res_data, res_size, &Size, NULL)) || (Size != res_size) ) {
uprintf("Couldn't write icon data #%d: %s.\n", i, WindowsErrorString());
goto out;
}
}
uprintf("Created: %s\n", filename);
r = TRUE;
out:
safe_closehandle(hFile);
return r;
}