Export functions in an EXE (just like a dll) - uses coffIT

Share your advanced PureBasic knowledge/code with the community.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Export functions in an EXE (just like a dll) - uses coffIT

Post by srod »

Hi,

in response to the thread : http://www.purebasic.fr/english/viewtop ... 13&t=40948, in which j50501313 wished to export a function, not in a dll, but in a regular EXE, it occurred to me that perhaps the simplest way of doing this is to use my coffIT tool.

It is a fact that executables can contain exported functions just like dll's and Purebasic's library functions can call these functions after using OpenLibrary() etc. The problem, however, is that the PB compiler will not presently allow us to export functions inside an EXE.

Two things are needed in order to export a function in an EXE as described...
  1. When the PB compiler creates the intermediate object (.obj) file (before the link stage), the name of the function to be exported from the final EXE must be placed within the object file's symbol table.
  2. The linker must be informed that the named function (which it will find in the aforementioned symbol table) is to be placed within the export section of the final EXE. The best way of doing this is by using a 'link file' through the IDE compiler options dialog.
Well, step i) is the most difficult step requiring that we grab ahold of the commented ASM file generated by the compiler, modify some code and some internal names throughout the ASM and then reassemble... a bit of a pain to be honest.

But then it occurs to me that my coffIT tool will do most of this automatically! :)

What follows in the next couple of posts within this thread are the steps required to complete a basic example of using coffIT to create an EXE containing an exported function. Other programs can then load this EXE via OpenLibrary() and call the functions as if it was a dll etc.

Post to follow...
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by srod »

First step then... download coffIT from the nxSoftware site. :)

Create a new folder, call it 'ExportFunctionInExe' say, and save the following code as "AddIntegers.pb" within this new folder :

Code: Select all

ProcedureDLL.i AddIntegers(a, b)
  Protected result = a + b
  ProcedureReturn result
EndProcedure 
Simple enough code I'm sure you agree! :wink:

Run coffIT and point it towards "AddIntegers.pb" and click the 'coffIT' button.

If no errors are reported by coffIT then you will find 3 new subfolders inside the 'ExportFunctionInExe' folder. Inside the 'Bin' subfolder you will find a newly created object file 'AddIntegers.obj". (This object file will hold the public symbol 'AddIntegers' in its symbol table as required by step i) in the above post.)

In the next post we create the executable file containing the exported function 'AddIntegers'.
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by srod »

Now that we have our object file 'AddIntegers.obj', we can create any executable we wish and which exports our AddIntegers function.

For example... save the following code as "AddIntegersExe.pb" in the same folder as the object file 'AddIntegers.obj' :

Code: Select all

Import "AddIntegers.obj"
  AddIntegers.i(a.i,b.i) As "AddIntegers"
EndImport

;The following will only run if the exe is run directly.
  x = AddIntegers(10, 20)

  MessageRequester("Heyho!", "AddIntegers(10, 20) returned " + Str(x)+#LF$+#LF$+"You will Not see this message when this EXE is loaded into another process just like a dll!")
Now, if you create an EXE from this code and run it you will of course be presented with the message requester. Also, the Import / EndImport construct will ensure that the 'AddIntegers.obj' object file (created previously) will be linked into the final EXE.

However, because there were no special instructions passed to the linker when we compiled the program, the AddIntegers function will not have been exported. That is, whilst our EXE physically contains the AddIntegers() function, it does not export it and thus we cannot call it from other programs.

What is needed is a way of instructing the linker to take that final step of ensuring that our function is exported.

Save the following as a text file ("link.txt") in the same folder as our object file :

Code: Select all

/EXPORT:AddIntegers
/FIXED:NO
(The /FIXED:NO option forces the linker to add a relocation .reloc section to the executable. Without this, the Windows loader will not be able to relocate the exe if required. Thanks to Mistrel for the heads up here.)

In the IDE compiler options dialog, select this text file in the 'Linker Options File'.

Now save and recompile the above code to create "AddIntegers.exe".

You now have an executable file in which the AddIntegers() function has been exported and can thus be used by other processes.

In the next post we test the new exe.
Last edited by srod on Sun Feb 14, 2010 2:23 am, edited 5 times in total.
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by srod »

Finally, a quick test to confirm that our newly created executable "AddIntegers.exe" really does contain the AddIntegers() exported function. Save the following test code in the same folder as the executable :

Code: Select all

If OpenLibrary(1, "AddIntegers.exe")
  Debug CallFunction(1, "AddIntegers", 10, 20)
  CloseLibrary(1)
EndIf
Notice how it loads the "AddIntegers.exe" as if it were a dll.

Run the program and, all things being ship shape and Bristol fashion, you should see the sum of 10 and 20 displayed.

:)

Finally, a quick note regarding more complex programs.
When using coffIT in this way to build an EXE with exported symbols, you will undoubtedly need to check the 'Create Purebasic wrapper' option. You will then need to add the generated wrapper file to your main program via XIncludeFile etc. (or just copy and paste). This will replace the Import/EndImport construct (as the wrapper file contains a copy of this anyhow) and will ensure that all library files required by our intermediate .obj file are linked in with the final EXE. With our simple example above, we were lucky in that there were no such dependencies.

For example, had we needed to generate a wrapper file with our above demo then our EXE code would have looked like :

Code: Select all

XIncludeFile "AddIntegers.pbi"  ;Our coffIT generated wrapper file.

;The following will only run if the exe is run directly.
  x = AddIntegers(10, 20)

  MessageRequester("Heyho!", "AddIntegers(10, 20) returned " + Str(x)+#LF$+#LF$+"You will Not see this message when this EXE is loaded into another process just like a dll!")
Note that we have removed the Import/EndImport construct and instead have included the coffIT generated wrapper file which contains all necessary imports.
Last edited by srod on Thu Feb 04, 2010 12:10 pm, edited 1 time in total.
I may look like a mule, but I'm not a complete ass.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by ts-soft »

Image, thx
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by SFSxOI »

Very nice. Thank You srod :)
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by srod »

You're welcome. 8)

I have edited my last post above in order to comment upon more complex programs. A slight omission on my part!
I may look like a mule, but I'm not a complete ass.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by Kwai chang caine »

Hello it's your furuncle :D

First...., i say thanks in advance, because like usually i have understand nothing...even not the title.
But all what my MASTER create is giant...so i can thanks in advance :mrgreen:

Well KCC don't be affraid and try to understand and do like his MASTER explain :oops:

1/ I have create folder c:\ExportFunctionInExe
2/ Put inside the big source code "AddIntegers.pb"

Code: Select all

ProcedureDLL.i AddIntegers(a, b)
  Protected result = a + b
  ProcedureReturn result
EndProcedure 
3/ I have download Coffit, selected the "AddIntegers.pb" in the "c:\ExportFunctionInExe" directory
And ...and ...and ...and ...and ...and ...
My MASTER wrote:If no errors are reported by coffIT
I have an error Line 1 structure no found :D
It's that the magical life of KCC !!! :mrgreen:
ImageThe happiness is a road...
Not a destination
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by srod »

Uhm Kwai.... which version of coffIT did you download?

I will guess that you are using the PB 4.2 version of coffIT but you are using PB 4.4!!!

Please download the correct version of coffIT.
I may look like a mule, but I'm not a complete ass.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by Kwai chang caine »

No no no my splendid MASTER....
Your servant his IDIOT..that's right ....but for one time, a little light of intelligence born in his "Sink" brain :D

I use the last PB version 4.41 and have download the 4.41 version
So i have W2000, it's perhaps that ??? :roll:

Say to me....you are impressed ...no...say to me ...?? :mrgreen:
ImageThe happiness is a road...
Not a destination
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by srod »

Let me try the download. Win2000 should make no difference.
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by srod »

Runs fine here with PB 4.41 under Vista.

I really cannot see as Win 2000 will make any difference.
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by srod »

Ah... wait a cotton picking minute?

Do you have several versions of Purebasic installed on your system? If so, then coffIT is using an older version of PB, probably version 4.2.

If this is the case then you will possibly need to rename your PB installation folders. coffIT trawls the registry looking for the entries pointing to the PB installation folder and it will probably find the one you originally used when installing the first version of PB.
I may look like a mule, but I'm not a complete ass.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by Kwai chang caine »

You are too strong ... 8)

You are Mister SROD SUN

Image

Efficiently...i have all the version of PB in one folder of PB, like this :

PB\v3.94\PureBasic.exe
PB\v4.20\PureBasic.exe
PB\v4.30\PureBasic.exe
PB\v4.41\PureBasic.exe

Too strong my excelent MASTER 8)
Last edited by Kwai chang caine on Thu Feb 04, 2010 2:40 pm, edited 1 time in total.
ImageThe happiness is a road...
Not a destination
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by Kwai chang caine »

You know where is the REG KEY of PB, for that i can modify it ???? :roll:
ImageThe happiness is a road...
Not a destination
Post Reply