DLL einbinden

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • DLL einbinden

    Hallo zusammen


    ich habe folgendes Problem beim einbinden einer DLL. Wenn ich die DLL direkt in der Hauptfunktion _tmain() einbinde, dann funktioniert das wunderbar. Sobald ich aber eine eigene Klasse erstelle, ist es nicht möglich in dieser Klasse die DLL einzubinden. Die DLL verwendet den Namespace "MyDll".

    Das Beispiel von main.cpp funktioniert wunderbar:

    main.cpp

    Quellcode

    1. #define LOAD_FUNCTION_POINTER
    2. #include "MyDllObjectDefs.h"
    3. void *notifyFunc( MyDll::NotificationType notificationType,
    4. void *data)
    5. {
    6. return NULL;
    7. }
    8. int clientId;
    9. int _tmain(int argc, _TCHAR* argv[])
    10. {
    11. bool res;
    12. res = MyDll::loadMyDll("MyDll.dll");
    13. if (res) {
    14. clientId = MyDll::startMyDllClient("192.168.1.20", 8080, notifyFunc, MyDll::ProtocolType_MyDll);
    15. }
    16. return 0;
    17. }
    Alles anzeigen


    Das nun folgende leider nicht...

    main1.cpp

    Quellcode

    1. #include "MyObject.cpp"
    2. int _tmain(int argc, _TCHAR* argv[])
    3. {
    4. myObject *my = new MyObject();
    5. return 0;
    6. }


    myObject.h

    Quellcode

    1. #pragma once
    2. class myObject
    3. {
    4. public:
    5. myObject(void);
    6. ~myObject(void);
    7. protected:
    8. int m_clientId;
    9. };


    myObject.cpp

    Quellcode

    1. #include myObject.h
    2. #define LOAD_FUNCTION_POINTER
    3. #include "MyDllObjectDefs.h"
    4. void *notifyFunc( MyDll::NotificationType notificationType,
    5. void *data)
    6. {
    7. return NULL;
    8. }
    9. myObject::myObject(void)
    10. : m_clientId(0)
    11. {
    12. res = MyDll::loadMyDll("MyDll.dll");
    13. if (res) {
    14. m_clientId = MyDll::startMyDllClient("192.168.1.20", 8080, notifyFunc, MyDll::ProtocolType_MyDll);
    15. }
    16. }
    17. myObject::~myObject(void)
    18. {
    19. }
    Alles anzeigen


    Ich hoffe die Codeausschnitte reichen für eine Beurteilung. Besten Dank für eure Hinweise.

    triple-m
  • hallo phax

    es scheint jetzt mal soweit zu funktionieren. es liegt an dem

    Quellcode

    1. #define LOAD_FUNCTION_POINTER

    Ich habe es aus main1.cpp entfernt und verwende es nur noch in MyObject.cpp. Dafür kann ich jetzt in main1.cpp auch keine Funktionen der DLL aufrufen. Naja das ist ja nicht weiter schlimm, dann ist wenigstens die DLL schon mal sauber gekapselt.

    hier noch ein kleiner Auszug aus der MyDllInterface.h:
    Mit LOAD_FUNCTION_POINTER scheint er die Funktionen aus der DLL dem Programm verfügbar zu machen.

    Quellcode

    1. /*-----------------------------------------------------------------------------------
    2. define CREATE_FUNCTION_POINTER to get all function pointer
    3. -----------------------------------------------------------------------------------*/
    4. #ifdef LOAD_FUNCTION_POINTER
    5. #define CREATE_FUNCTION_POINTER
    6. #endif
    7. #ifdef CREATE_FUNCTION_POINTER
    8. #undef CREATE_FUNCTION_POINTER
    9. #ifdef WIN32
    10. typedef void (*SetLogFile) (const MyDllChar *);
    11. typedef void (*SetLogfileSize) (MyDllUInt);
    12. typedef void (*SetLogLevel) (const MyDllChar *,
    13. LogLevel);
    14. #endif // #ifdef WIN32
    15. typedef MyDllBool (*StopMyDllServer) (MyDllPort);
    16. typedef sendResults (*SendServer) (MyDllMsg &);
    17. typedef sendResults (*SendClient) (MyDllMsg &);
    18. typedef void (*AcceptClientSessions) (MyDllBool);
    19. typedef MyDllClientId (*StartMyDllClient) (const MyDllChar *,
    20. const MyDllInt,
    21. NotifyFunc &,
    22. const ProtocolType protocolType);
    23. typedef MyDllBool (*StopMyDllClient) (MyDllClientId);
    24. typedef MyDllBool (*StartMyDllServer) (const MyDllPort,
    25. NotifyFunc &,
    26. const ProtocolType protocolType);
    27. typedef void (*StopMyDll) (void) ;
    28. #ifdef WIN32
    29. SetLogFile fpSetLogFile = 0;
    30. SetLogfileSize fpSetLogfileSize = 0;
    31. SetLogLevel fpSetLogLevel = 0;
    32. #endif // #ifdef WIN32
    33. StartMyDllServer fpStartMyDllServer = 0;
    34. StopMyDllServer fpStopMyDllServer = 0;
    35. AcceptClientSessions fpAcceptClientSessions = 0;
    36. StartMyDllClient fpStartMyDllClient = 0;
    37. StopMyDllClient fpStopMyDllClient = 0;
    38. SendServer fpSendServer = 0;
    39. SendClient fpSendClient = 0;
    40. StopMyDll fpStopMyDll = 0;
    41. #endif //#ifdef CREATE_FUNCTION_POINTER
    42. /*-----------------------------------------------------------------------------------
    43. define LOAD_FUNCTION_POINTER to get the function pointer loader function
    44. -----------------------------------------------------------------------------------*/
    45. #ifdef LOAD_FUNCTION_POINTER
    46. #undef LOAD_FUNCTION_POINTER
    47. static HINSTANCE hMyLib = 0 ;
    48. static char *loadDllError = 0;
    49. bool loadMyDll(const char *libName)
    50. {
    51. hMyLib = LoadLibrary(libName);
    52. if (!hMyLib)
    53. {
    54. #ifdef __linux__
    55. loadDllError = dlerror();
    56. printf("dlerror=%s\n",loadDllError);
    57. #endif // #ifdef __linux__
    58. return false ;
    59. }
    60. // Get function pointers
    61. fpStartMyDllServer = (StartMyDllServer) GetProcAddress(hMyLib, "startMyDllServer");
    62. if (!fpStartMyDllServer) return false;
    63. fpStopMyDllServer = (StopMyDllServer) GetProcAddress(hMyLib, "stopMyDllServer");
    64. if (!fpStopMyDllServer) return false ;
    65. fpAcceptClientSessions = (AcceptClientSessions) GetProcAddress(hMyLib, "acceptClientSessions");
    66. if (!fpAcceptClientSessions) return false ;
    67. fpStartMyDllClient = (StartMyDllClient) GetProcAddress(hMyLib, "startMyDllClient");
    68. if (!fpStartMyDllClient) return false;
    69. fpStopMyDllClient = (StopMyDllClient) GetProcAddress(hMyLib, "stopMyDllClient");
    70. if (!fpStopMyDllClient) return false;
    71. fpSendServer = (SendServer) GetProcAddress(hMyLib, "sendServer");
    72. if (!fpSendServer) return false ;
    73. fpSendClient = (SendClient) GetProcAddress(hMyLib, "sendClient");
    74. if (!fpSendClient) return false ;
    75. fpStopMyDll = (StopMyDll) GetProcAddress(hMyLib, "stopMyDll");
    76. if (!fpStopMyDll) return false;
    77. #ifdef WIN32
    78. fpSetLogFile = (SetLogFile) GetProcAddress(hMyLib, "setLogFile");
    79. if (!fpSetLogFile) return false;
    80. fpSetLogfileSize = (SetLogfileSize) GetProcAddress(hMyLib, "setLogfileSize");
    81. if (!fpSetLogfileSize) return false;
    82. fpSetLogLevel = (SetLogLevel) GetProcAddress(hMyLib, "setLogLevel");
    83. if (!fpSetLogLevel) return false;
    84. #endif // #ifdef WIN32
    85. return true ;
    86. }
    87. #define sendServer(a) (MyDll::fpSendServer) (a)
    88. #define sendClient(a) (MyDll::fpSendClient) (a)
    89. #define stopMyDllClient(a) (MyDll::fpStopMyDllClient) (a)
    90. #define startMyDllClient(a,b,c,d) (MyDll::fpStartMyDllClient) (a,b,c,d)
    91. #define acceptClientSessions(a) (MyDll::fpAcceptClientSessions) (a)
    92. #define startMyDllServer(a,b,c) (MyDll::fpStartMyDllServer) (a,b,c)
    93. #define stopMyDllServer(a) (MyDll::fpStopMyDllServer) (a)
    94. #define stopMyDll() (MyDll::fpStopMyDll) ()
    95. #ifdef WIN32
    96. #define setLogFile(a) (MyDll::fpSetLogFile) (a)
    97. #define setLogfileSize(a) (MyDll::fpSetLogfileSize) (a)
    98. #define setLogLevel(a,b) (MyDll::fpSetLogLevel) (a,b)
    99. #endif // #ifdef WIN32
    100. #endif // #ifdef LOAD_FUNCTION_POINTER
    Alles anzeigen


    Jetzt steht aber das nächste Problem an. Der dritte Parameter von startMyDllClient ist ein Callback. Im Beispiel verwende ich dort eine Funktion (void * notifyFunc), die global definiert ist. Das funktioniert auch, aber wenn diese Funktion aufgerufen wird, dann habe ich kein Bezug zum Objekt. Also deklariere ich die Callback Funktion als Klassenmember. Doch als Callback kann ich nur eine statische Memberfunktion angeben und da bin ich wieder gleich weit. Unter newty.de/fpt/callback.html habe ich gelesen, dass ich eine Wrapper Funktion schreiben muss, doch irgendwie komm ich damit auch nicht weiter. Example A geht nicht, da ich die Funktionsparameter der Callback Funktion nicht verändern kann (die DLL ist nich von mir). Und Example B funktioniert zwar mit einer Instanz von myObject, aber was wenn ich mehr Instanzen erstellen will? Wie gehe ich am besten vor?

    Besten Dank, triple-m