Ok, I just noticed this bug in some code I had written a while ago, only I can't tell where I went wrong. Basically what seems to be happening is that Windows is reassinging an hFile I already have open for no apparent reason. Here's my code:
Code:
BOOL OpenFileWithDialog(FILEINFO * fileInfo, HWND hWnd, char * filter, int index, char * initialDir, char * title, DWORD attributeFlags)
{

	char filePath[MAX_PATH] = "\0";
	char fileName[MAX_PATH] = "\0";
	char * mergedString;
	HANDLE oldHFile = fileInfo->hFile;

	OPENFILENAME ofn = {0};

	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.hwndOwner = hWnd;
	ofn.lpstrFilter = filter;
	ofn.lpstrCustomFilter = NULL;
	ofn.nFilterIndex = index;
	ofn.lpstrFile = filePath;
	ofn.nMaxFile = MAX_PATH;
	ofn.lpstrFileTitle = fileName;
	ofn.nMaxFileTitle = 255;
	ofn.lpstrInitialDir = initialDir;
	ofn.lpstrTitle = title;
	ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
	ofn.lpstrDefExt = NULL;

	if(!GetOpenFileName(&ofn))
		return 1;

	if(!strcmp(ofn.lpstrFile, fileInfo->filePath))
	{
		if(MessageBox(hWnd, "File already open. Reload and lose changes since last save?", "Information", MB_YESNO | MB_ICONINFORMATION) == IDYES)
			CloseHandle(fileInfo->hFile);
		else
			return 1;
	}
	
	fileInfo->hFile = CreateFile(
						ofn.lpstrFile,
						GENERIC_READ | GENERIC_WRITE,
						FILE_SHARE_READ,
						(LPSECURITY_ATTRIBUTES) NULL,
						OPEN_EXISTING,
						attributeFlags,
						(HANDLE) NULL);

	if(fileInfo->hFile == INVALID_HANDLE_VALUE)
	{
		fileInfo->hFile = oldHFile;
		
		mergedString = StringMerge("Couldn't open file: ", &fileName, "\0");
		ErrorMessage(mergedString, "Couldn't open file. Please check that the file is not already in use.", NULL, MB_ICONERROR, 1);
		free((void *) mergedString);

		return 1;
	}

	StringCbCopy(fileInfo->filePath, MAX_PATH, ofn.lpstrFile);

	StringCbCopy(fileInfo->fileName, MAX_PATH, ofn.lpstrFileTitle);

	fileInfo->fileType = GetFileType(fileInfo->hFile);

	fileInfo->fileSizeLow = GetFileSize(fileInfo->hFile, &fileInfo->fileSizeHigh);

	return 0;

}

BOOL DasmViewerLoadFile(char * tempFileFolder, DASMVIEWERINFO * dvp)
{

	char tempFileName[MAX_PATH];
	char * timeString;
	LPTSTR fileTestBuffer = MemAlloc(MAX_PATH * sizeof(TCHAR));
	LPTSTR fileTestPart;
	FILEINFO newFileInfo;
	RECT progressCords;
	HWND * progressWindow = MemAlloc(sizeof(HWND));

	memcpy(&newFileInfo, &dvp->fileInfo, sizeof(FILEINFO));

	if(OpenFileWithDialog(
		&newFileInfo,
		NULL,
		"All Files (*.*)\0*.*\0Binary Files (*.bin)\0*.BIN\0\0",
		1,
		"",
		NULL,
		FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN))
	{
		return TRUE;
	}
	else
	{
		DasmViewerCloseFile(dvp);
		memcpy(&dvp->fileInfo, &newFileInfo, sizeof(FILEINFO));
	}

	if(dvp->fileInfo.fileSizeHigh)
	{
		MessageBox(NULL, "File is too large to be opened in dissassembler.", "Warning", MB_ICONEXCLAMATION);
		CloseHandle(dvp->fileInfo.hFile);
		dvp->fileInfo.hFile = 0;
	}
	else
	{
		//Create a temporary file to hold changes until the file is saved

		for(;;)
		{
			StringCbCopy(dvp->tempFileInfo.filePath, MAX_PATH, tempFileFolder);

			timeString = TimeString();

			StringCbCopy(tempFileName, MAX_PATH, timeString);
			StringCbCat(tempFileName, MAX_PATH, ".tmp");

			if(!SearchPath(tempFileFolder, timeString, ".tmp", MAX_PATH, fileTestBuffer, &fileTestPart))
			{
				free(timeString);
				free(fileTestBuffer);
				break;
			}

			free(timeString);
		}

		if(strlen(tempFileName) + strlen(dvp->tempFileInfo.filePath) + 2 > MAX_PATH) // + 2 to account for the slash
		{
			CloseHandle(dvp->fileInfo.hFile);
			dvp->fileInfo.hFile = 0;

			MessageBox(NULL, "File path too deep to create temp file. Cannot start file mode.", "Warning", MB_ICONEXCLAMATION);
			return TRUE;
		}

		StringCbCat(dvp->tempFileInfo.filePath, MAX_PATH, "\\");
		StringCbCat(dvp->tempFileInfo.filePath, MAX_PATH, tempFileName);

		GetClientRect(dvp->hWnd, &progressCords);

		progressCords.left = progressCords.right / 2 - progressCords.right / 4;
		progressCords.right = progressCords.right / 2 + progressCords.right / 4;
		progressCords.top = progressCords.bottom / 2 - progressCords.bottom / 6;
		progressCords.bottom = progressCords.bottom / 2 + progressCords.bottom / 6;

		*progressWindow = CreateProgressWindow(dvp->hWnd, NULL, 0, &progressCords);

		if(!CopyFileEx(dvp->fileInfo.filePath, dvp->tempFileInfo.filePath, CopyProgressRoutine, progressWindow, NULL, COPY_FILE_FAIL_IF_EXISTS))
		{
			CloseHandle(dvp->fileInfo.hFile);
			dvp->fileInfo.hFile = 0;

			MessageBox(NULL, "Failed to create temp file. Cannot start file mode.", "Warning", MB_ICONEXCLAMATION);
			return TRUE;
		}

		dvp->tempFileInfo.hFile = CreateFile(
			(LPCTSTR) dvp->tempFileInfo.filePath,
			GENERIC_READ | GENERIC_WRITE,
			FILE_SHARE_READ,
			(LPSECURITY_ATTRIBUTES) NULL,
			OPEN_EXISTING,
			FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE,
			(HANDLE) NULL);

		if(dvp->tempFileInfo.hFile == INVALID_HANDLE_VALUE)
		{
			dvp->tempFileInfo.hFile = 0;

			CloseHandle(dvp->fileInfo.hFile);
			dvp->fileInfo.hFile = 0;

			MessageBox(NULL, "Failed to open temp file. Cannot start file mode.", "Warning", MB_ICONEXCLAMATION);
			return TRUE;
		}

		dvp->tempFileInfo.fileType = GetFileType(dvp->tempFileInfo.hFile);
		dvp->tempFileInfo.fileSizeLow = GetFileSize(dvp->tempFileInfo.hFile, &dvp->tempFileInfo.fileSizeHigh);

	}

	dvp->selectedAreaOriginIndex = 0;
	dvp->selectedAreaBeginIndex = 0;
	dvp->selectedAreaEndIndex = 0;

	dvp->offset = 0;

	InvalidateRect(dvp->hWnd, NULL, TRUE);
	UpdateWindow(dvp->hWnd);

	return FALSE;

}

void DasmViewerCloseFile(DASMVIEWERINFO * dvp)
{

	if(dvp->fileInfo.hFile)
		CloseHandle(dvp->fileInfo.hFile);

	if(dvp->tempFileInfo.hFile)
		CloseHandle(dvp->tempFileInfo.hFile);

	ZeroMemory(&dvp->fileInfo, sizeof(FILEINFO));
	ZeroMemory(&dvp->tempFileInfo, sizeof(FILEINFO));

	dvp->selectedAreaOriginIndex = 0;
	dvp->selectedAreaBeginIndex = 0;
	dvp->selectedAreaEndIndex = 0;

	dvp->offset = 0;

	InvalidateRect(dvp->hWnd, NULL, TRUE);
	UpdateWindow(dvp->hWnd);

	return;

}
To sumarize what happens, the first time DasmViewerLoadFile is called everything goes fine no matter what. However, if the user selects the file that is already open on the second call, then at the code I marked orange dvp->tempFileInfo.hFile will be set to the same value dvp->fileInfo.hFile already has. After that, functions passed the handle will operate on the the temp file, but now dvp->fileInfo.hFile has an hFile that points to the wrong file. To make matters worse, if you try to open a third file then the code I marked in red will trigger an invalid handle exeption since the same file handle is stored in both variables. I'm clueless as to what's going wrong here, can anyone help?