none
How to define a function that using extern “C” modifier

    Question

  • In a DLL, a function is exported, and it is defined as:

    extern "C" ULONGLONG WINAPI MyFun(CONST LPVOID lpParam);

    Now in My app, I need to get the address of the function and invoke it, so I use

    typedef ULONGLONG (WINAPI *MyFun) (CONST LPVOID lpParam);

    and use

    lpMyFun = (MyFun)GetProcAddress(hDLL, "MyFun");

    In such a way, there is no way to put the extern "C" modifier in the typedef codeline, I using

    extern "C" typedef ULONGLONG (WINAPI *MyFun) (CONST LPVOID lpParam);

    but that will cause compiler error.

    How to solve the problem?

    Thanks

    Thursday, August 28, 2014 4:03 AM

Answers

All replies

  • On 8/28/2014 12:03 AM, chcw wrote:

    In such a way, there is no way to put the extern "C" modifier in the typedef codeline

    It's OK. You don't need it.

    How to solve the problem?

    There is no problem to solve.


    Igor Tandetnik
    Thursday, August 28, 2014 5:07 AM
  • Hello,

    To export function from DLL you have to use below mentioned code format.

    extern "C"
    {
    __declspec(dllexport) void MyDllMethod(Input and Output parameters)
    {
      //Your code logic.
    }

    }

    And to use above exported method in MFC.

    You have to load that dll by using function name - LoadLibrary().

    After that use GetProcAddress() function to retrieve address (call to dll function) of exported dll function.

    Let me know incase you required more information.



    Regards, Rohan Jayraj Mohite.

    Thursday, August 28, 2014 12:38 PM
  • extern “C” only affects the name you pass to GetProcAddress, e.g. “MyFun” in GetProcAddress(..., “MyFun”).  If the name is mangled, it is no longer called “MyFun” but rather some mangled version of it like e.g. “@2MyFunc@xyz” which is totally made up by me but shows that the types of the return value and parameters are encoded into the function name.
     
    If your GetProcAddress is succeeding with whatever name you are passing to it, you’re good to go as Igor says.
     
    -- David
     
    Friday, August 29, 2014 2:33 AM
  • On 29/08/2014 04:33, David Ching [MVP] wrote:


    extern “C” only affects the name you pass to GetProcAddress, e.g. “MyFun” in GetProcAddress(..., “MyFun”).  If the name is mangled, it is no longer called “MyFun” but rather some mangled version of it like e.g.“@2MyFunc@xyz <mailto:&#147;@2MyFunc@xyz>” which is totally made up by me but shows that the types of the return value and parameters are encoded into the function name.

    Note that some form of name mangling happens also for extern "C" exported functions, e.g. if the calling convention is __stdcall (which is the expansion of the WINAPI macro):
       __stdcall on MSDN
      http://msdn.microsoft.com/en-us/library/zxk0tw93.aspx

    "An underscore (_) is prefixed to the name. The name is followed by the at sign (@) followed by the number of bytes (in decimal) in the argument list. Therefore, the function declared as int func( int a, double b ) is decorated as follows: _func@12"

    To my knowledge, if you don't want name mangling at all, a good option is to use the .DEF files for exporting functions from DLLs.

    Giovanni

    Friday, August 29, 2014 2:47 PM