CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2010
    Posts
    15

    Informations for a noob

    I'm confused about the following words:

    __declspec(dllexport) / __declspec(dllimport)

    extern "C"

    __stdcall


    I only realized that extern "C" preceding a function create a decoration-free symbol in the export table (assuming this is correct)

    Can you explain me what purposes are these conventions for?

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Informations for a noob

    Quote Originally Posted by pm44xl23 View Post
    __declspec(dllexport) / __declspec(dllimport)
    These two declare a function to be exported or imported from a DLL, respectively. __declspec(dllexport) is, obviously, used inside a module that is part of a (or the the entire) DLL. There is at least one other way to achieve this, i.e. declaring the function to be exported in the .def file. A function declared __declspec(dllimport) is to be linked at load time, as opposed to run-time linking using LoadLibrary() and GetProcAddress().

    __stdcall
    This declares a certain calling convention to be used for a function. This calling convention is used by Windows API functions and callbacks. It is also common for functions exported from a DLL, though it is not mandatory here. It has several #define aliases like WINAPI and APICALL.

    You are right about extern "C". But it also implies that code that is comiled under extern "C" is treated as real C by the compiler, i.e. it can't use any C++ features.

    See also http://www.codeguru.com/forum/showthread.php?t=500568 regarding this topic.

    HTH

  3. #3
    Join Date
    Jul 2010
    Posts
    15

    Re: Informations for a noob

    Thank you for your time answering me

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Informations for a noob

    Quote Originally Posted by Eri523 View Post
    You are right about extern "C". But it also implies that code that is comiled under extern "C" is treated as real C by the compiler, i.e. it can't use any C++ features.
    Not entirely true. Only the function signature has to be compatible with C (and it can't be a class method, etc, of course). The code within the function may do anything it likes; this is how you can call C++ code from C.

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Informations for a noob

    Quote Originally Posted by Lindley View Post
    Not entirely true. Only the function signature has to be compatible with C (and it can't be a class method, etc, of course). The code within the function may do anything it likes; this is how you can call C++ code from C.
    Oh, really... I thought I would remember a recent thread over there in the non-VC++ section, where extern "C" was used to integrate C code into a C++ project, allowing it to do some of the rare C things that are not allowed in C++. Looks like I didn't remember it correctly... So I think I'll have to dig that thread out again and re-read it.

    And what about the situation when I tell the compiler to treat the entire source file as C with the respective option in the file properties of the project? I.e. does that have the same meaning as using extern "C"?

    Thank you for putting things right, and let me apologize for spreading misinformation.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Informations for a noob

    The effect of an "extern "C"" statement is to tell the compiler to use C-compatible name mangling. In practice, this means you can use it to let C call C++ code, or to let C++ call C code.

    The easiest way to make an entire file be compiled as C rather than C++ is simply to make the filename end in .c. This does the trick for most build environments.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured