Problem mit SHARED und dll-import

gamerfunkie

Erfahrenes Mitglied
Hallo,
ich habe folgenden Code um WindowsHooks in einer DLL zu benutzen:
dll.cpp:
Code:
#include "dll.h"


// Globale Variablen
HHOOK g_hMouseHook SHARED = NULL;    // Handle unseres Hooks (als "shared" Deklariert)
HINSTANCE g_hInst SHARED = NULL;     // Handle der DLL selbst

BOOL APIENTRY DllMain (HINSTANCE hInst     /* Library instance handle. */ ,
                       DWORD reason        /* Reason this function is being called. */ ,
                       LPVOID reserved     /* Not used. */ )
{
    g_hInst = hInst;
    return TRUE;
}

DLLIMPORT BOOL InstallHook()
{ 
    if(g_hMouseHook != NULL)
        return TRUE;

    g_hMouseHook = SetWindowsHookEx(WH_MOUSE, MouseProc, g_hInst, 0);
    if(g_hMouseHook == NULL)
        return FALSE;
        
    return TRUE;
}

DLLIMPORT BOOL UninstallHook()
{
    if(g_hMouseHook != NULL)
    {
        UnhookWindowsHookEx(g_hMouseHook);
        g_hMouseHook = NULL;
    }
    return TRUE;
}

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

    if(nCode == HC_ACTION)
    {
        if ( (wParam == WM_LBUTTONDOWN)||(wParam == WM_NCLBUTTONDOWN) )
        {     
                MOUSEHOOKSTRUCT *mhs = (MOUSEHOOKSTRUCT*)lParam;
                HWND caller = FindWindow("Pipette", NULL);
                if(caller != NULL)
                    PostMessage(caller, WM_USER+123, 0, MAKELPARAM(mhs->pt.x, mhs->pt.y));
        }
    }
    return CallNextHookEx(g_hMouseHook, nCode, wParam, lParam);
}

dll.h
Code:
#ifndef _DLL_H_
#define _DLL_H_

#include <windows.h>

#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */

// zur Deklaration von Variablen innerhalb eines "shared data segment"
#define SHARED __attribute__((section(".shr"), shared))

DLLIMPORT BOOL InstallHook();
DLLIMPORT BOOL UninstallHook();

LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam);

#endif /* _DLL_H_ */

Code:
Wenn ich das ganze kompiliere kommt aber folgender Fehler:
1>------ Build started: Project: hook_dll, Configuration: Debug Win32 ------
1>Compiling...
1>dll.cpp
1>d:\programmieren\c++\ws_hack\hook_dll\dll.cpp(5) : error C2146: syntax error : missing ';' before identifier '__attribute__'
1>d:\programmieren\c++\ws_hack\hook_dll\dll.cpp(5) : error C2065: 'shared' : undeclared identifier
1>d:\programmieren\c++\ws_hack\hook_dll\dll.cpp(5) : error C3861: 'section': identifier not found
1>d:\programmieren\c++\ws_hack\hook_dll\dll.cpp(5) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\programmieren\c++\ws_hack\hook_dll\dll.cpp(5) : error C2143: syntax error : missing ';' before '='
1>d:\programmieren\c++\ws_hack\hook_dll\dll.cpp(5) : error C2513: 'int' : no variable declared before '='
1>d:\programmieren\c++\ws_hack\hook_dll\dll.cpp(6) : error C2146: syntax error : missing ';' before identifier '__attribute__'
1>d:\programmieren\c++\ws_hack\hook_dll\dll.cpp(6) : error C3861: 'section': identifier not found
1>d:\programmieren\c++\ws_hack\hook_dll\dll.cpp(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\programmieren\c++\ws_hack\hook_dll\dll.cpp(6) : error C2374: '__attribute__' : redefinition; multiple initialization
1>        d:\programmieren\c++\ws_hack\hook_dll\dll.cpp(5) : see declaration of '__attribute__'
1>d:\programmieren\c++\ws_hack\hook_dll\dll.cpp(6) : error C2143: syntax error : missing ';' before '='
1>d:\programmieren\c++\ws_hack\hook_dll\dll.cpp(6) : error C2513: 'int' : no variable declared before '='
1>d:\programmieren\c++\ws_hack\hook_dll\dll.cpp(17) : error C2491: 'InstallHook' : definition of dllimport function not allowed
1>d:\programmieren\c++\ws_hack\hook_dll\dll.cpp(29) : error C2491: 'UninstallHook' : definition of dllimport function not allowed
1>d:\programmieren\c++\ws_hack\hook_dll\dll.cpp(48) : error C2664: 'FindWindowW' : cannot convert parameter 1 from 'const char [8]' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>Build log was saved at "file://d:\Programmieren\C++\ws_hack\hook_dll\Debug\BuildLog.htm"
1>hook_dll - 15 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Das sieht aus als hätte er ein Problem mit dem SHARED.
Aber das braucht man ja ...
KAnn mir jemand helfen?
 
Okay,
ich hab jetzt gerade die Fehler mit dem SHARED gelöst. ich denke es liegt daran das ich ein aneren Compiler (VS 2005) benutze, als derjenige von dem mein Code stammt.
statt:
Code:
HHOOK g_hMouseHook SHARED= NULL;    // Handle unseres Hooks (als "shared" Deklariert)
HINSTANCE g_hInst SHARED = NULL;     // Handle der DLL selbst

habe ich jetzt
Code:
#pragma comment(linker, "/SECTION:.shr,RWS")
#pragma data_seg(".shr")
HHOOK g_hMouseHook = NULL;    // Handle unseres Hooks (als "shared" Deklariert)
HINSTANCE g_hInst = NULL;     // Handle der DLL selbst
#pragma data_seg()

Es bleiben die Fehler:
Code:
1>d:\programmieren\c++\ws_hack\hook_dll\dll.cpp(19) : error C2491: 'InstallHook' : definition of dllimport function not allowed
1>d:\programmieren\c++\ws_hack\hook_dll\dll.cpp(31) : error C2491: 'UninstallHook' : definition of dllimport function not allowed
 
Ich habe exakt den gleichen Fehler "definition of dllimport function not allowed".
Habe wohl das gleiche Tutorial wie gamerfunkie gemacht.

Hat denn niemand eine Lösung für das Problem?
Ich verwende Visual Studio 2008.
 
Ich habe exakt den gleichen Fehler "definition of dllimport function not allowed".
Habe wohl das gleiche Tutorial wie gamerfunkie gemacht.

Hat denn niemand eine Lösung für das Problem?
Ich verwende Visual Studio 2008.
Beim Erstellen der DLL muss das Makro BUILDING_DLL definiert sein. (Projekteinstellungen -> C/C++ -> Präprozessor -> Präprozessordefinitionen)

Gruß
 
So?

bi6kjhfqvor092kzt.jpg


Immerhin wird jetzt richtig kompilliert.
Das zugehörige Programm läuft zwar noch nicht richtig,
aber der Fehler wird wohl woanders liegen.

Danke!
 
Leider kann ich nicht editieren, daher leider ein kleiner Doppelpost.

Mir ist jetzt klar, warum es jetzt funktioniert.
Durch die definition von BUILDING_DLL wird das erste define und nicht mehr das zweite genommen.
Aber dann könnte ich doch auch einfach das zweite define weglassen oder?

#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
 
Leider kann ich nicht editieren, daher leider ein kleiner Doppelpost.

Mir ist jetzt klar, warum es jetzt funktioniert.
Durch die definition von BUILDING_DLL wird das erste define und nicht mehr das zweite genommen.
Aber dann könnte ich doch auch einfach das zweite define weglassen oder?
Beim Erstellen der DLL muss __declspec (dllexport) verwendet werden, wenn man die DLL nutzen möchte muss __declspec (dllimport) verwendet werden. Du müßtest die Headerdatei immer editieren.

Gruß
 
Hi,
danke für das Tutorial, nur kann ich es nicht erstellen, irgendwie komme ich nicht weiter mit dem was geschrieben wurde, ich habe nicht die Dateien die beschrieben wurden, gibts das Beispielprojekt zum dowloaden?

Besten Dank
mfc_user
 
Zurück