Qt application crashes when using DLL, works fine if exported function doesn't declare APIENTRY

liewl

I built a DLL project in Visual Studio 2010 (by following this post). It contains only one function:

extern "C" __declspec(dllexport) void APIENTRY hello()
{
    std::cout << "Hello DLL.\n" << std::endl;
}

Then I created a Qt console application to use that DLL. Its main.cpp contains this:

typedef bool (*f_void)(void);

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QLibrary lib("TestDll");
    f_void hello = (f_void) lib.resolve(QString("hello").toLatin1());
    hello();

    return a.exec();
}

When I use APIENTRY in the DLL, the program crashes when calling hello(). It works fine if I remove APIENTRY from hello() declaration though. Why does this happen?

Ben Voigt

In addition to the mechanics of making a function call requiring matching calling conventions, which is fixed by giving the function pointer the correct type as described below, the calling convention affects name mangling.

extern "C" prevents the C++ modus operandi of including the type into the name, so that overloads of a function get unique names and can be differentiated during symbolic lookup. But it doesn't prevent mangling entirely. For example, the function in the question, void __stdcall hello(void) will be exported by __declspec(dllexport) as _hello@0, where the trailing number is the number of bytes in the argument list. This helps avoid the situation that the caller and callee disagree on the size of the arguments, which is particularly problematic in __stdcall where the callee cleans up the stack.

Nevertheless, one can disable name mangling (and the Win32 DLLs such as gdi32.dll and shell32.dll have done so). For that you need a linker definitions file:

EXPORTS
  hello
  ; or hello = _hello@0

The linker knows about the mangling rules and will find the mangled name in the object file even if you don't provide it explicitly.

Additionally, when the exports are listed in the definition file, you no longer need __declspec(dllexport) in the code.

More information is available on MSDN.


If you call a function through a function pointer of the wrong type, you get undefined behavior. The calling convention is part of the type. Try:

typedef bool (APIENTRY *f_void)(void);  // or __stdcall instead of APIENTRY

I would guess that one of your header files contains #define APIENTRY __stdcall

It's always a good idea to explicitly set the calling convention of exported functions and function pointers. If you don't, you get the currently effective default calling convention, which is a project-specific compiler option.


Pedantically, whether a function and function pointer is marked extern "C" or not is also part of the type. But on Windows, where DLLs are found, extern "C" and extern "C++" have identical effect on calling convention.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Jsoup get works fine when using breakpoint but doesn't work in normal run

From Dev

Application crashes when using hooked function with cout

From Dev

C++ - Program crashes when I use sprintf() function (std::cout works fine)

From Dev

QSetting doesn't works fine for me

From Dev

EditText with SpannableStringBuilder and ImageSpan doesn't works fine

From Dev

Using a dll in a qt application program

From Dev

.desktop file doesn't execute a script, works fine when run by hand

From Dev

.desktop file doesn't execute a script, works fine when run by hand

From Dev

Batch file works fine. When converted to exe it doesn't [SOLVED]

From Dev

Application crashes when using startActivityForResult

From Dev

When using accelorometer application Crashes

From Dev

Application Crashes when using Intent

From Dev

mscorsn.dll doesn't load when using ILMerge

From Dev

SQL statement works in phpmyadmin, but it doesn't when using mysqli

From Dev

Publish desktop application working fine on my system but when I send setup to someone, it doesn't work

From Java

Declare a function that doesn't return in Scala

From Dev

Chrome doesn't declare my JavaScript function

From Dev

PutAsync doesn't send request to web api, but fiddler works fine

From Dev

Script doesn't run via crontab but works fine standalone

From Dev

Script doesn't run via crontab but works fine standalone

From Dev

Image Upload doesn't work in Server. Works fine in localhost

From Dev

axios doesn't work on Samsung Tizen TVs but works fine in emulator

From Dev

Application crashes Python when performing a function

From Dev

typedef works, 'using =' doesn't

From Dev

typedef works, 'using =' doesn't

From Dev

When i use cout.tie(NULL), program doesn't print anything for my code, but if i print endl, program works fine

From Dev

$.get() and $.post() in MVC app works fine in local Cassini webserver but when published into IIS 7.5 it doesn't work anymore

From Dev

DLL injection works, except when I compile it in Qt Creator

From Dev

JBoss REST WS application doesn't work when deployed as WAR file but works when expanded to a folder

Related Related

  1. 1

    Jsoup get works fine when using breakpoint but doesn't work in normal run

  2. 2

    Application crashes when using hooked function with cout

  3. 3

    C++ - Program crashes when I use sprintf() function (std::cout works fine)

  4. 4

    QSetting doesn't works fine for me

  5. 5

    EditText with SpannableStringBuilder and ImageSpan doesn't works fine

  6. 6

    Using a dll in a qt application program

  7. 7

    .desktop file doesn't execute a script, works fine when run by hand

  8. 8

    .desktop file doesn't execute a script, works fine when run by hand

  9. 9

    Batch file works fine. When converted to exe it doesn't [SOLVED]

  10. 10

    Application crashes when using startActivityForResult

  11. 11

    When using accelorometer application Crashes

  12. 12

    Application Crashes when using Intent

  13. 13

    mscorsn.dll doesn't load when using ILMerge

  14. 14

    SQL statement works in phpmyadmin, but it doesn't when using mysqli

  15. 15

    Publish desktop application working fine on my system but when I send setup to someone, it doesn't work

  16. 16

    Declare a function that doesn't return in Scala

  17. 17

    Chrome doesn't declare my JavaScript function

  18. 18

    PutAsync doesn't send request to web api, but fiddler works fine

  19. 19

    Script doesn't run via crontab but works fine standalone

  20. 20

    Script doesn't run via crontab but works fine standalone

  21. 21

    Image Upload doesn't work in Server. Works fine in localhost

  22. 22

    axios doesn't work on Samsung Tizen TVs but works fine in emulator

  23. 23

    Application crashes Python when performing a function

  24. 24

    typedef works, 'using =' doesn't

  25. 25

    typedef works, 'using =' doesn't

  26. 26

    When i use cout.tie(NULL), program doesn't print anything for my code, but if i print endl, program works fine

  27. 27

    $.get() and $.post() in MVC app works fine in local Cassini webserver but when published into IIS 7.5 it doesn't work anymore

  28. 28

    DLL injection works, except when I compile it in Qt Creator

  29. 29

    JBoss REST WS application doesn't work when deployed as WAR file but works when expanded to a folder

HotTag

Archive