Page 1 of 2
Export functions in an EXE (just like a dll) - uses coffIT
Posted: Wed Feb 03, 2010 7:59 pm
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...
- 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.
- 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...
Re: Export functions in an EXE (just like a dll) - uses coffIT
Posted: Wed Feb 03, 2010 8:07 pm
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!
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'.
Re: Export functions in an EXE (just like a dll) - uses coffIT
Posted: Wed Feb 03, 2010 8:20 pm
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 :
(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.
Re: Export functions in an EXE (just like a dll) - uses coffIT
Posted: Wed Feb 03, 2010 8:23 pm
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.
Re: Export functions in an EXE (just like a dll) - uses coffIT
Posted: Wed Feb 03, 2010 9:12 pm
by ts-soft

, thx
Re: Export functions in an EXE (just like a dll) - uses coffIT
Posted: Thu Feb 04, 2010 12:00 am
by SFSxOI
Very nice. Thank You srod

Re: Export functions in an EXE (just like a dll) - uses coffIT
Posted: Thu Feb 04, 2010 12:12 pm
by srod
You're welcome.
I have edited my last post above in order to comment upon more complex programs. A slight omission on my part!
Re: Export functions in an EXE (just like a dll) - uses coffIT
Posted: Thu Feb 04, 2010 1:54 pm
by Kwai chang caine
Hello it's your furuncle
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
Well KCC don't be affraid and try to understand and do like his MASTER explain
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
It's that the magical life of KCC !!! 
Re: Export functions in an EXE (just like a dll) - uses coffIT
Posted: Thu Feb 04, 2010 2:07 pm
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.
Re: Export functions in an EXE (just like a dll) - uses coffIT
Posted: Thu Feb 04, 2010 2:21 pm
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
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 ...??

Re: Export functions in an EXE (just like a dll) - uses coffIT
Posted: Thu Feb 04, 2010 2:23 pm
by srod
Let me try the download. Win2000 should make no difference.
Re: Export functions in an EXE (just like a dll) - uses coffIT
Posted: Thu Feb 04, 2010 2:26 pm
by srod
Runs fine here with PB 4.41 under Vista.
I really cannot see as Win 2000 will make any difference.
Re: Export functions in an EXE (just like a dll) - uses coffIT
Posted: Thu Feb 04, 2010 2:29 pm
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.
Re: Export functions in an EXE (just like a dll) - uses coffIT
Posted: Thu Feb 04, 2010 2:35 pm
by Kwai chang caine
You are too strong ...
You are Mister SROD SUN
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

Re: Export functions in an EXE (just like a dll) - uses coffIT
Posted: Thu Feb 04, 2010 2:38 pm
by Kwai chang caine
You know where is the REG KEY of PB, for that i can modify it ???? :roll: