flat assembler
Message board for the users of flat assembler.

Index > Windows > FASM dll in C++ and vice versa

Author
Thread Post new topic Reply to topic
Giedrius



Joined: 13 Feb 2005
Posts: 40
Location: Lithuania
Giedrius
Can someone give me a basic example. It could be only an argument1 + argument2 dll in fasm and a C++ program which calls it (I'm using visual c++ 6.0) and same thing, except the dll would be in C++ and to call it from fasm.

_________________
Better to rule in hell, than to be a slave in heaven...
Post 03 Apr 2005, 12:27
View user's profile Send private message Reply with quote
Reverend



Joined: 24 Aug 2004
Posts: 408
Location: Poland
Reverend
I'll use an example dll from fasm package. It exports ShowLastError, which gets one parameter, handle of window. So in your cpp file:
a) declare prototype: ('void' since the return value is meaningless for us in this case)
Code:
void STDCALL ShowLastError(HWND)    

b) change a dll file with a special tool, unfortuantely I don;t have it, but it was surely in MASM package and should be in vc++ package too. Should be named like 'dll2lib.exe' or somewhat like this
c) add the created .lib file to linked libraries in options in vc++
d) use the function normally like:
Code:
MessageBox(hWnd,"Nice text","Nic text too",MB_OK);
ShowLastError(hWnd);    
Post 03 Apr 2005, 13:45
View user's profile Send private message Visit poster's website Reply with quote
Giedrius



Joined: 13 Feb 2005
Posts: 40
Location: Lithuania
Giedrius
I've downloaded MASM32, because I haven't found the tool in VC++, and I can't find that tool neither in there Sad Can you check how is it called exactly?

_________________
Better to rule in hell, than to be a slave in heaven...
Post 03 Apr 2005, 20:00
View user's profile Send private message Reply with quote
Reverend



Joined: 24 Aug 2004
Posts: 408
Location: Poland
Reverend
Shit, I made a mistake. Sorry for misleading you. There's no such tool (btw: I don't know why?). So the only solutions I see now is to compile the file as MS COFF (but it needs some changes in source file) or to write such a tool Smile
I guess you'll have to read much about .lib file before. I may start to write such a tool (as it sounds interesting and useful), but I have a lack of time, so I don;t think I'd be able to do it.

There's also another soultion. There is a tool that creates static library out of .dll file. You wouldn't need then to import any function, all code will be pasted directly to executable file. Unfortunately it cost 300$ Wink
Post 04 Apr 2005, 16:50
View user's profile Send private message Visit poster's website Reply with quote
Giedrius



Joined: 13 Feb 2005
Posts: 40
Location: Lithuania
Giedrius
So there is no way (without those two) of making fasm and VC++ to be friends? Smile
Post 04 Apr 2005, 17:51
View user's profile Send private message Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22
Why won't C++ let you use the LoadLibrary api and GetProcAddress ?
C++ had to try really really hard to make itself less useful.
Post 05 Apr 2005, 04:56
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
Of course C++ lets you use LoadLibrary and GetProcAddress, but it's often more useful to have implicit linkage...
Post 05 Apr 2005, 07:47
View user's profile Send private message Visit poster's website Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22
I'm sorry I was being sarcastic.

It really is a problematic issue in c++, unless someone can come up with a converter that turns a fASM source file into something c++'s inline assembler can use or improve the inline assembly feature in c++ all together..
Post 06 Apr 2005, 00:25
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
Giedrius



Joined: 13 Feb 2005
Posts: 40
Location: Lithuania
Giedrius
Visual C++ inline assembler is very simple and almost the same as fasm. You can just write:

int asdf;

void function()
{
_asm
{
mov asdf,5
}
//now asdf contains 5
}

_________________
Better to rule in hell, than to be a slave in heaven...
Post 06 Apr 2005, 05:59
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
I'm considering writing a little tool that will build import libraries from a .txt file containing DLL-name and a list of exports, which would let you easily build import libraries for FASM .dll's. It shouldn't be too hard a task, unfortunately I don't have much spare time at the moment :/
Post 06 Apr 2005, 07:43
View user's profile Send private message Visit poster's website Reply with quote
Reverend



Joined: 24 Aug 2004
Posts: 408
Location: Poland
Reverend
f0dder wrote:
I'm considering writing a little tool that will build import libraries from a .txt file containing DLL-name and a list of exports, which would let you easily build import libraries for FASM .dll's.
But why do you need to have the user to input a txt file with such info? Wouldn't it be better if the program could open a common dialog for getting files' paths or just inputing dll name from commandline? Exports names are saved in a file so I can't see why not to read it from there. You can check sources of my program that is reading an export table and saves exports names in a file from which fasm can later import these functions. I thought about modifying these sources to create a tool for importing fasm procs in C++, but...
Quote:
It shouldn't be too hard a task, unfortunately I don't have much spare time at the moment :/
Yes, I have a lack of time too, and I'd need to know first how import .lib file is built.

Btw. How to call a procedure in C++ using a pointer returned by GetProcAddress ?
Post 06 Apr 2005, 12:54
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
Quote:

But why do you need to have the user to input a txt file with such info? Wouldn't it be better if the program could open a common dialog for getting files' paths or just inputing dll name from commandline?

I'm planning that, too - initially I want to concentrate on building the import libraries, though... and parsing a .txt is less work than parsing a .dll Wink. I've done that kind of work before, but I'll rather incorporate it later.

Quote:

and I'd need to know first how import .lib file is built.

It's not too bad. Microsoft has it documented in the "pecoff" document - google for "pecoff site:msdn.microsoft.com".

Quote:

Btw. How to call a procedure in C++ using a pointer returned by GetProcAddress ?

You need some typecasting magic. This looks *extremely* ugly, and thus the best thing to do is adding some typedefs. Something like this:

Code:
#include <stdio.h>

typedef int (*funcptr_t)(int,int,int);

int myfunc(int a, int b, int c)
{
        return a+b+c;
}

int main(void)
{
        funcptr_t func = &myfunc;
        printf("func(10,20,30): %d\n", func(10,20,30));
        return 0;
}
    
Post 06 Apr 2005, 13:28
View user's profile Send private message Visit poster's website Reply with quote
Vortex



Joined: 17 Jun 2003
Posts: 318
Vortex
Hi f0dder,

Pelle's librarian Polib is able to create import libraries from DLLs. These import libraries are just usefull to work with Fasm.

Quote:

Creating Borland style MS COFF import libraries
Polib V3.00.1 can create import libraries directly from DLLs. With the /nound switch offered by Polib, it's possible to get import libraries with no underscored names making easy the linkage of Fasm and Sphinx C-- object files. This new version of Polib replaces my old tool dll2lib. ( Thanks Pelle )


The zip file below contains an example for Fasm:

http://vortex.masmcode.com/files/implib.zip

_________________
Code it... That's all...
Post 06 Apr 2005, 17:45
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
cute Smile

I'm still gonna do my tool anyway, as a nice excercise in multiple things.
(PS: does polib do it directly, or does it depend on an assembler or similar lameness?)
Post 06 Apr 2005, 17:55
View user's profile Send private message Visit poster's website Reply with quote
Vortex



Joined: 17 Jun 2003
Posts: 318
Vortex
f0dder wrote:
cute Smile

I'm still gonna do my tool anyway, as a nice excercise in multiple things.
(PS: does polib do it directly, or does it depend on an assembler or similar lameness?)


No, Polib doesn't depend on an assembler:
Code:
polib /nound /out:kernel32.lib \windows\system32\kernel32.dll
polib /nound /out:user32.lib \windows\system32\user32.dll
polib /nound /out:gdi32.lib \windows\system32\gdi32.dll
    


The nound option creates import libraries with no decoration.

_________________
Code it... That's all...
Post 06 Apr 2005, 19:52
View user's profile Send private message Visit poster's website Reply with quote
Vortex



Joined: 17 Jun 2003
Posts: 318
Vortex
Hi Giedrius,

Here is a simple example for you : An autotyping application
Code:
#define WIN32_LEAN_AND_MEAN  
#include <windows.h>

BOOL APIENTRY DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
       return TRUE;
}

void WINAPI Autotype(char* message)
{

    HWND hWnd=FindWindowEx(FindWindow("Notepad",0),0,"Edit",0);
        while (*message)
                        {
                                SendMessage(hWnd,WM_CHAR,(WPARAM)*message,0);
                                Sleep(200);
                                ++message;
                        }
}
    


Code:
format PE GUI 4.0
entry start

Include '%fasminc%\win32a.inc'
Include '%fasminc%\macro\if.inc'

section '.data' data readable writeable

notepad db "Notepad.exe",0
msg     db "Hello friends!",13,10,"This is an example of autotyping. :)",0

section '.text' code readable executable

start:
        invoke  WinExec,notepad,SW_SHOW
        invoke  Autotype,msg
        invoke  ExitProcess,0

section '.idata' import data readable writeable

  library kernel32,'kernel32.dll',\
          small,'small.dll'

  import kernel32,\
         ExitProcess,'ExitProcess',\
         WinExec,'WinExec'

  import small,\
         Autotype,'Autotype'
    


Description:
Download
Filename: Fasm+Dll.zip
Filesize: 16.13 KB
Downloaded: 128 Time(s)


_________________
Code it... That's all...
Post 06 Apr 2005, 20:00
View user's profile Send private message Visit poster's website Reply with quote
Giedrius



Joined: 13 Feb 2005
Posts: 40
Location: Lithuania
Giedrius
Thanks Very Happy And can you maybe do an example for fasm dll usage in C++? (Or I should use that polib?)

_________________
Better to rule in hell, than to be a slave in heaven...
Post 07 Apr 2005, 12:50
View user's profile Send private message Reply with quote
Vortex



Joined: 17 Jun 2003
Posts: 318
Vortex
Giedrius,

The critical problem to use Fasm DLLs with VC++ is to create import libraries for MS link.exe

I coded a tool converting Masm include files to MS COFF import libraries.

Quote:

Include to library file converter V1.1
Inc2lib converts a MASM include file to the associated import library. To create the libraries, the tool launches Pelle's linker Polib. The location of Polib should be defined via the environment variable PATH inc2lib accepts wildcards such *.inc The libraries can be used with COFF linkers MS link and Pelle's Polink


Assume that your Fasm DLL exports these functions belows:
Code:
func1 takes one parameter
func2 takes two paramaters
func3 takes four parameters
func4 has no parameters
    


Creating the Masm include file is easy , dllname.inc

func1 PROTO :DWORD ; one parameter
func2 PROTO :DWORD,:DWORD ; two parameters
func3 PROTO :DWORD,:DWORD,:DWORD,:DWORD
func4 PROTO ; no any paramaters

To get the import library:
Code:
inc2lib dllname.inc
    


Include to library file converter V1.1

http://vortex.masmcode.com/files/i2l11.zip


Description:
Download
Filename: importlib.zip
Filesize: 1.08 KB
Downloaded: 123 Time(s)


_________________
Code it... That's all...
Post 07 Apr 2005, 18:55
View user's profile Send private message Visit poster's website 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-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.