FFI Library Question

General discussion forum for SDStudio Design Environment
Post Reply
wilbert2940
Registered User
Posts: 61
Joined: Fri Feb 02, 2018 7:44 am

FFI Library Question

Post 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
English is not my native language.
User avatar
Serkan
Developer
Developer
Posts: 216
Joined: Thu Jul 04, 2013 8:44 pm
Location: Windows.h
Contact:

Post by Serkan »

Hi, Need dll file to test
wilbert2940
Registered User
Posts: 61
Joined: Fri Feb 02, 2018 7:44 am

Post 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 1020 times
English is not my native language.
wilbert2940
Registered User
Posts: 61
Joined: Fri Feb 02, 2018 7:44 am

Post by wilbert2940 »

Hello, did you test?
English is not my native language.
User avatar
Serkan
Developer
Developer
Posts: 216
Joined: Thu Jul 04, 2013 8:44 pm
Location: Windows.h
Contact:

Post 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);
wilbert2940
Registered User
Posts: 61
Joined: Fri Feb 02, 2018 7:44 am

Post by wilbert2940 »

Thanks, I was able to write the correct code with your help.
English is not my native language.
Post Reply