Page 1 of 1

FFI Library Question

Posted: Mon Mar 11, 2024 8:06 pm
by wilbert2940
Hello Sirkan
I am using this environment to build my software and I need your help if you still see this forum.
I have a dll that has a function that takes a string argument and returns a string output. Of course, the string is in Arabic.
No matter how hard I tried, I could not use the FFI library for this.
In Autoplay this is easily done.

function Number2Letter(num)
  return DLL.CallFunction("AutoPlay\\Docs\\L2N.dll", "Number2Letter", "\""..num.."\"", DLL_RETURN_TYPE_STRING, DLL_CALL_STDCALL);
end

Please help me to implement the above codes in sdstudio. Thankful

Re: FFI Library Question

Posted: Tue Apr 02, 2024 6:14 pm
by Serkan
Hi, Need dll file to test

Re: FFI Library Question

Posted: Wed Apr 03, 2024 6:24 pm
by wilbert2940
Hello
The quality and power of this incomplete environment is more than the new autoplay update.
I still prefer to write my software with this environment.
L2N.zip
(4.83 KiB) Downloaded 207 times

Re: FFI Library Question

Posted: Wed Apr 10, 2024 6:18 am
by wilbert2940
Hello, did you test?

Re: FFI Library Question

Posted: Fri Apr 12, 2024 9:36 pm
by Serkan
SDStudio is designed to fast and CPU/Memory efficient applications.
Therefore, things working different in this environment.

When you use that function, it makes everything in background, maybe it might be looking simply to use,
but you have no control over the process.

So, SDStudio allows developer to create these steps their selves. as they wish
In short you need to create a memory entry for every argument and return value, regardless of if an argument is not a pointer,
FFI lib automatically handles these,

It also allows you to load a library only once, instead of a load call on every function call.
In addition to this getting function pointer and defining args on every function call,
So, in SDStudio you do these only once and call function as much as you want.

Based on your question, it is unclear if your DLL is a UNICODE or ANSI dll , and what is pushed as the arguments if it is a number or string
My reply is as a general reply, and might not fit your exact requirement but, you can easily adjust based on your needs.

For every argument you need to create a memory pointer using FFI:MemoryNew and provide its return value as real argument.
You need to do this for return value as well, and after function call succeeded you can get these values using FFI:MemoryGet function.

In short you will use memory addresses as the arguments and return values

Here is a simple code to get you started, on my tests it returns some characters, but like i said it is unclear if your DLL is Unicode or Ansi or exact arg types etc..

Code: Select all

local hLibrary = FFI:LoadLibrary("L2N.dll");
local hFunction = FFI:GetFunction(hLibrary,ABI_STDCALL,"Number2Letter",WCHAR_PTR,{WCHAR_PTR});

local hArg1 = FFI:MemoryNew(WCHAR_PTR,3,-1,"hello");
local hReturn = FFI:MemoryNew(WCHAR_PTR,100,100,"");
local hResult = FFI:CallFunction(hFunction,hReturn,hArg1);

local nResult = Dialog:MessageBox("Info",FFI:MemoryGet(hReturn),MB_OK,MB_ICONINFORMATION,MB_DEFOPTION1);

Re: FFI Library Question

Posted: Fri Apr 19, 2024 2:42 pm
by wilbert2940
Thanks, I was able to write the correct code with your help.