System Error 998

The Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <windows.h>
#include <strsafe.h>
//-----------------
#include <fstream>
#include <string>
//debug - function retrieved from: http://msdn.microsoft.com/en-us/library/windows/desktop/ms680582(v=vs.85).aspx
void ErrorExit(LPTSTR lpszFunction) 
{ 
    // Retrieve the system error message for the last-error code

    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;
    DWORD dw = GetLastError(); 

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );

    // Display the error message and exit the process

    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
        (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR)); 
    StringCchPrintf((LPTSTR)lpDisplayBuf, 
        LocalSize(lpDisplayBuf) / sizeof(TCHAR),
        TEXT("%s failed with error %d: %s"), 
        lpszFunction, dw, lpMsgBuf); 
    MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 

    LocalFree(lpMsgBuf);
    LocalFree(lpDisplayBuf);
    ExitProcess(dw); 
}
//handle declarations
	HWND txtfld;
	HWND btn;
	HWND txtbx;
	HANDLE hfile;
//variable declarations
    std::string line;
    LPVOID* ntns;
	
/* This is where all the input to the window goes to */
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {

	char txtmem[20];
	switch(Message) {
	
    case WM_CREATE:
    	{
    		txtfld = CreateWindowA("STATIC", "    CommonwealthCore Database Application", WS_VISIBLE | WS_CHILD | WS_BORDER, 150, 20, 300, 20, hwnd, NULL, NULL, NULL);
    		txtfld = CreateWindowA("STATIC", "                     Find A Nation:", WS_VISIBLE | WS_CHILD | WS_BORDER, 20, 70, 250, 20, hwnd, NULL, NULL, NULL);
    		txtbx = CreateWindowA("EDIT", "NationShortName", WS_VISIBLE | WS_CHILD | WS_BORDER, 20, 110, 250, 20, hwnd, NULL, NULL, NULL);
    		btn = CreateWindowA("BUTTON", "SEARCH", WS_VISIBLE | WS_CHILD | WS_BORDER, 100, 150, 100, 30, hwnd, (HMENU) 1, NULL, NULL);
    		break;
    	}

	case WM_COMMAND:
		{
		
		switch (LOWORD (wParam) )
		{
			case 1:
				{
					int gwt1=0;
					char * txtmemptr = &txtmem[0];
					gwt1=GetWindowText(txtbx, &txtmem[0], 20);
					::MessageBeep(MB_ICONERROR);
					::MessageBoxA(hwnd, txtmemptr , "Searching For Nation:", MB_OK);
					hfile=CreateFile("nations.txt", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
					int x=ReadFile(hfile, ntns, 1000000, NULL, NULL);
					//DWORD xx=GetLastError();
					//::MessageBoxA(hwnd, (LPCSTR)xx, "-", MB_OK);
					//LPCSTR ntnst=(LPCSTR)ntns;
					ErrorExit("GetProcessId");
					//::MessageBoxA(hwnd, ntnst, "SearchData", MB_OK);
					break;
				}
				
		}
}
//[-snip-]
}


I am thrown the following system error:
Error
Process failed with error 998: Invalid access to memory location.

I know it's referring to these lines: (75 & 76)
1
2
hfile=CreateFile("nations.txt", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
					int x=ReadFile(hfile, ntns, 1000000, NULL, NULL);


I've been researching solutions and fixes all day, haven't found 1 useful article.

I think i'm formatting the file location wrong [for the HANDLE], but I've played around with it and got nothing else but error 998 (or error 6 [invalid handle] when I really messed up the file location format.

Is this the problem or am I doing something else wrong?

Thanks,
~Hom.
Last edited on
Where is this "nations.txt"? Since it's a relative path the file must exist at the current path.

I think i'm formatting the file location wrong
What does that mean?
You declare this as your buffer for ReadFile:
LPVOID* ntns;
But this isn't a buffer, it's just a pointer to some random chunk of memory that you don't own.

Then you attempt to read 1000000 bytes into this buffer without having actually allocated any memory:
int x=ReadFile(hfile, ntns, 1000000, NULL, NULL);

MSDN states:

lpBuffer [out]
A pointer to the buffer that receives the data read from a file or device.
This buffer must remain valid for the duration of the read operation. The caller must not use this buffer until the read operation is completed.


Since it looks like you're trying to read from a text file, maybe you should do something like this:

1
2
3
4
char* ntns = new char[1000000];
int x=ReadFile(hfile, static_cast<LPVOID>(ntns), 1000000, NULL, NULL);
// Do something with your buffer, then deallocate it properly
delete [] ntns;
Last edited on
Lodger:

Thanks, the code worked. Just my mind blanking out about buffers.

~Hom
Welcome :-)
Topic archived. No new replies allowed.