4

I have created Dll project. I created myasm.asm file that has one function:

 .486
.model flat, stdcall
.code
MyProc1 proc x: DWORD, y: DWORD
    xor eax,eax
//......//
    ret
MyProc1 endp
    end

There is my heade file:

#pragma once

#include <Windows.h>

#ifdef LAB1DLL_EXPORTS
#define LAB1DLL_API __declspec(dllexport)
#else
#define LAB1DLL_API __declspec(dllimport)
#endif

extern "C"
{
    LAB1DLL_API int _stdcall MyProc1(DWORD x, DWORD y);
}

And dllMain (beging of it)"

#define LAB1DLL_EXPORTS 1
#include "Lab1Dll.h"

Im my test application in wich I want to use that dll and it's exported function I have:

#include "Lab1Dll.h"

But my dll is not exporting my MyProc1 function. If I add "normal" function to that DLL and exprot it it is avilable in my Test application and DLL compilation process produces lib file.

Without "normal" functions I don't get .lib file. And I can't link to that library.

How to make that exported function work? Or how to make it export in the first place?

UPDATE: I saw that adding .def file with below contens works perfect. But then. I shouldn't be doing it. That is what __declspec(dllexport) should do for me?

LIBRARY

EXPORTS

MyProc1
Hooch
  • 25,377
  • 28
  • 89
  • 154
  • Might have something to do with decorated/mangled names; check with `dumpbin`. Try defining the proc with its decorated/mangled name in `myasm.asm` (but not in the header and C sources). Other suggestions: http://stackoverflow.com/questions/2804893/c-dll-export-decorated-mangled-names – Ruud Helderman Feb 25 '14 at 21:47
  • @Ruud How? I'm asking about that "decoration". – Hooch Feb 26 '14 at 12:48

3 Answers3

2

I had a similar problem when trying to use from another module (DLL/EXE) an assembly function exported from a DLL. From the linker error I figured out that the importing module was trying to access e.g. __impl_SRFlushCache function instead of SRFlushCache function as it is declared in the exporting module's header file and defined in assembly file. So something was adding __impl_ prefix nevertheless I declared the function as extern "C" to avoid any mangling.

I solved the problem by adding a module definition (.def) file to the exporting module and listing my assembly function there like below:

LIBRARY SRPlatform
EXPORTS
  SRFlushCache

For reference:

Function declaration in a header file:

#ifdef SRPLATFORM_EXPORTS
#define SRPLATFORM_API __declspec(dllexport)
#else
#define SRPLATFORM_API __declspec(dllimport)
#endif // SRPLATFORM_EXPORTS

SRPLATFORM_API void __fastcall SRFlushCache(const void *pFirstCl, const void *pLimCl, const size_t clSize);

Function definition in the .asm file:

_DATA SEGMENT

_DATA ENDS

_TEXT SEGMENT

PUBLIC SRFlushCache

; RCX=pFirstCl
; RDX=pLimCl
; R8=clSize
SRFlushCache PROC

SRFlushCache_Loop:
  clflushopt byte ptr [RCX]
  add RCX, R8
  cmp RCX, RDX ; RCX-RDX
  jl SRFlushCache_Loop
  ret

SRFlushCache ENDP

_TEXT ENDS

END
Serge Rogatch
  • 11,119
  • 4
  • 58
  • 117
  • In Visual Studio, first you create a name.def file with your definitions in your project. Then you go to the project Properties > Linker > Input > Module Definition File. Add the name.def file to that property. It should export then. (If anyone runs into how to add the definition file to the project, like I did.) – douggard Jan 10 '18 at 23:41
2

The masm equivalent to __declspec(dllexport) is EXPORT after proc

So this will add the .drectve section to the *.obj file that masm produces and will be processed by the linker

.386
.model flat, stdcall
.code
MyProc1 proc EXPORT x: DWORD, y: DWORD
    xor eax,eax
    ret
MyProc1 endp
end

Also if you declare MyProc1 as extern "C" in your header file it's

MyProc1 proc C EXPORT x: DWORD, y: DWORD

otherwise it's

MyProc1 proc stdcall EXPORT x: DWORD, y: DWORD

to make sure the name-mangling matches

PeterT
  • 6,683
  • 1
  • 21
  • 27
  • well this is awkward, didn't see that this was an old question bumped from 2014 – PeterT Apr 01 '19 at 18:33
  • 1
    That's OK. I was going to answer it anyways, knowing it was an old question, though with a not as good an answer (I didn't realize there was an EXPORT keyword to PROC.) . I would add though that the reason why `__declspec(dllexport)` doesn't work for the original poster is because it needs to be used on the definition of the function, not the declaration. – Ross Ridge Apr 01 '19 at 18:37
  • @RossRidge yeah, the dllexport can not work with masm since you can't include the *.h into your *.asm file. Also the EXPORT thing is not very well documented apparently, but I just thought I'd add it for completeness – PeterT Apr 01 '19 at 18:41
0

There are four methods for exporting a definition, listed in recommended order of use:

  • __declspec(dllexport) in the source code

  • An EXPORTS statement in a .def file

  • An /EXPORT specification in a LINK command

  • A comment directive in the source code, of the form #pragma comment(linker, "/export: definition ").

Source

Origin

Vozzie
  • 325
  • 2
  • 9