flat assembler
Message board for the users of flat assembler.

Index > Windows > Using Assembly DLL files created with FASMW in c++

Author
Thread Post new topic Reply to topic
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt 13 Apr 2004, 00:22
How would you load and use a dll file created with FASMW in a c++ program? Has anyone tried this before?
Post 13 Apr 2004, 00:22
View user's profile Send private message Reply with quote
comrade



Joined: 16 Jun 2003
Posts: 1150
Location: Russian Federation
comrade 13 Apr 2004, 02:42
same way as you do with any other DLL
its all standard

_________________
comrade (comrade64@live.com; http://comrade.ownz.com/)
Post 13 Apr 2004, 02:42
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 13 Apr 2004, 18:56
except that if you want to import DLL file, you need .LIB (i think) file which lists imported procedures.

But you can still use LoadLibrary and GetProcAddress

btw. that LIB file is just some sort of COFF, but i don't know how exactly is it performed (I've read something from IczLion about it some time ago)
Post 13 Apr 2004, 18:56
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt 13 Apr 2004, 21:26
The reason I want to know is that I going full speed ahead with learning c++, and sometime in the future will purchase the Microsoft Visual C++ standard edition, but would still like to be able to use assembly when I need it. So I guess the better question would be, is it difficult to use an external assembly dll (made with FASMW) in c++?
Post 13 Apr 2004, 21:26
View user's profile Send private message Reply with quote
zenek_tm



Joined: 21 Mar 2004
Posts: 33
Location: Poland
zenek_tm 13 Apr 2004, 21:35
Just to make it clear. In c/c++ to statically import from library you need a header file with imported function prototypes marked as imported and a lib file, which propably contains names and addresses of functions in this dll. The lib file can be generated using some utils. however I don't know any except one. Borland is, at least used to, distribute along with his c++ compilers such util since Borland lib is not compatible with MS lib file.
In Object Pascal compilers, such as Delphi, there is only one pas file. In this file in interface section there are function prototypes and in implementation section function prototypes are repeated and marked as external followed by information in which dll this function is. So Delphi evaluates the addresses of dll functions during compilation time. At least I think so.


So if You want to use a dll in c++ app either use dynamic linking or somehow generate the lib file. In Delphi it's far more easier Smile
Post 13 Apr 2004, 21:35
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 13 Apr 2004, 21:54
No, it's not difficult at all - you just need to know a few things about the tools you use Smile

For instance, with C/C++, you need some function prototypes. It's a good idea to put these in a .h file, and if you want to be able to use your asm funcs from both C and C++, it will look something like this:
Code:
#ifdef __cplusplus
extern "C" {
#endif
// prototypes go here
#ifdef __cplusplus
}
#endif
    

This part is generic for all compilers.

Next, the prototypes themselves. If you just do a normal "void asmfoobar(void);", it will be C calling convention, and most compilers will expect the decorated name of the function to be "_asmfoobar".

The STDCALL convention is imho a bit nicer, but it waries a bit from compiler to compiler how to add it. "void __stdcall asmfoobar(void);" in Visual C++, for instance. Compiler specifics can usually be handled by some preprocessor stuff, like "#define STDCALL __stdcall", then you use STDCALL instead of __stdcall, and only need to change the definition at the top of your header file.

However, STDCALL calling convention also changes the name decoration rules. VC++'s name decoration rule for STDCALL is to prefix the name with an underscore, and append '@' + the bytesize of the arguments. Thus, void asmfoobar(void) would be _asmfoobar@0 .

Okay, libraries. Make fasm (or another assembler, if you so choose) output to MS COFF format. Doing a static library is easy - you just add all your .obj files to a .lib with the use of a librarian (for vc++ it's lib.exe, which is really just a wrapper around link.exe). For DLL's it requires a bit more, like writing a .def file. It's been a while since I've messed with DLLs, so I can't remember that stuff off top of my head, but iirc it's as simple as:
Code:
LIBRARY mydll
EXPORTS
    myfunc1
     myfunc2=_RenamedFromThis
    

et cetera. It should be easy to google up some more information if nobody else has anything to add.

Oh, I think there was some utility to directly generate import libraries from a dll without the use of any intermediate tools or whatever... perhaps it was alib from the alink package? (google).
Post 13 Apr 2004, 21:54
View user's profile Send private message Visit poster's website Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt 14 Apr 2004, 08:06
looks a bit more involved than what I thought. I knew you needed a header file, but didn't know you had to generate a library for the dll file. Looks like i'll have to wait to get the VC++ compiler so I can inline the assembly code.
Post 14 Apr 2004, 08:06
View user's profile Send private message Reply with quote
coconut



Joined: 02 Apr 2004
Posts: 326
Location: US
coconut 14 Apr 2004, 10:29
the masm32 package from http://www.masm32.com has a few utils which can help you generate .lib files. check out their boards at http://www.masmforum.com the author of one of those tools posts regularly there and can help ya out
Post 14 Apr 2004, 10:29
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 14 Apr 2004, 15:30
madmatt, it's not bad once you get used to it, and a lot of it can be automated Smile
Post 14 Apr 2004, 15:30
View user's profile Send private message Visit poster's website Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt 14 Apr 2004, 21:48
I do have the Masm package, along with the Fasm and free borland 5.5 package. Wink I guess this is one of those things that once you get past the blood, sweat, and tears, in the end you'll be grateful that you learned this, much like working on your car or truck! Razz (just changed the starter on my truck, and it went more smoothly than I ever thought it would! Shocked I like it when things work out that way!! Laughing ) Ok, thanks guys (gals?) for your help, I'll look into this further. Two more questions, Which would be better to learn first, how to link in an object file (.obj) or how to use a dll? and can fasm create an obj. file?
Post 14 Apr 2004, 21:48
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.