flat assembler
Message board for the users of flat assembler.
Index
> Windows > Problem using my DLL in C |
Author |
|
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'| How can I fix this? (DLL.lib is as an attachment)
|
|||||||||||
16 Jan 2014, 00:08 |
|
typedef 16 Jan 2014, 00:35
__cdecl vs __stdcall.
Your DLL to LIB tool didn't do a good job |
|||
16 Jan 2014, 00:35 |
|
Walter 16 Jan 2014, 01:11
Try this:
DLL.asm proc ShowMessage c TEST.asm cinvoke ShowMessage TEST.cpp void ShowMessage(void); |
|||
16 Jan 2014, 01:11 |
|
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. |
|||
16 Jan 2014, 11:43 |
|
A$M 16 Jan 2014, 12:43
Walter wrote: Try this: typedef wrote: 1. Make your DLL in FASM. 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...
|
|||||||||||||||||||
16 Jan 2014, 12:43 |
|
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. |
|||
16 Jan 2014, 13:22 |
|
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. I've tried extern void __cdecl _ShowMessage. But I'm not understanding. Can you write a simple example for me? |
|||
16 Jan 2014, 13:56 |
|
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; } |
|||
16 Jan 2014, 14:17 |
|
A$M 16 Jan 2014, 15:13
typedef wrote: For some reason the stupid Dll to Lib adds links to LIBC.lib. This works for you? Then maybe my compiler is the guilty. It's GNU GCC Compiler. I'll try to compile with another. |
|||
16 Jan 2014, 15:13 |
|
typedef 16 Jan 2014, 18:53
A$M wrote:
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. |
|||
16 Jan 2014, 18:53 |
|
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 |
|||
16 Jan 2014, 20:50 |
|
A$M 16 Jan 2014, 21:19
typedef wrote:
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. |
|||
16 Jan 2014, 21:19 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.