Explanation

Started by
8 comments, last by O_o 20 years, 7 months ago
Can someone please explain to me in some detail what the following error message means. I can''t seem to find anything wrong with my code but I''m not exactly sure what I''m looking for lol. Unhandled exception at 0x1002a558 (Mydll.dll) in Myapp.exe: 0xC0000005: Access violation writing location 0x00000001.
Advertisement
Check your pointer arithmetic.
You're trying to write to memory address 0x1 for some reason.

EDIT: typo...

[edited by - Nik02 on September 1, 2003 2:08:55 PM]

Niko Suni

So is the problem occuring in Mydll.dll or Myapp.exe? I also know I don't have any pointer initilized to 1...

[edited by - O_o on September 1, 2003 3:03:53 PM]
I believe that this occurs in the dll.
However, the real reason for the error can be at the exe (for example, it passing an invalid pointer to the dll, and the dll trying faithfully to use it).

Niko Suni

Ok I found my problem but doesn't make any sense to me.

I have this code

typedef bool (WINAPI *SET_HOOK) (HWND, int, int, int, int);SET_HOOK SetHook = NULL;int x1 = 1;int y1 = 0;int x2 = 0;int y2 = 96;void StartHooking(){	HWND hApp;	hApp = FindWindow(TARGET_WINDOW_NAME, NULL);	if(hApp != NULL)	{		if(!(*SetHook)(hApp, x1, y1, x2, y2))		{			MessageBox(hWnd, "Failed to set the hook.", "Error", MB_OK);		}		else		{			bHooked = true;		}	}	else	{		MessageBox(hWnd, "Failed to find window.", "Error", MB_OK);	}}


and this code in the dll

#define LOG      #pragma data_seg(".shared")HHOOK hHookGETMESSAGE = NULL;HWND hHookedWindow = NULL;HINSTANCE hDLL = NULL;int x1 = 0;int y1 = 0;int x2 = 0;int y2 = 0;#pragma data_seg()extern "C" bool WINAPI SetHook(HWND phHookedWindow, int px1, int py1, int px2, int py2){	DWORD threadID;	#ifdef LOG	FILE *pFile;        pFile = fopen("hook.log", "a");        fprintf(pFile, "SetHook\n");	fclose(pFile);	#endif	x1 = px1;	y1 = py1;	x2 = px2;	y2 = py2;	hHookedWindow = phHookedWindow;	threadID = GetWindowThreadProcessId(hHookedWindow, NULL);	if(threadID == 0)	{		return false;	}	hHookGETMESSAGE = SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, hDLL, threadID);	if(hHookGETMESSAGE == NULL)	{		return false;	}	return true;} 


and the line that has problems is the "x1 = px1;" in the dll. I don't really get why though lol. My def file has the function exported and the section read, write and shared.

[edited by - O_o on September 1, 2003 3:49:20 PM]

[edited by - O_o on September 1, 2003 3:49:54 PM]

[edited by - O_o on September 1, 2003 3:51:49 PM]
I''ll get back to you tomorrow on this. Now i have really got to go to sleep and my brain doesn''t work too bright this late anyway

Niko Suni

Try changing this:
typedef bool (WINAPI *SET_HOOK) (HWND, int, int, int, int); 


To this:

typedef extern "C" bool (WINAPI *SET_HOOK) (HWND, int, int, int, int); 


Also, what is #pragma data_seg(".shared") for?



[edited by - Martel on September 2, 2003 6:07:27 AM]
Didn''t help =(
Very naughty! You''re not checking for successful allocation of pFile.

This error is a classic exception raised for failed pointer arithmetic and has nothing to do with "x1 = px1;" in the dll.

Make sure you check for successful allocation of pFile with something like (if not exactly the same as)

FILE *pFile = NULL;

if((pFile = fopen("hook.log", "a")) == NULL)
MessageBox(NULL, "Failed to open pFile!", "Allocation Failure", MB_OK);

I have not compiled this, so be careful to check it!

When using pointers, always check for successful allocation.
I realize it isn''t exactly sound programming practice to not check for proper allocation of a pointer but in this case it is not my problem. I get the exact same error when ''LOG'' is not defined and the logging code doesn''t even get compiled. The reason I think the line that is giving me problems is the "x1 = px1;" line is because when I changed the value of x1 passed to the function my error message changes from...

Unhandled exception at 0x1002a558 (Mydll.dll) in Myapp.exe: 0xC0000005: Access violation writing location 0x00000001.

to...

Unhandled exception at 0x1002a558 (Mydll.dll) in Myapp.exe: 0xC0000005: Access violation writing location ''x1''

but it makes no sense to me =p

This topic is closed to new replies.

Advertisement