神
12mo ago

What should I specify to pass a "char *" in Deno.dlopen?

For example, this is how you would use it in Python:
import ctypes

lib = ctypes.cdll.LoadLibrary('example.dll')
check = lib.check
check.argtypes = [ctypes.c_char_p]
check.restype = ctypes.c_char_p
import ctypes

lib = ctypes.cdll.LoadLibrary('example.dll')
check = lib.check
check.argtypes = [ctypes.c_char_p]
check.restype = ctypes.c_char_p
10 Replies
神
12mo ago
https://deno.land/manual@v1.36.0/runtime/ffi_api#supported-types I looked here but couldn't find the appropriate type Not familiar with native code
Deno
Foreign Function Interface | Manual | Deno
As of Deno 1.13 and later, the FFI (foreign function interface) API allows users to call libraries written in native languages that support the C ABIs (C/C++, Rust, Zig, V, etc.) using Deno.dlopen.
NeTT
NeTT12mo ago
buffer should do if it is from the JS side pointer if from Native side check out denonomicon https://denonomicon.deno.dev/types (tho the styles are broken atm)
神
12mo ago
Using buffer gets a funny response. [Object: null prototype] {} So pointer is probably the correct answer. Learn about the pointer. Given pointer, it asks for Deno.PointerValue as its argument, so it tries to find a way to convert the string to Deno.PointerValue. Thank you!
NeTT
NeTT12mo ago
are you passing a char * to the native code? or are you sending it from native code to JS?
神
12mo ago
I send it to a Go program compiled from Deno to dll.
func check(ExampleArg *C.char) *C.char {
// do anything
return C.CString('a')
}
func check(ExampleArg *C.char) *C.char {
// do anything
return C.CString('a')
}
NeTT
NeTT12mo ago
ah idk how it is done generally but I can provide an example of how I do it I convert my text into a Uint8Array with a TextEncoder and pass it as a buffer type the native code can parse the byte stream as a string or whatever I also pass an additional Uint8Array to which the native code writes the response and I parse it in the JS side after the FFI call
神
12mo ago
const library = Deno.dlopen('example.dll', {
check: {
parameters: ['buffer'],
result: 'buffer',
nonblocking: true,
},
});

const response = await library.symbols.check(new TextEncoder().encode('a'));
console.log(response);
const library = Deno.dlopen('example.dll', {
check: {
parameters: ['buffer'],
result: 'buffer',
nonblocking: true,
},
});

const response = await library.symbols.check(new TextEncoder().encode('a'));
console.log(response);
[Object: null prototype] {} The console displays the above results I thought there was a problem with the dll, but I suspect there is a problem with my code because I called it from Python and it successfully asked for a response Sorry [Object: null prototype] {} I looked at it and decided it wasn't the right response, but when I correctly parsed the results, it worked. I mistakenly thought that an empty object was being returned. Thank you 😢
NeTT
NeTT12mo ago
<:hooray_deno:1035517542200004688> result: 'buffer' doesn't do anything different from pointer as parameter types, they are different but as return types, they are the same
神
12mo ago
I see, I learn a lot! Thank you so much😭
NeTT
NeTT12mo ago
<:cookie_deno:1002977285734932480>