Archived

This topic is now archived and is closed to further replies.

O_o

Explanation

Recommended Posts

O_o    175
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.

Share this post


Link to post
Share on other sites
Nik02    4349
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]

Share this post


Link to post
Share on other sites
O_o    175
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]

Share this post


Link to post
Share on other sites
Nik02    4349
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).

Share this post


Link to post
Share on other sites
O_o    175
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]

Share this post


Link to post
Share on other sites
Martel    122
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]

Share this post


Link to post
Share on other sites
Mathematix    259
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.

Share this post


Link to post
Share on other sites
O_o    175
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

Share this post


Link to post
Share on other sites