cwindowswinapidll

How to declare DLL in user32.dll (aka stdcall?) fashion?


I have a third party application that allows you to call C functions from DLL files. Provided sample to this app shows you can call MessageBoxW from user32.dll. It also allows you to call C functions from your DLL files.

I've did a DLL from a file.h file like this:

_declspec (dllexport) void example(int);

and file.c like this:

#include <stdio.h>
#include "file.h"

_declspec (dllexport) void example(int s1)
{
    printf("dsa");    
}

And compile it with C/C++ Compiler Version 15 from Windows SDK like this:

cl file.c /DLL /LD

And i get proper compilation with DLL file. In DLL functions examiner I see my function. I drop this file into System32 folder and call it from this third party application.

Application finds the file, but is unable to find the function.

I think the cause of the problem is that i declare (or compile) my DLL in other fashion/way/standard that Windows libraries (like user32.dll) because user32.dll works fine.

I've found that the third party app uses this kind of calling functions in DLL:

winapi_abi Used for calling Windows system functions. These are declared as stdcall on Windows, but do not have mangled names.

So my question is: how to prepare and compile DLL file in the user32.dll fashion (stdcall?) so it will work with third party app?


Solution

  • The easy answer is:

    __declspec(dllexport) void __stdcall example(int);
    

    And the same in the implementation, of course.

    If you look into windows.h and friends you'll see:

    #define WINUSERAPI __declspec(dllexport)
    #define WINAPI __stdcall
    

    And then:

    WINUSERAPI int WINAPI MessageBoxW(HWND,LPCWSTR,LPCWSTR,UINT);
    

    But if you just define a few functions there is no need for the macros.