Reputation: 1255
I start to make code in 32 platform and code work great, but in 64 bit can't even start, what i'm searching for is where problem start, and why think don't work under 64?
The error is:
Unhandled exception at 0x00FF1230 in “MyApp.exe”: 0xC0000005: Access violation executing location 0x00FF1230
Actual code is:
// win.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
/* Keymapper v1.1
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details. */
// Make a really small executable
#pragma comment(linker,"/ENTRY:main") // Set entry point
// Merge all default sections into the .text (code) section.
#pragma comment(linker,"/MERGE:.rdata=.data")
#pragma comment(linker,"/MERGE:.text=.data")
#pragma comment(lib, "msvcrt.lib")
#if (_MSC_VER < 1300)
#pragma comment(linker,"/IGNORE:4078")
#pragma comment(linker,"/OPT:NOWIN98")
#endif
#pragma comment(linker, "/FILEALIGN:0x200")
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#define WH_KEYBOARD_LL 13
#include <windows.h>
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
BOOL WINAPI ConsoleEventHandler(DWORD dwCtrlType); // for graceful exit
DWORD dwMainThread = 0; // because apparently the console event handler thread for Ctrl+C is different from the main thread
int main(int args, char* argv[])
{
const char message[] =
"Caps Lock Remapper\n"
"Remaps Caps Lock to Backspace on the fly without rebooting.\n"
"Written by Petrus Theron http://freshcode.co/\n"
"Fork this on GitHub: http://github.com/pate/keymapper\n"
"\nPress Ctrl+C or close window to exit.\n";
DWORD count = 0;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
WriteConsoleA(hStdOut, message, sizeof(message)-2, &count, NULL);
if (!SetConsoleCtrlHandler(ConsoleEventHandler, TRUE))
return -1;
dwMainThread = GetCurrentThreadId();
// Retrieve the applications instance
HINSTANCE appInstance = GetModuleHandle(NULL);
// Attach global keyboard hook to capture keystrokes
HHOOK hHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, appInstance, 0);
if (!hHook)
return -2;
MSG msg;
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Clean up
UnhookWindowsHookEx(hHook);
SetConsoleCtrlHandler(ConsoleEventHandler, FALSE);
return 0;
}
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
KBDLLHOOKSTRUCT *pKeyBoard = (KBDLLHOOKSTRUCT *)lParam;
switch( pKeyBoard->vkCode )
{
case VK_CAPITAL:
{
switch (wParam)
{
case WM_KEYDOWN:
keybd_event(VK_BACK, 0x8e, 0, 0);
return 1;
case WM_KEYUP:
keybd_event(VK_BACK, 0x8e, KEYEVENTF_KEYUP, 0);
return 1;
}
}
default:
return CallNextHookEx( NULL, nCode, wParam, lParam );
}
return 0;
}
BOOL WINAPI ConsoleEventHandler(DWORD dwCtrlType)
{
switch(dwCtrlType)
{
case CTRL_C_EVENT:
case CTRL_CLOSE_EVENT:
PostThreadMessage(dwMainThread, WM_QUIT, NULL, NULL);
return TRUE;
default:
return FALSE;
}
}
Upvotes: 0
Views: 1926
Reputation: 2180
Text and data sections are merged.
#pragma comment(linker,"/MERGE:.text=.data")
Attempt to execute non-executable address 0000000000111ee6
(1078.1080): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
*** WARNING: Unable to verify checksum for test.exe
test!ILT+225(_main):
00111ee6 e955010000 jmp test!main (00112040)
00111ee6 address belongs to .data section and has PAGE_WRITECOPY memory protection which not allowed execute code.
0:000:x86> !address 00111ee6
Usage: Image
Allocation Base: 00100000
Base Address: 00111000
End Address: 00114000
Region Size: 00003000
Type: 01000000 MEM_IMAGE
State: 00001000 MEM_COMMIT
Protect: 00000008 PAGE_WRITECOPY
0:000:x86> !dh test
[...]
----- new -----
0000000000100000 image base
[...]
SECTION HEADER #2
.data name
2E3A virtual size
11000 virtual address
2C00 size of raw data
[..]
Upvotes: 2