Re: DLL Interface

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

You could use either version of the dll. The VB version is alot simpler to implement but may not offer the full functionality of the API.

For any dll, you need to import the code you need to use into euphoria.

First, you must open the required dll and store it's handle.

constant DLL = open_dll("db1api.dll") 

then for each function you want to import, you will have to define it for euphoria

-- Declare Sub DB1ValStringGet_VB Lib "db1api.dll" (ByVal hUser As Long, ByVal vString As Long, ByVal res As String, ByVal max As Integer) 
constant C_DB1ValStringGet = define_c_proc(DLL,"DB1ValStringGet_VB",{C_LONG,C_LONG,C_POINTER,C_UINT}) 

Notice above that the defined variable C_POINTER corresponds with ByVal res as String. It's actually the memory address of the string that is passed, not the chars themselves.

C strings are terminated with a byte value of 0, so to read the string, you peek each byte starting from the string's address until you encounter a value of 0. (which is what peek_string() does in the code below)

For ease of use, you should also define a wrapper function, this is where you will handle converting char arrays to euphoria sequences and simplify the use of the API function in eu.

function DB1ValStringGet(atom hUser, atom vString) 
 atom buffer 
 integer MAX_LEN 
 sequence s 
   MAX_LEN = 255 
   buffer = allocate(MAX_LEN) 
   c_proc(C_DB1ValStringGet,{hUser,vString,buffer,MAX_LEN}) 
   s = peek_string(buffer) 
   deallocate(buffer) 
   return s 
end function 

I don't think that peek_string() is in the standard libraries, so here it is in case you need it. The optimized version is not very clear what it does, so I've provided a slow version as well.

-- unoptimized 
function slow_peek_string(atom ptr) 
 sequence s 
 integer c 
  c = peek(ptr) 
  while (c != 0) do 
    s &= c 
  end while 
  return s 
end function 
 
-- optimized 
function peek_string(atom ptr) 
 integer len 
  len = 0 
  while (peek(ptr+len) != 0) do 
   len += 1 
  end while 
  return peek({ptr,len)) 
end function 

You can read more about interfacing with C in platform.doc

HTH

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

Search



Quick Links

User menu

Not signed in.

Misc Menu