Thread: need help converting c dll to cpp

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    63

    need help converting c dll to cpp

    Hi, im haveing a rather strange problem and im not sure whats causing it. Im building myself a plugin type program, and c plugins(.dlls) seem to load fine, but when i try to convert them to cpp plugins one particular function fails.

    here is the code for my "dummy" plugin.
    dll.h
    Code:
    #ifndef _DLL_H_
    #define _DLL_H_
    
    #if BUILDING_DLL
    # define DLLIMPORT __declspec (dllexport)
    #else /* Not BUILDING_DLL */
    # define DLLIMPORT __declspec (dllimport)
    #endif /* Not BUILDING_DLL */
    #include <windows.h>
    #include <cstdlib>
    #include <iostream>
    ///// DEFINE THE "REQUEST" FUNCTION WHICH IS USED TO RECIEVE THE FUNCTIONS FROM THE CORE AND OTHER MODULES
    typedef int(*defaultFunc)();
    typedef defaultFunc(*request)(char *, char *);
    ///// DEFINE FUNCTIONS THAT WE WANT TO USE
    typedef void(*logMessage)(char*);
    
    ///// DEFINE DLL FUNCTIONS
    
    DLLIMPORT char * getName();
    DLLIMPORT int initialize(request _req);
    
    #endif /* _DLL_H_ */
    the only thing ive changed is the c includes to the cpp includes, but it didnt solve the problem

    dllmain.cpp
    Code:
    /* Replace "dll.h" with the name of your header */
    #include "dll.h"
    
    ///// MAKE A POINTER TO OUR FUNCTIONS
    request _request;
    logMessage _logMessage;
    
    
    DLLIMPORT void doSomething()
    {
        _logMessage("Im a loaded DUMMY :D");
    }
    
    DLLIMPORT char * getName()
    {
        return("dummy");
    }
    
    ///// THIS FUNCTION IS CALLED AUTOMATICALLY WHEN LOADED, AND THE POINTER TO REQUEST() IS PASSED TO IT
    DLLIMPORT int initialize(request _req)
    {
        _request = _req; // NOW THE ADDRESS OF THE REQUEST FUNCTION IS STORED AND ANYTHING CAN USE IT
        _logMessage = (void (*)(char*))_request("core", "logMessage"); //STORE THE ADDRESS OF LOG MESSAGE
        doSomething();
    }
    
    
    
    BOOL APIENTRY DllMain (HINSTANCE hInst     /* Library instance handle. */ ,
                           DWORD reason        /* Reason this function is being called. */ ,
                           LPVOID reserved     /* Not used. */ )
    {
        switch (reason)
        {
          case DLL_PROCESS_ATTACH:
            break;
    
          case DLL_PROCESS_DETACH:
            break;
    
          case DLL_THREAD_ATTACH:
            break;
    
          case DLL_THREAD_DETACH:
            break;
        }
    
        /* Returns TRUE on success, FALSE on failure */
        return TRUE;
    }
    and this is the code which loads a dll
    Code:
    int loadModule(wxString path)
    {
        const char * cpath = path.c_str();
        HINSTANCE hInstLibrary = LoadLibrary(cpath);
        if(hInstLibrary == NULL)
        {
            wxString output;
            output.Printf("didnt Load:%d",GetLastError());
            logMessage(output);
            return 0;
        }
        logMessage(wxT("initdef"));
        int (*initialize)(int(*(*)(char *, char *))()) = NULL;
        logMessage(wxT("initeq"));
        initialize = (int(*)(int(*(*)(char *, char *))()))GetProcAddress(hInstLibrary, "initialize");
        logMessage(wxT("namedef"));
        char * (*getName)() = NULL;
        logMessage(wxT("nameeq"));
        getName = (char*(*)())GetProcAddress(hInstLibrary, "getName");
        logMessage(wxT("namecall"));
        if(!modLog.findMod((*getName)()))
        {
            logMessage(wxT("initcall"));
            (*initialize)(&request);
            logMessage(wxT("namecall"));
            modLog.addMod((*getName)(),hInstLibrary);
        }
        return 1;
    }
    its basically a fairly jumbled mess of function pointers, but it calls initialize() and getName() from the .dll. Using the c dll, this works perfectly, but fails using the cpp dll. The last successful call is the first logMessage(wxT("nameCall")), so it is the actual calling of the function which crashes the program.

    EDIT: also i forgot to mention that the function pointer is still null, meaning that getProcAddress failed, and also last error is 127.

    I pulled this off another forum with a similar problem, but ive got no clue what it means. Im using dev-cpp.
    Code:
    it means that it could not find the NtOpenSection() function in ntdll.dll. Procedure is basically the function in the dll. When you call GetProcAddress(handle,"NtOpenSection"), it means you ask Windows to search for the address of procedure ( GetProcAddress is Get Procedure Address ) NtOpenSection in the dll and return it if found.
    
    Maybe, your version of ntdll.dll doesn't implement that.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    To call a C function linked with C linkage in a C++ program, the prototype must have extern "C" around it.
    Code:
    extern "C" void c_function();
    
    /* alternative syntax */
    extern "C" {
        void func1();
        void func2();
    }
    Does that help?

    You often see, in header files:
    Code:
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    /* ... */
    
    #ifdef __cplusplus
    }
    #endif
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    63
    thanks that did it. I can finally stop banging my head against the table, such a seemingly simple problem has been gnawing at me for a day now. Anyway, it seems defining the functions like that is almost like a legacy support for c. What would be the proper way of exporting functions in a cpp dll. Thanks again.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's the accepted way to use C functions in C++ code. You see, the linker handles C and C++ functions differently (because of classes, polymorphism, and so on [edit] type nm ccode.a and nm cppcode.a and you'll see what I mean [/edit]), so if it's in C++ mode and gets passed a library with C linkage, it needs to be told as such.

    If you want to avoid all of this extern "C" stuff, compile the C source code as C++ in the first place, if possible. But personally I wouldn't bother. It's not like extern "C" is non-standard or anything, and if code is C, it should be compiled as C. Otherwise you might get warnings or errors about perfectly valid C code.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    63
    Yes i agree, c code is best left in c, but unfortunately i need to use wxWidgets for some of the dlls functionality, which means cpp. o well, this works perfectly, so if it aint broke dont fix it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 01-14-2008, 10:25 AM
  2. How to convert dll to cpp file
    By hYph3n in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-13-2006, 06:27 AM
  3. Converting exe to DLL
    By jamez05 in forum Windows Programming
    Replies: 0
    Last Post: 10-11-2005, 12:17 PM
  4. dll communicating between each other
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2005, 02:20 AM
  5. How to convert dll file to cpp files?
    By uday222 in forum C++ Programming
    Replies: 1
    Last Post: 06-18-2002, 12:32 AM