Link to home
Start Free TrialLog in
Avatar of mrwad99
mrwad99Flag for United Kingdom of Great Britain and Northern Ireland

asked on

pragma comment EXPORT confusion!

Ah hello.

I recently built a DLL that needed to be used via GetProcAddress.  I discovered that due to name mangling, this function call was failing, so began to create a .DEF file that would declare my function to be exported.  However, I realised that I had seen this done via the

#pragma comment(linker, "/EXPORT"

I have the following code:

// stdafx.h

#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>


// ExportedFunction.cpp

#include "StdAfx.h"

#pragma comment(linker, "/EXPORT:ExportedFunction2=_ExportedFunction@4")

typedef int MSIHANDLE;

extern "C" UINT __stdcall ExportedFunction( UINT )
{
      return 0;
}

// stdafx.cpp

<Empty>

I build to a Win32 DLL.

My question is about the arguments to the #pragma comment code.  Obviously the first part "ExportedFunction" is what I want my function to be accessed via using GetProcAddresss.  Indeed, when I load my DLL in dependency viewer, I see one entry:  ExportedFunction2.

The second part is confusing.  The code as-is will compile and link.  If I remove the _ from the second part, it fails with

unresolved external symbol ExportedFunction@4      MYDLL_.exp

If I change @4 to another number, it fails with the same error.  If I remove the parameter from the function, leaving everything else as-is, it also fails with this error.  

Can someone please tell me What is going on here, and if there is any documentation I can read so I can learn about what these arguments mean?

PS I know I can also achieve this via __declspec etc, but I would like to learn how to export in this manner too.

TIA
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mrwad99

ASKER

Thanks.

>> I'd rather recommend to not us '__stdcall' unless you really have to. Without it, that can just be...

I tried that, but without the __stdcall, I still get the

error LNK2001: unresolved external symbol ExportedFunction:

#pragma comment(linker, "/EXPORT:ExportedFunction2=ExportedFunction")

extern "C"  UINT ExportedFunction( UINT )
{
      return 0;
}

Why is this please?

TIA
Ooops, forgot that '__cdecl' (the default) also adds a leading underscore (unless you use the 'dllexport') modifier - try

#pragma comment(linker, "/EXPORT:ExportedFunction2=_ExportedFunction")

instead.
Avatar of mrwad99

ASKER

Thank you :)