Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want plain C exports, use a C project not C++. C++ DLLs rely on name-mangling for all the C++isms (namespaces etc...). You can compile your code as C by going into your project settings under C/C++->Advanced, there is an option "Compile As" which cooresponds to the compiler switches /TP and /TC.</p> <h2>Exporting/Importing DLL Libs in VC++</h2> <p>What you really want to do is define a conditional macro in a header that will be included in all of the source files in your DLL project:</p> <pre><code>#ifdef LIBRARY_EXPORTS # define LIBRARY_API __declspec(dllexport) #else # define LIBRARY_API __declspec(dllimport) #endif </code></pre> <p>Then on a function that you want to be exported you use <code>LIBRARY_API</code>:</p> <pre><code>LIBRARY_API int GetCoolInteger(); </code></pre> <p>In your library build project create a define <code>LIBRARY_EXPORTS</code> this will cause your functions to be exported for your DLL build.</p> <p>Since <code>LIBRARY_EXPORTS</code> will not be defined in a project consuming the DLL, when that project includes the header file of your library all of the functions will be imported instead.</p> <p>If your library is to be cross-platform you can define LIBRARY_API as nothing when not on Windows:</p> <pre><code>#ifdef _WIN32 # ifdef LIBRARY_EXPORTS # define LIBRARY_API __declspec(dllexport) # else # define LIBRARY_API __declspec(dllimport) # endif #elif # define LIBRARY_API #endif </code></pre> <p>When using dllexport/dllimport you do not need to use DEF files, if you use DEF files you do not need to use dllexport/dllimport. The two methods accomplish the same task different ways, I believe that dllexport/dllimport is the recommended method out of the two.</p> <h2>Exporting unmangled functions from a C++ DLL for LoadLibrary/PInvoke</h2> <p>If you need this to use LoadLibrary and GetProcAddress, or maybe doing PInvoke from .NET you can use <code>extern "C"</code> inline with your dllexport. And since we are using GetProcAddress instead of dllimport we don't need to do the ifdef dance from above, just a simple dllexport:</p> <p>The Code:</p> <pre><code>#define EXTERN_DLL_EXPORT extern "C" __declspec(dllexport) EXTERN_DLL_EXPORT int getEngineVersion() { return 1; } EXTERN_DLL_EXPORT void registerPlugin(Kernel &amp;K) { K.getGraphicsServer().addGraphicsDriver( auto_ptr&lt;GraphicsServer::GraphicsDriver&gt;(new OpenGLGraphicsDriver()) ); } </code></pre> <p>And here's what the exports look like with Dumpbin /exports:</p> <pre><code> Dump of file opengl_plugin.dll File Type: DLL Section contains the following exports for opengl_plugin.dll 00000000 characteristics 49866068 time date stamp Sun Feb 01 19:54:32 2009 0.00 version 1 ordinal base 2 number of functions 2 number of names ordinal hint RVA name 1 0 0001110E getEngineVersion = @ILT+265(_getEngineVersion) 2 1 00011028 registerPlugin = @ILT+35(_registerPlugin) </code></pre> <p>So this code works fine:</p> <pre><code>m_hDLL = ::LoadLibrary(T"opengl_plugin.dll"); m_pfnGetEngineVersion = reinterpret_cast&lt;fnGetEngineVersion *&gt;( ::GetProcAddress(m_hDLL, "getEngineVersion") ); m_pfnRegisterPlugin = reinterpret_cast&lt;fnRegisterPlugin *&gt;( ::GetProcAddress(m_hDLL, "registerPlugin") ); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload