UsingDLLs
Euphoria DLL Howto
Here is an introduction to writing a dll using Euphoria.
This is not to be confused with using an existing Windows dll or Linux .so library.
Tools needed
- Euphoria 4.02
- Openwatcom C win32 1.9
Step 1. Creating a simple DLL using Euphoria
Open your favorite text editor an enter following lines:
include std/console.e constant SCREEN = 1 global procedure HelloWorld() puts(SCREEN, " \n Hello, World! \n") any_key() end procedure
Now, save the file with targetDLL.ex name.
Open command prompt, navigate to the folder where targetDLL.ex located (leave it opened for further usage). Translate the Euphoria code to C and compile it as DLL using following command:
euc -con -dll targetDLL.exWhere:
- euc - is a Euphoria to C translator
- -con - to state that we do not want the GUI
- -dll - to state that we want to create a DLL
You should see the output like following:
Build directory: build-433938\ Translating code, pass: 1 2 3 4 generating Compiling with Watcom Compiling 1% init-.c Compiling 7% targetDLL.c Compiling 11% main-.c Compiling 15% console.c Compiling 19% get.c Compiling 23% error.c Compiling 26% io.c Compiling 30% machine.c Compiling 34% memconst.c Compiling 38% memory.c Compiling 42% dll.c Compiling 46% types.c Compiling 50% text.c Compiling 53% convert.c Compiling 57% search.c Compiling 61% filesys.c Compiling 65% datetime.c Compiling 69% math.c Compiling 73% rand.c Compiling 76% sequence.c Compiling 80% sort.c Compiling 84% wildcard.c Compiling 88% pretty.c Compiling 92% serialize.c Compiling 96% graphcst.c Linking 100% ..\targetDLL.dll
The test DLL is ready, we can go to next step.
Step 2. Using your external DLL with a Euphoria program
Open the text editor and enter following lines:
include std/dll.e atom thedll integer theroutine -- step 1. Open the DLL thedll = open_dll("targetDLL.dll") -- step 2. Define the routine inside the DLL theroutine = define_c_proc(thedll , "HelloWorld", {}) -- step 3. Execute the routine c_proc(theroutine)
Now, save the file with openDLL.ex name, and execute it in command prompt:
openDLL.exYou should see the output like following:
Hello, World! Press Any Key to continue...
Conclusion
Thats all. We called C procedure "HelloWorld", stored in compiled DLL file from the Euphoria program. This is the basics, it does not cover how to handle the variables, it does not cover how to deal with "real" C/C++ coded DLLs, and I'd like that basics will be extended in following way:
- Include some variables to deal with it
- Build the test DLL using C-source code and with MinGW
- Build the test DLL using C++ source code and with MinGW
- Finally (to make it brilliant) create a real step by step example based on some popular DLL (ex. lame)