A DLL has the following shared variables (I'm using MinGW):
int iCount __attribute__((section(".str"), shared)) = 0;
HANDLE hMainFile __attribute__((section(".shr"), shared)) = NULL;
HANDLE hProcess __attribute__((section(".shr"), shared)) = NULL;
and the global variable:
HANDLE hFile = NULL;
This is how I'm handling my DLL_PROCESS_ATTACH:
case DLL_PROCESS_ATTACH:
if(!iCount)
{
hMainFile = CreateFile("Hello.txt", GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
hFile = hMainFile;
hProcess = GetCurrentProcess();
iCount = 1;
}
else
{
DuplicateHandle(hProcess, hMainFile, GetCurrentProcess(), &hFile, 0, FALSE, DUPLICATE_SAME_ACCESS);
}
break;
As you can see, the first instance of the DLL will create the file and set the shared file handle. The rest of the DLL instances should duplicate the original file handle to one that is compatible for its instance. However, DuplicateHandle is always giving an error of "The handle is invalid". I'm confused because I don't know which handle it's talking about. I have confirmed that the shared variables are indeed the same between all instances. Can somebody tell me what I'm doing wrong here?