Arawn
Sep 27th, 2002, 05:24 PM
I'm really confused here. I've written a function that opens the .exe, goes to the end of the actual executable and then starts reading info I've appened onto it. First it reads how many files are attached, then the name and size of the first file. My problem is, I *HAVE* to know the name of the first file. But while detatching the rest of the files, the variable becomes unusable and causes my proggie to hang. *** is going wrong?
LPSTR RipFiles()
{
HANDLE hInputFile;
HANDLE hOutputFile;
BOOL bSuccess = FALSE;
LPSTR lpzMainFileName;
hInputFile = CreateFile(TEXT("ABSOTEK.EXE"), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if(hInputFile != INVALID_HANDLE_VALUE)
{
DWORD dwFileSize;
DWORD dwFilePoint;
dwFileSize = GetFileSize(hInputFile, NULL);
dwFilePoint = SetFilePointer(hInputFile, EXESIZE, NULL, FILE_BEGIN);
if(dwFilePoint != 0xFFFFFFFF)
{
PBYTE pszFileText; // reads files being ripped
pszFileText = (PBYTE)GlobalAlloc(GPTR, dwFileSize + 1);
if(pszFileText != NULL)
{
DWORD dwRead;
LPSTR lpzFileBuffer;
LPSTR lpzFileSize;
LPSTR lpzFileName;
DWORD dwFileCount;
lpzFileBuffer = (LPSTR)GlobalAlloc(GPTR, 2);
lpzFileSize = (LPSTR)GlobalAlloc(GPTR, 8);
lpzFileName = (LPSTR)GlobalAlloc(GPTR, _MAX_FNAME);
lpzMainFileName = (LPSTR)GlobalAlloc(GPTR, _MAX_FNAME);
// Get total number of files
while (strcmp(lpzFileBuffer, "*"))
{
if (ReadFile(hInputFile, lpzFileBuffer, 1, &dwRead, NULL))
{
if (strcmp(lpzFileBuffer, "*"))
strcat(lpzFileSize, lpzFileBuffer);
}
}
dwFileCount = atoi(lpzFileSize);
lpzFileBuffer[0] = 0;
lpzFileSize[0] = 0;
// Read main html file size
while (strcmp(lpzFileBuffer, "*"))
{
lpzFileBuffer[0] = 0;
if (ReadFile(hInputFile, lpzFileBuffer, 1, &dwRead, NULL))
{
if (strcmp(lpzFileBuffer, "*"))
strcat(lpzFileSize, lpzFileBuffer);
}
}
lpzFileBuffer[0] = 0;
// Read main html file name
while (strcmp(lpzFileBuffer, "*"))
{
lpzFileBuffer[0] = 0;
if (ReadFile(hInputFile, lpzFileBuffer, 1, &dwRead, NULL))
{
if (strcmp(lpzFileBuffer, "*"))
{
strcat(lpzMainFileName, lpzFileBuffer);
}
}
}
// Read main html file
if(ReadFile(hInputFile, pszFileText, atoi(lpzFileSize), &dwRead, NULL))
{
hOutputFile = CreateFile(lpzMainFileName, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hOutputFile != INVALID_HANDLE_VALUE)
{
DWORD dwWritten;
WriteFile(hOutputFile, pszFileText, dwRead, &dwWritten, NULL);
}
CloseHandle(hOutputFile);
}
if (dwFileCount > 1)
{
DWORD dwCurrent;
dwCurrent = 2;
while (dwCurrent <= dwFileCount)
{
lpzFileSize[0] = 0;
lpzFileName[0] = 0;
lpzFileBuffer[0] = 0;
// Read file size
while (strcmp(lpzFileBuffer, "*"))
{
lpzFileBuffer[0] = 0;
if (ReadFile(hInputFile, lpzFileBuffer, 1, &dwRead, NULL))
{
if (strcmp(lpzFileBuffer, "*"))
strcat(lpzFileSize, lpzFileBuffer);
}
}
lpzFileBuffer[0] = 0;
// Read file name
while (strcmp(lpzFileBuffer, "*"))
{
lpzFileBuffer[0] = 0;
if (ReadFile(hInputFile, lpzFileBuffer, 1, &dwRead, NULL))
{
if (strcmp(lpzFileBuffer, "*"))
{
strcat(lpzFileName, lpzFileBuffer);
}
}
}
// Read file
if(ReadFile(hInputFile, pszFileText, atoi(lpzFileSize), &dwRead, NULL))
{
hOutputFile = CreateFile(lpzFileName, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hOutputFile != INVALID_HANDLE_VALUE)
{
DWORD dwWritten;
WriteFile(hOutputFile, pszFileText, dwRead, &dwWritten, NULL);
}
CloseHandle(hOutputFile);
}
dwCurrent++;
MessageBox(NULL, lpzMainFileName, NULL, MB_OK);
}
GlobalFree(lpzFileSize);
GlobalFree(lpzFileName);
GlobalFree(lpzFileBuffer);
}
GlobalFree(pszFileText);
}
}
CloseHandle(hInputFile);
}
return lpzMainFileName;
}
The last place I can use the variable is where I have the MessageBox pop up and tell me. if I put the very same command after the } I got squat. Further more I get frustrated!
LPSTR RipFiles()
{
HANDLE hInputFile;
HANDLE hOutputFile;
BOOL bSuccess = FALSE;
LPSTR lpzMainFileName;
hInputFile = CreateFile(TEXT("ABSOTEK.EXE"), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if(hInputFile != INVALID_HANDLE_VALUE)
{
DWORD dwFileSize;
DWORD dwFilePoint;
dwFileSize = GetFileSize(hInputFile, NULL);
dwFilePoint = SetFilePointer(hInputFile, EXESIZE, NULL, FILE_BEGIN);
if(dwFilePoint != 0xFFFFFFFF)
{
PBYTE pszFileText; // reads files being ripped
pszFileText = (PBYTE)GlobalAlloc(GPTR, dwFileSize + 1);
if(pszFileText != NULL)
{
DWORD dwRead;
LPSTR lpzFileBuffer;
LPSTR lpzFileSize;
LPSTR lpzFileName;
DWORD dwFileCount;
lpzFileBuffer = (LPSTR)GlobalAlloc(GPTR, 2);
lpzFileSize = (LPSTR)GlobalAlloc(GPTR, 8);
lpzFileName = (LPSTR)GlobalAlloc(GPTR, _MAX_FNAME);
lpzMainFileName = (LPSTR)GlobalAlloc(GPTR, _MAX_FNAME);
// Get total number of files
while (strcmp(lpzFileBuffer, "*"))
{
if (ReadFile(hInputFile, lpzFileBuffer, 1, &dwRead, NULL))
{
if (strcmp(lpzFileBuffer, "*"))
strcat(lpzFileSize, lpzFileBuffer);
}
}
dwFileCount = atoi(lpzFileSize);
lpzFileBuffer[0] = 0;
lpzFileSize[0] = 0;
// Read main html file size
while (strcmp(lpzFileBuffer, "*"))
{
lpzFileBuffer[0] = 0;
if (ReadFile(hInputFile, lpzFileBuffer, 1, &dwRead, NULL))
{
if (strcmp(lpzFileBuffer, "*"))
strcat(lpzFileSize, lpzFileBuffer);
}
}
lpzFileBuffer[0] = 0;
// Read main html file name
while (strcmp(lpzFileBuffer, "*"))
{
lpzFileBuffer[0] = 0;
if (ReadFile(hInputFile, lpzFileBuffer, 1, &dwRead, NULL))
{
if (strcmp(lpzFileBuffer, "*"))
{
strcat(lpzMainFileName, lpzFileBuffer);
}
}
}
// Read main html file
if(ReadFile(hInputFile, pszFileText, atoi(lpzFileSize), &dwRead, NULL))
{
hOutputFile = CreateFile(lpzMainFileName, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hOutputFile != INVALID_HANDLE_VALUE)
{
DWORD dwWritten;
WriteFile(hOutputFile, pszFileText, dwRead, &dwWritten, NULL);
}
CloseHandle(hOutputFile);
}
if (dwFileCount > 1)
{
DWORD dwCurrent;
dwCurrent = 2;
while (dwCurrent <= dwFileCount)
{
lpzFileSize[0] = 0;
lpzFileName[0] = 0;
lpzFileBuffer[0] = 0;
// Read file size
while (strcmp(lpzFileBuffer, "*"))
{
lpzFileBuffer[0] = 0;
if (ReadFile(hInputFile, lpzFileBuffer, 1, &dwRead, NULL))
{
if (strcmp(lpzFileBuffer, "*"))
strcat(lpzFileSize, lpzFileBuffer);
}
}
lpzFileBuffer[0] = 0;
// Read file name
while (strcmp(lpzFileBuffer, "*"))
{
lpzFileBuffer[0] = 0;
if (ReadFile(hInputFile, lpzFileBuffer, 1, &dwRead, NULL))
{
if (strcmp(lpzFileBuffer, "*"))
{
strcat(lpzFileName, lpzFileBuffer);
}
}
}
// Read file
if(ReadFile(hInputFile, pszFileText, atoi(lpzFileSize), &dwRead, NULL))
{
hOutputFile = CreateFile(lpzFileName, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hOutputFile != INVALID_HANDLE_VALUE)
{
DWORD dwWritten;
WriteFile(hOutputFile, pszFileText, dwRead, &dwWritten, NULL);
}
CloseHandle(hOutputFile);
}
dwCurrent++;
MessageBox(NULL, lpzMainFileName, NULL, MB_OK);
}
GlobalFree(lpzFileSize);
GlobalFree(lpzFileName);
GlobalFree(lpzFileBuffer);
}
GlobalFree(pszFileText);
}
}
CloseHandle(hInputFile);
}
return lpzMainFileName;
}
The last place I can use the variable is where I have the MessageBox pop up and tell me. if I put the very same command after the } I got squat. Further more I get frustrated!