0

我开始在 32 平台上编写代码并且代码工作得很好,但是在 64 位甚至无法启动,我正在寻找的是问题开始的地方,为什么认为在 64 下不起作用?

错误是:

“MyApp.exe”中 0x00FF1230 处未处理的异常:0xC0000005:访问冲突执行位置 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;
    }
}
4

1 回答 1

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
[..]
于 2013-02-18T07:46:26.577 回答