Wrapping DLLs in Euphoria (was: [OT] Swap RGB)

new topic     » topic index » view thread      » older message » newer message

Ricardo Forno wrote:

<snip>

> By the way, I don't really know how to deal with DLLs.
> Where can I find an explanation? I know how to program in C,
> but only at the applicaton level.

I do not know any tutorial for doing this with Euphoria.
However, the basic stuff -- which covers at least 80% of what I
personally need -- is easy. I'll start writing some basic things.
As far as more advanced stuff is concerned, and when I make mistakes
or forget something, hopefully others will correct or complement the
text.

a) You cannot do without the documentation of the regarding DLL.
b) Tell your Eu programm about the DLL and define the desired
   routines/variables inside the DLL.
c) Use the previously defined routines/variables.


ad a)
Of course often it's useful to wrap API functions of the operating
system. Unfortunately, I know little about Linux, so I'll write only
about Windows here. I find the documentation of the Windows API somewhat
unsorted. However, normally I use this as entry:

     Windows API Reference
     http://msdn2.microsoft.com/en-us/library/aa383749.aspx

You'll find there documentation of API functions, and of special
structures that are sometimes needed as well (say e.g. SYSTEMTIME).

Some functions need special constants. Their names are mentioned in the
documentation, but normally not their value. You'll find the value of
most constant in the header files of your C compiler (or in some User
contributions in the Eu archieves, I think).

Of many Windows API functions there are 2 versions: One with an "A" at
the end (e.g. "DeleteFileA", see below), and another one with a "W" at
the end. "A" denotes the ANSI version, "W" denotes the Unicode version.
If you want your program to be able to run on the old Windows 9x
systems, then it should only wrap the "A" versions.


ad b)
You'll need in Euphoria:
- open_dll()
- define_c_func()
- define_c_proc()
- define_c_var()


ad c)
You'll need in Euphoria:
- c_func()
- c_proc()

... and often also:
- allocate()
- allocate_string()
- peek()
- poke()
- free()

The Eu documentation of all these routines is good or very good IMHO.
A simple example follows:
include dll.e
include machine.e
include misc.e                                    -- for constant WIN32

atom Kernel32
integer DeleteFile

if platform() = WIN32 then
   Kernel32   = open_dll("kernel32.dll")
   DeleteFile = define_c_func(Kernel32, "DeleteFileA", {C_POINTER}, C_LONG)
end if

global function delete_file (sequence fileName)
   atom lpszFileName, ret

   if DeleteFile = -1 then
      return -1                                   -- error
   end if

   lpszFileName = allocate_string(fileName)
   ret = c_func(DeleteFile, {lpszFileName}) - 1
   free(lpszFileName)
   return ret                                     -- -1 on error
end function


-- Demo
sequence fileName

fileName = "trash.txt"

if delete_file(fileName) = -1 then
   printf(1, "Can't delete file '%s'.", {fileName})
else
   printf(1, "File %s' deleted.", {fileName})
end if
if getc(0) then end if


> Moreover, where can I get a description of the other routines in user32.dll?

On the MSDN website, there are lists where the API functions are sorted
alphabetically, or grouped by category. ATM I don't know about a list
where they are grouped by DLL. But as a rough estimate, I think that
functions that are in the same category often may be in the same DLL,
too.

> One thing that recently I wondered how is it done is how some programs
> intercept the Ctrl-C call to the clipboard, and then manage to store
> the clipboard contents in a file. Can this be done with some system dll?

Yes, it can. If you want, I can write something like that in Euphoria.

> Many thanks for your help.

You are welcome!

Regards,
   Juergen

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu