Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What does declaration extern "C" for variable mean?

extern "C" BOOL (WINAPI* const _pRawDllMain)(HINSTANCE, DWORD, LPVOID) = &ExtRawDllMain;


Update:
Is it declaration of internal variable or external variable?
Posted
Updated 3-Mar-11 2:13am
v3

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Mar-11 11:15am    
Correct, 5.
--SA
extern "C" prevents the C++ compiler of mangling the name, making the function have C linkage. See, for instance: extern "C" at StackOverflow[^].
:)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Mar-11 11:15am    
Correct, 5.
--SA
It means that the code for that function is written (and more importantly, compiled) in C.
Now that you want to use it from C++ the function definition is the same as far as the compiler is concerned WINAPI *const RawDllMain(HINSTANCE, DWORD, LPVOID) in both C and C++, however due to extensions in C++ that allow for function overriding, the symbol name for the linker is different.

In C, the symbol name will be either RawDllMain or _RawDllMain depending on the convention used.
In C++, parameter and return types are "mangled" into the name, making the C++ name ?RawDllMain@@YGHPAUHINSTANCE__@@KPAX@Z. The encoded variable types are easier to spot when using custom types like structs or classes.

If you were then to call the function RawDllMain from the DLL, the function name that you would search for depends on your language, C or C++.
If you are using GetProcAddress then you would need to search for the function ?RawDllMain@@YGHPAUHINSTANCE__@@KPAX@Z if you did not use extern "C".

There are other uses for this too.
If you are using a 3rd party library that is written in C and you want to use it from C++ then you may need to wrap
extern "C" {
#include "the_lib\lib.h"
}

around the includes for that library for the same reasons. Often these libraries would automatically do this however.
 
Share this answer
 
Comments
Albert Holguin 3-Mar-11 10:07am    
nice complete answer! :)
Sergey Alexandrovich Kryukov 3-Mar-11 11:15am    
Agree, a 5.
--SA
mbue 5-Mar-11 9:31am    
The "mangled" names are so called "decorations", that make the compiler able to adjust the stack around imported functions.
regards.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900