Compile a DLL in C/C++, then call it from another program

前端 未结 5 1141
死守一世寂寞
死守一世寂寞 2020-11-27 09:12

I want to make a simple, simple DLL which exports one or two functions, then try to call it from another program... Everywhere I\'ve looked so far, is for complicated matter

相关标签:
5条回答
  • 2020-11-27 09:54

    There is but one difference. You have to take care or name mangling win C++. But on windows you have to take care about 1) decrating the functions to be exported from the DLL 2) write a so called .def file which lists all the exported symbols.

    In Windows while compiling a DLL have have to use

    __declspec(dllexport)

    but while using it you have to write __declspec(dllimport)

    So the usual way of doing that is something like

    #ifdef BUILD_DLL
    #define EXPORT __declspec(dllexport)
    #else
    #define EXPORT __declspec(dllimport)
    #endif
    

    The naming is a bit confusing, because it is often named EXPORT.. But that's what you'll find in most of the headers somwhere. So in your case you'd write (with the above #define)

    int DLL_EXPORT add.... int DLL_EXPORT mult...

    Remember that you have to add the Preprocessor directive BUILD_DLL during building the shared library.

    Regards Friedrich

    0 讨论(0)
  • 2020-11-27 10:03

    Regarding building a DLL using MinGW, here are some very brief instructions.

    First, you need to mark your functions for export, so they can be used by callers of the DLL. To do this, modify them so they look like (for example)

    __declspec( dllexport ) int add2(int num){
       return num + 2;
    }
    

    then, assuming your functions are in a file called funcs.c, you can compile them:

    gcc -shared -o mylib.dll funcs.c
    

    The -shared flag tells gcc to create a DLL.

    To check if the DLL has actually exported the functions, get hold of the free Dependency Walker tool and use it to examine the DLL.

    For a free IDE which will automate all the flags etc. needed to build DLLs, take a look at the excellent Code::Blocks, which works very well with MinGW.

    Edit: For more details on this subject, see the article Creating a MinGW DLL for Use with Visual Basic on the MinGW Wiki.

    0 讨论(0)
  • 2020-11-27 10:06

    For VB6:

    You need to declare your C functions as __stdcall, otherwise you get "invalid calling convention" type errors. About other your questions:

    can I take arguments by pointer/reference from the VB front-end?

    Yes, use ByRef/ByVal modifiers.

    Can the DLL call a theoretical function in the front-end?

    Yes, use AddressOf statement. You need to pass function pointer to dll before.

    Or have a function take a "function pointer" (I don't even know if that's possible) from VB and call it?)

    Yes, use AddressOf statement.

    update (more questions appeared :)):

    to load it into VB, do I just do the usual method (what I would do to load winsock.ocx or some other runtime, but find my DLL instead) or do I put an API call into a module?

    You need to decaler API function in VB6 code, like next:

    Private Declare Function SHGetSpecialFolderLocation Lib "shell32" _
       (ByVal hwndOwner As Long, _
        ByVal nFolder As Long, _
        ByRef pidl As Long) As Long
    
    0 讨论(0)
  • 2020-11-27 10:08

    Here is how you do it:

    In .h

    #ifdef BUILD_DLL
    #define EXPORT __declspec(dllexport)
    #else
    #define EXPORT __declspec(dllimport)
    #endif
    
    extern "C" // Only if you are using C++ rather than C
    {    
      EXPORT int __stdcall add2(int num);
      EXPORT int __stdcall mult(int num1, int num2);
    }
    

    in .cpp

    extern "C" // Only if you are using C++ rather than C
    {    
    EXPORT int __stdcall add2(int num)
    {
      return num + 2;
    }
    
    
    EXPORT int __stdcall mult(int num1, int num2)
    {
      int product;
      product = num1 * num2;
      return product;
    }
    }
    

    The macro tells your module (i.e your .cpp files) that they are providing the dll stuff to the outside world. People who incude your .h file want to import the same functions, so they sell EXPORT as telling the linker to import. You need to add BUILD_DLL to the project compile options, and you might want to rename it to something obviously specific to your project (in case a dll uses your dll).

    You might also need to create a .def file to rename the functions and de-obfuscate the names (C/C++ mangles those names). This blog entry might be an interesting launching off point about that.

    Loading your own custom dlls is just like loading system dlls. Just ensure that the DLL is on your system path. C:\windows\ or the working dir of your application are an easy place to put your dll.

    0 讨论(0)
  • 2020-11-27 10:11

    The thing to watch out for when writing C++ dlls is name mangling. If you want interoperability between C and C++, you'd be better off by exporting non-mangled C-style functions from within the dll.

    You have two options to use a dll

    • Either use a lib file to link the symbols -- compile time dynamic linking
    • Use LoadLibrary() or some suitable function to load the library, retrieve a function pointer (GetProcAddress) and call it -- runtime dynamic linking

    Exporting classes will not work if you follow the second method though.

    0 讨论(0)
提交回复
热议问题