|
-
Sep 27th, 2002, 05:24 PM
#1
Thread Starter
Lively Member
Can't use my variable in the middle of my program! (LONG)
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?
Code:
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!
-
Sep 27th, 2002, 05:32 PM
#2
I'm too tired to really look at the code. But why don't you use real binary resources? Windows would do all this stuff for you then.
And GlobalAlloc is more or less deprecated, you should use HeapAlloc. Call GetProcessHeap to get a heap handle.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 27th, 2002, 05:35 PM
#3
Thread Starter
Lively Member
Because each time it get distrubuted there's going to be different html on the end of it.
-
Sep 27th, 2002, 06:34 PM
#4
Thread Starter
Lively Member
I converted it over to HeapAlloc and that didn't seem to make a difference.. I don't get it.. Nothing in the the block after "if (dwfileCount > 1)" even uses lpzMainFileName. So why does it get changed?
-
Sep 28th, 2002, 07:29 AM
#5
I didn't assume that the HeapAlloc thing would change anything. It's just that the difference between GlobalAlloc and LocalAlloc only made sense in 16-bit windows and with 32-bit windows they were deprecated and replaced by HeapAlloc.
Have you tried doing a debug run with that program? Debug it step by step and find out where the value gets changed.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 28th, 2002, 03:05 PM
#6
Thread Starter
Lively Member
Ahh.. Well, I don't think I can since I have to run the .asp script to append the files.. But I have moved the MessageBox to different places and narrowed it down to where it is in the code I posted. If I place it after the closing } to the while loop it doesn't work. It won't even pop up the message box just hangs.
-
Sep 28th, 2002, 03:20 PM
#7
Maybe the loop is infinite...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 28th, 2002, 03:25 PM
#8
Thread Starter
Lively Member
I would expect an infinate loop to repeatedly pop up the MessageBox, but it doesn't.. Also If I don't try to use lpzMainFileName the program continues to run. It's only hanging when I attempt to use it after the while loop... But I think I'm just going to have to take a different approach. One function to rip the main file and a second to take off the rest.
-
Sep 28th, 2002, 03:37 PM
#9
Why don't you have one block of code to do both. So far I see no difference between the blocks and it would make the code easier to read.
Explain again to me why you can't debug that app?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 28th, 2002, 03:56 PM
#10
Thread Starter
Lively Member
The only difference between the 2 chunks is I need to know the name of the first file later on in the program, so I use a different variable for the filename. The rest of the file(s) I only need to know the name when extracting them.
And as far as debugging, before I can run the program I have to copy it over to my web server and run an asp script that appends the files to the end of it.
-
Sep 28th, 2002, 04:58 PM
#11
Can't you for debug purposes manually append some files and then run it through the debugger?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 28th, 2002, 05:17 PM
#12
Thread Starter
Lively Member
alright forgive if this dumb, but I've only been using msvc for a little over a month. But to do that I just hit F11 in the IDE? If so I tried that after copying the .exe back into the project debug directory and it gives me an access violation at the first CreateWindow(). And I tried to set a break point at the while loop in question and it still did it when I tried to run it in the IDE. But if I'm doing this wrong please tell me
-
Sep 28th, 2002, 06:51 PM
#13
Usually you set a breakpoint and start the app by hitting F5. But if it crashes this may be because the file appending messes up the debug information.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 28th, 2002, 06:53 PM
#14
In this case you might want to call AllocConsole which creates a console window for you and then write much debug info to it - like variable values at many code points etc. . You can use the usual CRT or iostream functions/objects for this.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 29th, 2002, 01:40 PM
#15
Fanatic Member
Arawn, If you declare code inside an if statement, it will be destroyed as soon as you leave that if statement, kind of like function scoping...
PHP Code:
int i = 0;
if (!i) {
int i = 99; // local i
int j = 10; // local j
}
// i is still 0
// j is now gone
The loop is going infinite probably cuz when you send in the lpstrFilename variable it cant find the null terminator, all c/c++ strings should be terminated by null. But since you are passing in invalid memory location, you never know what to expect.
-
Sep 29th, 2002, 01:41 PM
#16
Fanatic Member
Originally posted by MoMad
Arawn, If you declare code inside an if statement, it will be destroyed as soon as you leave that if statement, kind of like function scoping...
PHP Code:
int i = 0;
if (!i) {
int i = 99; // local i
int j = 10; // local j
}
// i is still 0
// j is now gone
[...]
BTW: Many compilers dont support this C++ standard just yet, msvc partially supports it, maybe thats why you got this?
-
Sep 29th, 2002, 01:48 PM
#17
Right, you should zero out everything you allocate. Use the handy ZeroOut API function.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|