RE: Using shared objects
Thank you! Your code worked perfectly.
I didn't test on Windows. (I don't have a windows box
at the moment to test on) So I'm not sure about the
whole bit about ...
__declspec(dllexport) void __stdcall
But, if I had to guess, (and this is only a guess) you
should only need that stuff if and only if you are
using a c++ compiler as opposed to a c compiler. I
compiled the dog.so using GCC which is a C compiler
and didn't need to make any changes to the C code. I
suspect if I compiled using g++, I would need to
make some changes.
I studied your code, cross referenced it with the eu docs,
and now I'm goint to take a crack at explaining it. Let
me know if I'm close.
atom dogLib -- used like a pointer
-- You would need to use "./dog.so" here if dog.so
-- is actually in the current directory or you could
-- use ./somedir/dog.so if the .so is nested deeper
-- relative to the current directory
-- load the dll and return a pointer to its mem location
dogLib = open_dll("./dog.so")
-- This is like porting the C header files to Euphoria
constant
mush_ = define_c_proc(dogLib, "mush", {C_POINTER, C_INT}),
bark_ = define_c_func(dogLib, "bark", {}, C_INT)
global procedure mush(sequence p)
atom lp -- used as a pointer
sequence lps -- used as an array of pointers
integer len -- len of p where p is expected to be a sequence of
sequences
len = length(p)
-- create a pointer to a block of mem 4 bytes long * the len of p
-- the reason for 4 bytes is because that's the size of a pointer
lp = allocate(len * 4)
-- delete any garbage in the memory locations
lps = repeat(0, len)
-- start making pointers to each sequence in p
for loop = 1 to len do
-- allocate enough mem for p[loop] + 1, strcpy the val
-- in p[loop] & "\0" to that location and return a
-- pointer to it.
lps[loop] = allocate_string(p[loop])
end for
-- poke4(atom where_p, object what)
-- put the 4 pointers just created into the lp mem location
poke4(lp, lps)
-- call the c function (procedure since the C return val is void)
c_proc(mush_, {lp, len})
-- look ma... mem leaks in euphoria are posible. :)
for loop = 1 to len do
free(lps[loop]) -- free the strings
end for
free(lp) -- free the mem storing pointers to the strings
end procedure -- how'd I do??
global function bark()
return c_func(bark_, {})
end function
|
Not Categorized, Please Help
|
|