1. A Stupid Question : A DLL Wrapper

If a DLL function accepts a string, how do I write the defining code?

Alex

new topic     » topic index » view message » categorize

2. Re: A Stupid Question : A DLL Wrapper

Alex Chamberlain wrote:

> If a DLL function accepts a string, how do I write the defining code?

It depends, e.g. whether the DLL was written in C or in Euphoria. You
should have a documentation for the concerning DLL, or at least for the
function you are interested in.

Here is an example for wrapping the function "DeleteFileA", which is in
"kernel32.dll" as part of the Windows API. It accepts a string (= the
concerning file name) as parameter.

include dll.e
include machine.e

constant
   KERNEL32 = open_dll("kernel32.dll"),
   DEL_FILE = define_c_func(KERNEL32, "DeleteFileA", {C_POINTER}, C_LONG),
   ERROR = -1

global function del_file (sequence fileName)
   atom lpFilename, ret

   if DEL_FILE != ERROR then
      lpFilename = allocate_string(fileName)
      ret = c_func(DEL_FILE, {lpFilename}) - 1
      free(lpFilename)
      return ret            -- -1 on error
   end if
   return ERROR
end function


-- Demo
sequence file
file = "test.txt"

puts(1, "'" & file & "' ")
if del_file(file) then
   puts(1, "not deleted.")
else
   puts(1, "successfully deleted.")
end if


Regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

3. Re: A Stupid Question : A DLL Wrapper

Alex Chamberlain wrote:
> 
> If a DLL function accepts a string, how do I write the defining code?
> 

You have to pass a pointer to the string allocated in memory (plus a zero
at the end of the string, which allocate_string() does for you).
Assuming you had something like this in the dll:

    void foo( lpzstring * bar )

You would wrap it like this:
include machine.e
include dll.e
constant
foo_dll = open_dll( "foo.dll" ),
foo = define_c_proc( foo_dll, "foo", {C_POINTER} )

global procedure Foo( sequence bar )
    atom bar_ptr
    bar_ptr = allocate_string( bar )
    c_proc( foo, {bar_ptr})
    free( bar_ptr )
end procedure


Matt Lewis

new topic     » goto parent     » topic index » view message » categorize

4. Re: A Stupid Question : A DLL Wrapper

Thanks Matt & Juergen

I have actually used another library (ab_dll) to do it for me!

Alex

new topic     » goto parent     » topic index » view message » categorize

5. Re: A Stupid Question : A DLL Wrapper

Just posted the results of my questions - a "UCalc Wrapper and Calculator" (for
Windows).

"DLL Wrapper of ucalc32.dll - a very fast and accurate maths expressions
evaluator. Also included is a calculator that uses the program and the original
download zip from ucalc.com, which includes wrappers for C++, VB and Delphi -
still have alot to do."

Thanks alot,
 Alex

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu