0

我開始在32位平臺上做代碼並且代碼工作得很好,但是在64位中甚至無法啓動,我在尋找什麼是哪裏出現問題,爲什麼認爲不能在64下工作?「some.exe」中0x00FF1230未處理的異常:0xC0000005:執行位置0x00FF1230的訪問衝突

的錯誤是:

在0x00FF1230在「MyApp.exe將」未處理的異常:0000005:訪問衝突執行位置0x00FF1230

實際的代碼是:

// 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; 
    } 
} 
+2

嘗試使用調試器。 – 2013-02-18 03:26:17

回答

2

文字和數據部分被合併。

#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地址屬於.data段並且具有不允許執行代碼PAGE_WRITECOPY存儲器保護。

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 
[..] 
相關問題