【问题标题】:Didn't work on Windows 7, but it works on Windows 8 dll不适用于 Windows 7,但它适用于 Windows 8 dll
【发布时间】:2015-07-07 11:46:02
【问题描述】:

我有两个 DLL 文件,A 和 B。

A 需要 B 用于 setWindowsHookEx()。

我用:

LoadLibrary(L"C:\\Sources\\TestDLL.dll") and GetProcAddress(hDll, "GetMsgProc")

当我尝试在 Windows 7 上运行我的程序时,GetProcAddress(hDll, "GetMsgProc") 返回错误。

GetLastError() 返回 122,但我认为这不是一个好的错误代码。

当我在 Windows 8 上运行我的程序时,一切正常。

当我更改GetProcAddress(hDll, "foo")中的函数调用时

typedef void(*foo)(); 只是创建消息框

在 Windows 7 和 Windows 8 上一切正常。

我认为我的问题是__stdcall,但我没有找到解决方案。

DLL 文件 A

typedef void(*foo)();

typedef LRESULT(_stdcall *LPGetMsgProc)(int nCode, WPARAM wParam, LPARAM lParam);


#define NOM_CLASSE_JAVA_TEST "Lcom/XXX/controller/fonctions/Fonctions;"

JNIEXPORT jboolean JNICALL Java_com_XXX_system_Jni_getVeille
    (JNIEnv *env, jclass)
{
    {
        HINSTANCE hDll = (HINSTANCE)LoadLibrary(L"C:\\Sources\\TestDLL.dll");
        CString str1("it ok");

        if (!hDll)
        {
            CString str5("error1");
            CString str4(GetLastError().ToString());
            MessageBox(NULL, str4, str5, MB_ICONERROR);
            return -1;
        }

        LPGetMsgProc pfnProc = (LPGetMsgProc)GetProcAddress(hDll, "GetMsgProc");

        if (!pfnProc)
        {
            CString str5("error2");
            CString str4(GetLastError().ToString());
            MessageBox(NULL, str4, str5, MB_ICONERROR);
            FreeLibrary(hDll);
            return -1;
        }

        // Setup global hook
        HHOOK hHook = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)pfnProc, hDll, 0);
        if (!hHook)
        {
            CString str5("hookeroor");
            CString str4(GetLastError().ToString());
            MessageBox(NULL, str4, str5, MB_ICONERROR);
            FreeLibrary(hDll);
            return -1;
        }

        while (TRUE)
        {
            // Check to see if any messages are waiting in the queue
            while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            {
                // Translate the message and dispatch it to WindowProc()
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }

            // If the message is WM_QUIT, exit the while loop
            if (msg.message == WM_QUIT)
                break;
        }
        return true;
    }
}

DLL 文件 B

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <stdio.h>
#include <string>
#include <conio.h>
#include <tchar.h>
#include <boost/interprocess/file_mapping.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
#include <fstream>
#include <cstddef>
#include <cstdio>    //std::remove
#include <vector>
#pragma comment(lib, "user32.lib")


extern "C" _declspec(dllexport) void foo(void)
{
    MessageBox(NULL, (LPCTSTR)"ok", (LPCTSTR)"ok", MB_ICONERROR);
}

//============================================================================
extern "C"
{
    _declspec(dllexport) LRESULT _stdcall GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
        MessageBox(NULL, (LPCTSTR)"ok", (LPCTSTR)"ok", MB_ICONERROR);
        nCode = 0;
        wParam = NULL;
        lParam = NULL;
    }
}
//============================================================================

我在 Windows 8 64 位和 Windows 7 32 位上运行我的程序。

我运行 Dependency Walker 并找到了名称 GetProcAddress(hDll, "_GetMsgProc@12");,但我的程序无法运行。

【问题讨论】:

  • 如果你在失败调用和GetLastError调用之间做任何涉及系统调用的事情,GetLastError返回的值可能已经失效。总是先打电话给GetLastError
  • 另外,你有没有超载GetLastError?你真的用 C++ 编程吗?没有特定于编译器的扩展?你还能如何调用GetLastError 返回的DWORD 上的方法?
  • 如果你必须投,那你就错了。删除您对SetWindowsHookEx 的调用中的强制转换,编译器将愉快地输出诊断信息。同样,您在MessageBox-调用中的LPCTSTR-casts 是一个错误。
  • 我用unicode编译,这部分文件是c++ "#ifdef __cplusplus"
  • 你错了。这并不是说演员阵容是不必要的。演员表是一个错误。您正在调用MessageBoxW,它需要两个const wchar_t* 类型的参数,但您正在传递const char[3] 类型的参数。删除所有演员表,并修复错误。

标签: c++ windows dll stdcall getprocaddress


【解决方案1】:

然后,代码或编译器必须具有与 Windows 7 或更早版本不兼容的 Windows 8 和更新的代码。

【讨论】:

    猜你喜欢
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多