Better understanding of C external features

I'm just trying to understand the external functions of C.

To my knowledge, extern C is always a function that you are trying to call from an application that has already been compiled. Either an executable, static, or dynamic library.

extern "C" { HRESULT CreateDevice(); typedef HRESULT (*CREATEDEVICE)(); HRESULT ReleaseDevice(); typedef HRESULT (*RELEASEDEVICE)(); } 

So my question is ...

Do I understand correctly?

Should it always be a C function pointer?

Why should you use typedef for every function?

I assume when you use GetProcAddress (). You are allocating memory for HEAP applications, not the one from which you are calling it. So you also have to free it from this heap?

+6
c ++ c visual-studio-2008 visual-c ++ visual-studio
source share
5 answers

extern "C" has 2 meanings. First, he states that symbolic function names are not β€œnamed” to support C ++. Secondly, it tells the compiler that the function is being called using the C calling convention, not the PASCAL calling convention. The difference is that the return address is pushed onto the stack. Using the wrong calling convention will crash your application.

This declaration is for the compiler, not the linker. Thus, an extern C function can exist in your own modules or in a binary library: the source of the actual bytes for the implementation of the function is resolved by the linker. If the function signature is declared as a regular C ++ function rather than extern C, the compiler will cast a symbolic name to encode the type information from the function signature. This will make it incompatible with the object code generated by other C ++ compilers. Therefore, creating an extern C function allows you to share code between compilers in binary form. Note that you cannot expose member functions this way, but only old-style C functions.

+4
source share

This is an optional function pointer. You can usually specify a function declaration and its prefix using extern "C" , as shown in some Microsoft examples .

If you use GetProcAddress() , you are not allocating any memory. You just get the memory address of the function inside the DLL, which is already loaded into the (presumably) LoadLibrary() memory.

Even when using function pointers (such as those returned by GetProcAddress) you don't need to use typedef , it is just that the code looks pretty ugly without it. It is always difficult to understand what to write. I think it will be something like:

 void (*pReleaseDevice)() = (void (__cdecl *)(void))GetProcAddress(hInstance, "ReleaseDevice"); 
+1
source share

extern "C" {} is a C ++ convention declaring that nested functions are C functions, not C ++. C ++ has a slightly different naming convention that conflicts with C. If you have a library written in C and want to use it in a C ++ program, you must use extern "C" {} to make the compiler knew that these are C-functions. If the library was written in C ++, I believe that extern "C" {} will throw an error.

Note that extern has several meanings - this particular case is a C ++ convention and is not related to the different uses of extern. For example,

 extern int count; 

has a completely different meaning than extern "C" {}.

typedef is separated from the external problem "C" {}. typedefs allows you to create aliases for generic types that make more sense. For example, declaring structures is often a verbose process. I can use typedef to shorten it:

 struct mystruct {int a; int b}; typedef struct mystruct returncode; // I can now declare a variable as type 'returncode' returncode a; 

So in your example, HRESULT is indeed an alias for (* CREATEDEVICE) (), although I believe that you should put it before the function (and not after).

0
source share

An important aspect of specifying extern "C" bindings is that function names are not processed, which is the default for C ++ names.

So that your library functions can be loaded using GetProcAddress , you need to either add a function to . def file , use __declspec(dllexport) or use extern "C" .

0
source share

To answer, in order:

  • extern "C" functions are used to interact with C from C ++. Using them leads to the fact that the C code can call the function. Because the Windows API is an API C, all functions are external "C" to ensure that C and C ++ code can use the API.

  • In order for C ++ programs to interact with other languages, including C, as a convention, functions are exported using extern "C". This is why many DLLs do this. However, this is not a technical requirement.

  • No, this should NOT be a function pointer C.

  • You also do not need to use typedef.

Sample code is provided from a header file that publishes a DLL export twice. Once as a set of external C methods that are exported so that the DLL can be statically linked. the other is a set of types of function pointers, so the dll can be dynamically loaded and the types of function pointers used with GetProcAddress.

0
source share

All Articles