0

I have two DLL files, A and B.

A needs B for setWindowsHookEx().

I use:

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

When I try to run my program on Windows 7, GetProcAddress(hDll, "GetMsgProc") returns an error.

GetLastError() returns 122, but I think it not a good error code.

When I run my program on Windows 8 everything works.

When I change the function call in GetProcAddress(hDll, "foo")

typedef void(*foo)(); just creates message box

Everything works on Windows 7 and Windows 8.

I think my problem is __stdcall, but I don't find the solution.

DLL file 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 file 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;
    }
}
//============================================================================

I run my program on Windows 8 64 bit and Windows 7 32 bit.

I run Dependency Walker and I found the name GetProcAddress(hDll, "_GetMsgProc@12");, but my program won't work.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gun95
  • 24
  • 5
  • If you do anything ahtat involves system calls between the failed call and the call to `GetLastError`, the value returned by `GetLastError` might have become invalid. Always call `GetLastError` before anything else. – Some programmer dude Jul 07 '15 at 11:53
  • Also, have you overloaded `GetLastError`? Are you really programming in C++? Without compiler-specific extensions? How else would you be able to call a method on the `DWORD` returned by [`GetLastError`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms679360%28v=vs.85%29.aspx)? – Some programmer dude Jul 07 '15 at 11:54
  • If you have to cast, you're doing it wrong. Remove the cast in your call to `SetWindowsHookEx`, and the compiler will happily output diagnostics. Likewise, your `LPCTSTR`-casts in your `MessageBox`-call is a bug. – IInspectable Jul 07 '15 at 11:59
  • i compile in unicode and this part of the file is c++ "#ifdef __cplusplus" – gun95 Jul 07 '15 at 12:00
  • @IInspectable yes the cast it not necessary i have add after for test – gun95 Jul 07 '15 at 12:02
  • 1
    You have it wrong. It's not that the cast isn't necessary. The cast is a **bug**. You are calling `MessageBoxW` that expects two parameters of type `const wchar_t*`, but you are passing arguments of type `const char[3]`. Remove **all** casts, and fix the bugs. – IInspectable Jul 07 '15 at 12:06
  • Hint: If you get warnings or even errors that can be removed through a cast, then you doing it wrong Casting away errors or warnings is almost always the wrong solution. – Some programmer dude Jul 07 '15 at 12:08
  • if i remove the cast fot messagebox i have 1 error MessageBox(HWND,LPCTSTR,LPCTSTR,UINT)' : impossible de convertir l'argument 2 de 'DWORD' en 'LPCTSTR' – gun95 Jul 07 '15 at 12:14
  • because i compile on UNICODE for JNI project – gun95 Jul 07 '15 at 12:15
  • Shouldn't you be using e.g. `_T("ok")` to get the right type of string literal? Because plain `"ok"` is a narrow-character string literal, always, and the wide-character functions needs a wide-character literal (e.g `L"ok"`). – Some programmer dude Jul 07 '15 at 12:20
  • ok but when i call `foo()` i already see `"ok" ` – gun95 Jul 07 '15 at 12:28
  • There is an old saying, that goes something like this: If you don't know, why your code works, it doesn't. – IInspectable Jul 07 '15 at 12:37
  • yes i don't know all in windows and programing but i know my problem is not here and i can't use `L(GetLastError())` or `_T` ,`_TEXT` in `MessageBox()` – gun95 Jul 07 '15 at 12:46
  • @JoachimPileborg yes i move `getlasterror` and he return 127 – gun95 Jul 07 '15 at 13:03
  • That's `ERROR_PROC_NOT_FOUND` ("The specified procedure could not be found.") which makes much more sense. – Some programmer dude Jul 07 '15 at 14:31

1 Answers1

-2

The code or the compiler then must have Windows 8 and more recent code that is not compatible with Windows 7 or older.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Star OS
  • 119
  • 1
  • 1
  • 15