Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 120791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:52:02+00:00 2026-05-11T03:52:02+00:00

I’d like a simple example of exporting a function from a C++ Windows DLL.

  • 0

I’d like a simple example of exporting a function from a C++ Windows DLL.

I’d like to see the header, the .cpp file, and the .def file (if absolutely required).

I’d like the exported name to be undecorated. I’d like to use the most standard calling convention (__stdcall?). I’d like the use __declspec(dllexport) and not have to use a .def file.

For example:

  //header   extern 'C'   {    __declspec(dllexport) int __stdcall foo(long bar);   }    //cpp   int __stdcall foo(long bar)   {     return 0;   } 

I’m trying to avoid the linker added underscores and/or numbers (byte counts?) to the name.

I’m OK with not supporting dllimport and dllexport using the same header. I don’t want any information about exporting C++ class methods, just c-style global functions.

UPDATE

Not including the calling convention (and using extern 'C') gives me the export names as I like, but what does that mean? Is whatever default calling convention I’m getting what pinvoke (.NET), declare (vb6), and GetProcAddress would expect? (I guess for GetProcAddress it would depend on the function pointer the caller created).

I want this DLL to be used without a header file, so I don’t really need the a lot of the fancy #defines to make the header usable by a caller.

I’m OK with an answer being that I have to use a *.def file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:52:03+00:00Added an answer on May 11, 2026 at 3:52 am

    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 corresponds to the compiler switches /TP and /TC.

    If you still want to use C++ to write the internals of your lib but export some functions unmangled for use outside C++, see the second section below.

    Exporting/Importing DLL Libs in VC++

    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:

    #ifdef LIBRARY_EXPORTS #    define LIBRARY_API __declspec(dllexport) #else #    define LIBRARY_API __declspec(dllimport) #endif 

    Then on a function that you want to be exported you use LIBRARY_API:

    LIBRARY_API int GetCoolInteger(); 

    In your library build project create a define LIBRARY_EXPORTS this will cause your functions to be exported for your DLL build.

    Since LIBRARY_EXPORTS 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.

    If your library is to be cross-platform you can define LIBRARY_API as nothing when not on Windows:

    #ifdef _WIN32 #    ifdef LIBRARY_EXPORTS #        define LIBRARY_API __declspec(dllexport) #    else #        define LIBRARY_API __declspec(dllimport) #    endif #elif #    define LIBRARY_API #endif 

    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.

    Exporting unmangled functions from a C++ DLL for LoadLibrary/PInvoke

    If you need this to use LoadLibrary and GetProcAddress, or maybe importing from another language (i.e PInvoke from .NET, or FFI in Python/R etc) you can use extern "C" inline with your dllexport to tell the C++ compiler not to mangle the names. And since we are using GetProcAddress instead of dllimport we don’t need to do the ifdef dance from above, just a simple dllexport:

    The Code:

    #define EXTERN_DLL_EXPORT extern "C" __declspec(dllexport)  EXTERN_DLL_EXPORT int getEngineVersion() {   return 1; }  EXTERN_DLL_EXPORT void registerPlugin(Kernel &K) {   K.getGraphicsServer().addGraphicsDriver(     auto_ptr<GraphicsServer::GraphicsDriver>(new OpenGLGraphicsDriver())   ); } 

    And here’s what the exports look like with Dumpbin /exports:

      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) 

    So this code works fine:

    m_hDLL = ::LoadLibrary(T"opengl_plugin.dll");  m_pfnGetEngineVersion = reinterpret_cast<fnGetEngineVersion *>(   ::GetProcAddress(m_hDLL, "getEngineVersion") ); m_pfnRegisterPlugin = reinterpret_cast<fnRegisterPlugin *>(   ::GetProcAddress(m_hDLL, "registerPlugin") ); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 191k
  • Answers 191k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The following script renames all files and directories recursively, starting… May 12, 2026 at 6:07 pm
  • Editorial Team
    Editorial Team added an answer In our cases, since we have 4 processors, we then… May 12, 2026 at 6:07 pm
  • Editorial Team
    Editorial Team added an answer PSEUDO SQL: SELECT * FROM CONTACTS as c WHERE c.ID… May 12, 2026 at 6:07 pm

Related Questions

I have a French site that I want to parse, but am running into
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have text I am displaying in SIlverlight that is coming from a CMS
I am currently running into a problem where an element is coming back from

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.