Externe dll will nicht so recht



  • Hallo ich versuche eine dll in mein Programm einzubinden, das sieht dann wie folgt aus:

    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    #include "AtUsbHid.h"
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        HINSTANCE hLib = NULL;
        hLib =LoadLibrary(AT_USB_HID_DLL);
        if(hLib == NULL){
            cout << "boing\n";
        }
        loadFuncPointers(hLib);
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    

    und das ist die AtUsbHid.h:

    #ifndef _ATUSBHID_H_
    #define _ATUSBHID_H_
    
    // Error codes.
    #define ERROR_USB_DEVICE_NOT_FOUND          0xE0000001
    #define ERROR_USB_DEVICE_NO_CAPABILITIES    0xE0000002
    
    // name of the DLL to be loaded
    #define AT_USB_HID_DLL "AtUsbHid"
    
    // Implement the DLL export/import mechanism and allow a C-written program
    // to use our DLL.
    #ifdef ATUSBHID_EXPORTS
    #define ATUSBHID_API extern "C" __declspec(dllexport)
    #else
    #define ATUSBHID_API extern "C" __declspec(dllimport)
    #endif
    
    // This macro function calls the C runtime's _beginthreadex function.
    // The C runtime library doesn't want to have any reliance on Windows' data
    // types such as HANDLE. This means that a Windows programmer needs to cast
    // values when using _beginthreadex. Since this is terribly inconvenient,
    // this macro has been created to perform the casting.
    typedef unsigned(__stdcall *PTHREAD_START)(void *);
    
    #define chBEGINTHREADEX(psa, cbStack, pfnStartAddr, \
       pvParam, fdwCreate, pdwThreadId)                 \
          ((HANDLE)_beginthreadex(                      \
             (void *)        (psa),                     \
             (unsigned)      (cbStack),                 \
             (PTHREAD_START) (pfnStartAddr),            \
             (void *)        (pvParam),                 \
             (unsigned)      (fdwCreate),               \
    (unsigned *)(pdwThreadId)))
    
    // Allow applications not built with Microsoft Visual C++ to link with our DLL.
    #define STDCALL __stdcall
    
    // These macros make calling our DLL functions through pointers easier.
    #define DECLARE_FUNCTION_POINTER(FUNC)  PF_##FUNC lp##FUNC=NULL;
    #define LOAD_FUNCTION_POINTER(DLL,FUNC) lp##FUNC = (PF_##FUNC)GetProcAddress(DLL, #FUNC);
    #define ADDR_CHECK(FUNC) if (lp##FUNC == NULL) {fprintf(stderr,"%s\n", "Error: Cannot get address of function."); return FALSE;}
    #define DYNCALL(FUNC) lp##FUNC
    
    ///////////////////////////////////////////////////////////////////////////////
    typedef BOOLEAN (STDCALL *PF_findHidDevice)(const UINT VendorID, const UINT ProductID);
    typedef void    (STDCALL *PF_closeDevice)(void);
    typedef BOOLEAN (STDCALL *PF_writeData)(UCHAR* buffer);
    typedef BOOLEAN (STDCALL *PF_readData)(UCHAR* buffer);
    typedef int     (STDCALL *PF_hidRegisterDeviceNotification)(HWND hWnd);
    typedef void    (STDCALL *PF_hidUnregisterDeviceNotification)(HWND hWnd);
    typedef int     (STDCALL *PF_isMyDeviceNotification)(DWORD dwData);
    typedef BOOLEAN (STDCALL *PF_startBootLoader)(void);
    typedef BOOLEAN (STDCALL *PF_setFeature)(UCHAR type,UCHAR direction, unsigned int length);
    ///////////////////////////////////////////////////////////////////////////////
    
    // Exported functions prototypes.
    
    ///////////////////////////////////////////////////////////////////////////////
    ATUSBHID_API BOOLEAN STDCALL findHidDevice(const UINT VendorID, const UINT ProductID);
    
    //  Closes the USB device and all handles before exiting the application.
    ATUSBHID_API void    STDCALL closeDevice(void);
    
    ATUSBHID_API BOOLEAN STDCALL writeData(UCHAR* buf);
    
    ATUSBHID_API BOOLEAN STDCALL readData(UCHAR* buffer);
    
    ATUSBHID_API int     STDCALL hidRegisterDeviceNotification(HWND hWnd);
    
    ATUSBHID_API void    STDCALL hidUnregisterDeviceNotification(HWND hWnd);
    
    ATUSBHID_API int     STDCALL isMyDeviceNotification(DWORD dwData);
    
    ATUSBHID_API BOOLEAN STDCALL setFeature(UCHAR type,UCHAR direction, unsigned int length);
    
    ///////////////////////////////////////////////////////////////////////////////
    
    #ifndef ATUSBHID_EXPORTS
    
    DECLARE_FUNCTION_POINTER(findHidDevice);
    DECLARE_FUNCTION_POINTER(closeDevice);
    DECLARE_FUNCTION_POINTER(writeData);
    DECLARE_FUNCTION_POINTER(readData);
    DECLARE_FUNCTION_POINTER(hidRegisterDeviceNotification);
    DECLARE_FUNCTION_POINTER(hidUnregisterDeviceNotification);
    DECLARE_FUNCTION_POINTER(isMyDeviceNotification);
    DECLARE_FUNCTION_POINTER(setFeature);
    
    // this function call all function available in the DLL *
    static bool loadFuncPointers(HINSTANCE hLib)
    {
        LOAD_FUNCTION_POINTER(hLib, findHidDevice);
        ADDR_CHECK(findHidDevice);
    
    	LOAD_FUNCTION_POINTER(hLib, closeDevice);
        ADDR_CHECK(closeDevice);
    
    	LOAD_FUNCTION_POINTER(hLib, writeData);
        ADDR_CHECK(writeData);
    
    	LOAD_FUNCTION_POINTER(hLib, readData);
        ADDR_CHECK(readData);
    
    	LOAD_FUNCTION_POINTER(hLib, hidRegisterDeviceNotification);
    	ADDR_CHECK(hidRegisterDeviceNotification);
    
    	LOAD_FUNCTION_POINTER(hLib, hidUnregisterDeviceNotification);
    	ADDR_CHECK(hidUnregisterDeviceNotification);
    
    	LOAD_FUNCTION_POINTER(hLib, isMyDeviceNotification);
    	ADDR_CHECK(isMyDeviceNotification);
    
    	LOAD_FUNCTION_POINTER(hLib, setFeature);
    	ADDR_CHECK(setFeature);
    
        return true;
    }
    
    #endif
    
    #endif  // _ATUSBHID_H_
    

    Compiliert wird ohne Problem. beim laufen gibt es dann folgende Ausgabe:
    boing
    Error: Cannot get address of function.
    Kann mir da jemand weiterhelfen? Programmier erst wieder seit kurzen C++ und das bissche Wissen,was ich mal hatte, ist wieder total eingerostet. *nerv*



  • Hallo,

    bei

    // name of the DLL to be loaded
    #define AT_USB_HID_DLL "AtUsbHid.dll" // <- .dll nicht vergessen
    

    Und du solltest sicherstellen, dass die Datei auch tatsächlich so heißt.

    gruß
    Martin



  • Is eine Header-Datei von Atmel. Deswegen ollte ich dadrin nicht rumfuschen. Hat aber eh nichts geholfen. Geht auch so nicht.



  • Wenn boing kommt hat Loadlibrary die dll nicht gefunden. Schreib da doch einfach mal den Namen (evtl samt Pfad) direkt rein. Dann sollte es gehen.
    Bei den vielen defines bekomme ich das kalte Grausen. 😉



  • Dieser Thread wurde von Moderator/in HumeSikkins aus dem Forum C++ in das Forum WinAPI verschoben.

    Im Zweifelsfall bitte auch folgende Hinweise beachten:
    C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?

    Dieses Posting wurde automatisch erzeugt.



  • Moin das war der entscheidende Tipp. Ich hatte es zwar schon vorher mit "./AtUsbHid.dll" versucht dabei aber nur die dll im Projekt- also Sourcecode-Ordner gehabt. Somit einmal in den Ordner mit der Binärdatei reinkopiert und es klappt.

    Bezüglich der defines: Das is schon ziemlich schlimm, aber du solltes mal die Atmel Firmware für den Micocontroller sehen. Da wird hin und her definiert...GRAUSAM. Was die sich dabei wohl gedacht haben.

    Bezüglich des Verschiebens; habs eigentlich ganz bewusst nicht in das WinApi-Forum gesetz, weil es ja keine Windows-Library ist, aber gut, wenn die Moderatoren meinen, es passt hier besser, ist mir das auch recht.

    Besten Dank nochmal!



  • Bzgl. WinAPI: Gibt es noch andere Platformen ausser Win32 wo eine *Windows-DLL* läuft!?
    Geschweige denn, wo es "LoadLibrary" gibt???

    Also "C++" ist mit Sicherheit das falscheste Forum überhaupt 😉



  • Nagut :p



  • Kannst ja mal schauen,
    vlt hilft dir Henkesoft's Dll-Programmiererung's Tutorial :

    http://www.henkessoft.de/C++/WinAPI/WinAPI Kapitel 1 bis 6/api6.htm


Anmelden zum Antworten