flat assembler
Message board for the users of flat assembler.

Index > Windows > Problem using my DLL in C

Author
Thread Post new topic Reply to topic
A$M



Joined: 29 Feb 2012
Posts: 94
A$M 16 Jan 2014, 00:08
I've made a simple DLL test code. This is DLL.asm:
Code:
format PE GUI 4.0 DLL

include 'win32a.inc'

proc ShowMessage
  invoke MessageBoxA, 0, _msg, 0, MB_OK
  ret
endp

_msg db "This is the message in the DLL!", 0

data import

 library user32,'USER32.DLL'

 import user32,\
        MessageBoxA,'MessageBoxA'

end data

data export

 export 'DLL.DLL',\
        ShowMessage,'ShowMessage'

end data

section '.reloc' fixups data readable discardable    

This is TEST.asm:
Code:
format PE GUI 4.0

include 'win32a.inc'

 invoke ShowMessage
 invoke ExitProcess,0

data import

 library kernel32,'KERNEL32.DLL',\
         DLL,'DLL.DLL'

 import kernel32,\
        ExitProcess,'ExitProcess'

 import DLL,\
        ShowMessage,'ShowMessage'

end data
    

It works. Now this is TEST.cpp:
Code:
#include <windows.h>
#pragma comment(lib,"DLL.lib")

void WINAPI ShowMessage(void);

int main()
{
    ShowMessage();
    return 0;
}
    

This is the output:
Quote:
C:\[...]\TEST.o:TEST.cpp|| undefined reference to `ShowMessage()@0'|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 1 seconds) ===|

How can I fix this? (DLL.lib is as an attachment)


Description: You will need this to build TEST.cpp
Download
Filename: DLL.rar
Filesize: 1.03 KB
Downloaded: 258 Time(s)

Post 16 Jan 2014, 00:08
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 16 Jan 2014, 00:35
__cdecl vs __stdcall.

Your DLL to LIB tool didn't do a good job
Post 16 Jan 2014, 00:35
View user's profile Send private message Reply with quote
A$M



Joined: 29 Feb 2012
Posts: 94
A$M 16 Jan 2014, 00:58
Well... I used DLL to Lib. Its output:
Quote:
Start conversion.
Preparing for the conversion...
Convert DLL file into object files...
Warning 10: Cannot find corresponding export symbol for ShowMessage in the import libraries, we have to assume the export symbol follows __cdecl call convention.
Bind object files to static library files.
Conversion finished.
I do not understand this. Can you explain me?
Post 16 Jan 2014, 00:58
View user's profile Send private message Reply with quote
Walter



Joined: 26 Jan 2013
Posts: 155
Walter 16 Jan 2014, 01:11
Try this:


DLL.asm

proc ShowMessage c


TEST.asm

cinvoke ShowMessage

TEST.cpp

void ShowMessage(void);
Post 16 Jan 2014, 01:11
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 16 Jan 2014, 11:43
1. Make your DLL in FASM.
2. Make a header file with function type declaration
3. Make a static library project that dynamically imports from your DLL.
4. Link your EXE against the library in step 3 using the header file in step 2.

or cut it short to dynamic import within your EXE. After all, the .lib code ends up in your EXE anyway so you might as well just do the import in your EXE.
Post 16 Jan 2014, 11:43
View user's profile Send private message Reply with quote
A$M



Joined: 29 Feb 2012
Posts: 94
A$M 16 Jan 2014, 12:43
Walter wrote:
Try this:


DLL.asm

proc ShowMessage c


TEST.asm

cinvoke ShowMessage

TEST.cpp

void ShowMessage(void);
Nothing changes. But thanks.
typedef wrote:
1. Make your DLL in FASM.
2. Make a header file with function type declaration
3. Make a static library project that dynamically imports from your DLL.
4. Link your EXE against the library in step 3 using the header file in step 2.

or cut it short to dynamic import within your EXE. After all, the .lib code ends up in your EXE anyway so you might as well just do the import in your EXE.

1. Yes, I've done this.
2. This is "void WINAPI ShowMessage(void);" ?
3. This is what I think I'm doing.
4. This is "#pragma comment(lib,"DLL.lib")" ?

Well, see the DLL to Lib window...


Description: dumpbin command
Filesize: 22.38 KB
Viewed: 4596 Time(s)

capture-20140116-104107.png


Description: DLL to Lib window
Filesize: 40.81 KB
Viewed: 4596 Time(s)

capture-20140116-103704.png


Post 16 Jan 2014, 12:43
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 16 Jan 2014, 13:22
What I'm saying is. DLL2Lib is messing up your export name. If your function is __stdcall is should be __imp__<fn_Name>@paramCount*4 in the .lib file.

DLL2Lib says your function is __stdcall yet the name conforms to __cdecl.

By convention, anyone looking at _ShowMessage would think that this is a __cdecl.

So if you want to link with the lib, try changing your declaration to

extern void __cdecl _ShowMessage (or omit the __cdecl) or as whatever Dll2Lib tells you.
Remove the void parameter type as that will add an unnecessary parameter count. Also, make sure your actual function in the DLL is of the same calling convention.

OR

Simply link dynamically.
Post 16 Jan 2014, 13:22
View user's profile Send private message Reply with quote
A$M



Joined: 29 Feb 2012
Posts: 94
A$M 16 Jan 2014, 13:56
typedef wrote:
What I'm saying is. DLL2Lib is messing up your export name. If your function is __stdcall is should be __imp__<fn_Name>@paramCount*4 in the .lib file.

DLL2Lib says your function is __stdcall yet the name conforms to __cdecl.

By convention, anyone looking at _ShowMessage would think that this is a __cdecl.

So if you want to link with the lib, try changing your declaration to

extern void __cdecl _ShowMessage (or omit the __cdecl) or as whatever Dll2Lib tells you.
Remove the void parameter type as that will add an unnecessary parameter count. Also, make sure your actual function in the DLL is of the same calling convention.

OR

Simply link dynamically.

I've tried extern void __cdecl _ShowMessage. But I'm not understanding. Can you write a simple example for me?
Post 16 Jan 2014, 13:56
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 16 Jan 2014, 14:17
For some reason the stupid Dll to Lib adds links to LIBC.lib.

Code:
#pragma comment(lib,"DLL.lib")

void __cdecl ShowMessage();

int main(int argc, char **argv)
{

ShowMessage();

return 0;
}
    
Post 16 Jan 2014, 14:17
View user's profile Send private message Reply with quote
A$M



Joined: 29 Feb 2012
Posts: 94
A$M 16 Jan 2014, 15:13
typedef wrote:
For some reason the stupid Dll to Lib adds links to LIBC.lib.

Code:
#pragma comment(lib,"DLL.lib")

void __cdecl ShowMessage();

int main(int argc, char **argv)
{

ShowMessage();

return 0;
}
    

This works for you? Then maybe my compiler is the guilty. It's GNU GCC Compiler. I'll try to compile with another.
Post 16 Jan 2014, 15:13
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 16 Jan 2014, 18:53
A$M wrote:
typedef wrote:
For some reason the stupid Dll to Lib adds links to LIBC.lib.

Code:
#pragma comment(lib,"DLL.lib")

void __cdecl ShowMessage();

int main(int argc, char **argv)
{

ShowMessage();

return 0;
}
    

This works for you? Then maybe my compiler is the guilty. It's GNU GCC Compiler. I'll try to compile with another.


I hope you are actually using a C compiler though I don't see why it would impede the linking process convention-wise.

And you need to have libc.lib as that stupid dll to lib requires it. DLL to Lib is the culprit and your log says it all. I rest my case.
Post 16 Jan 2014, 18:53
View user's profile Send private message Reply with quote
RIxRIpt



Joined: 18 Apr 2013
Posts: 50
RIxRIpt 16 Jan 2014, 20:50
Maybe that's not you want (obj instead of DLL/LIB) but it works

ASM
Code:
format MS COFF

public ShowMessage as '_ShowMessage@0'
extrn '__imp__MessageBoxA@16' as MessageBoxA:dword

include 'win32a.inc' 

section '.code' code readable executable
        proc ShowMessage 
                invoke MessageBoxA, 0, msg, 0, MB_OK
                ret
        endp

section '.data' data readable writeable
        msg db 'This is the message in the DLL!', 0 
    

C
Code:
//Add .obj file to linker
//In VC++: Project Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies
//You can add library directory in Project Properties -> Configuration Properties -> VC++ Directories
//#pragma comment(lib, ".obj"); doesn't work
void __stdcall ShowMessage(void);

int main(void) {
        ShowMessage();
        return 0;
}
    



http://board.flatassembler.net/topic.php?t=13026
Post 16 Jan 2014, 20:50
View user's profile Send private message Visit poster's website Reply with quote
A$M



Joined: 29 Feb 2012
Posts: 94
A$M 16 Jan 2014, 21:19
typedef wrote:
A$M wrote:
typedef wrote:
For some reason the stupid Dll to Lib adds links to LIBC.lib.

Code:
#pragma comment(lib,"DLL.lib")

void __cdecl ShowMessage();

int main(int argc, char **argv)
{

ShowMessage();

return 0;
}
    

This works for you? Then maybe my compiler is the guilty. It's GNU GCC Compiler. I'll try to compile with another.


I hope you are actually using a C compiler though I don't see why it would impede the linking process convention-wise.

And you need to have libc.lib as that stupid dll to lib requires it. DLL to Lib is the culprit and your log says it all. I rest my case.

Well... I've changed the code and now works (or seems to work).
New TEST.cpp:
Code:
#include <Windows.h>

typedef void (WINAPI *FUNC)(void);

int main(int argc, char **argv)
{
        HMODULE DLL;
        FUNC ShowMessage;
        DLL = LoadLibrary("DLL.dll");
        if (!DLL) { MessageBox(NULL, "DLL.dll is missing.", "ERROR", MB_OK + MB_ICONERROR); return -1; }
        ShowMessage = (FUNC)GetProcAddress(DLL, "ShowMessage");
        ShowMessage();
        return 0;
}
    

Yeah!!! Thanks typedef and Walter.
EDIT: and RIxRIpt too. I had not seen your post. But thanks you too.
Post 16 Jan 2014, 21:19
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.