Hooch
Hooch

Reputation: 29673

Exporting ASM function from DLL - Visual Studio C++

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

Upvotes: 4

Views: 5053

Answers (3)

PeterT
PeterT

Reputation: 8284

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

Upvotes: 4

Vozzie
Vozzie

Reputation: 455

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

Upvotes: 0

Serge Rogatch
Serge Rogatch

Reputation: 15030

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

Upvotes: 3

Related Questions