How do you translate the following WinAPI C code? ...
# kotlin-native
e
How do you translate the following WinAPI C code?
Copy code
struct addrinfo *result = NULL, *ptr = NULL, hints;
ZeroMemory(&hints, sizeof(hints));
getaddrinfo(..., ..., &hints, &result);
o
it’s hard to help without declarations, but I believe this should help: https://kotlinlang.org/docs/mapping-struct-union-types-from-c.html#create-struct-and-union-as-cvaluesref-t I think, that it should look something like (could be wrong, not checked in IDE):
Copy code
val result = allocPointerTo<addrinfo>()
val hints = alloc<addrinfo>()
ZeroMemory(hints.ptr, sizeof<addrinfo>())
getaddrinfo(..., ..., hints.ptr, result)
e
Thanks @Oleg Yukhnevich, where did you find
allocPointerTo
?
Oh, ok. While in a
memScoped
block, or using
nativeHeap
directly.
o
yeah, all those are extension functions on
MemScope
(or subclass)
all
alloc
should be called in memory scope
e
@Oleg Yukhnevich is the memory initialized/zeroed by default? Or is it done using other functions
o
AFAIK it’s not zeroed
e
zeroValue
might do the trick, to use in place of alloc 🤔
image.png
o
ah, looks like you need pointer to a pointer here so may be you need something like this
result = allocPointerTo<CPointerVar<addrinfo>>
e
passing
result.ptr
also matches the signature
o
yeah, you are right, that should be correct… totally forgot all those things 😞
e
I'm trying to get used to the Kotlin terminology here, too many functions lol
rawPtr, ptr, pointed, value
o
rawPtr - should not be used really 🙂 ptr - rather a hack IMO pointed - rather strait-forward for me value - yeah, not that strait-formard So I agree 🙂 that’s why in my experiment on MPP FFI Im trying to rework everything from scratch 🙃 So, if you have any issues/ideas how to make it better, I will be very glad to hear them
thank you color 1
e
So to recap, this
Copy code
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
Can be translated to
Copy code
val wsaData = alloc<WSADATA>()
WSAStartup(0x202u, wsaData.ptr)
And this
Copy code
WSADATA *wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
Can be translated to
Copy code
val wsaData = allocPointerTo<WSADATA>()
WSAStartup(0x202u, wsaData.value)
Am I correct?
o
hm what is this function accepts, cause even in C example in first case it receives pointer but in second pointer to a pointer, isn’t it?
e
Oh the first case is the only right one, I was just making up a second option to understand how it would translate
o
the idea is, that
p=alloc<TYPE>
will create struct(or primitive) variable (first case, in
TYPE p
) and you can access it pointer via
.ptr
p = allocPointerTo<TYPE>
will create a pointer variable (so in C it will be
TYPE *p
) and then by
.ptr
you can get pointer to a pointer and by
.value
you can get variable on which pointer is referenced I think, that all of this is described in doc (may be with other words and examples)
e
@Oleg Yukhnevich thanks, that explanation makes it all have more sense
o
just in case, you can look at
allocPointerTo
declaration, it’s just a simple shortcut for
alloc<Pointer<TYPE>>
, but because of some implementation details it could be cumbersome 🙂
👀 1
glad that I helped you!
e
One last question. If I want to pass in a byte array that acts as a buffer, is the following correct?
o
overall, almost everything on what im writing here is in https://kotlinlang.org/docs/native-c-interop.html (and 5 tutorials under this section) And now, it’s just not really easy to start with cinterop, but when you spend some time and read FULL documentation, almost everything is now looks easy 🙂
it depends on what do you want to archive Overall, you can do like this, and then do soemthing like
buffer.copyTo
(don’t remember the correct name of function) Or you can create ordinary
ByteArray
and use pinning (https://kotlinlang.org/docs/native-c-interop.html#object-pinning) - example in doc uses
recv
🙂
e
Thanks! I guess what I'd like to see is more documentation for native functions and classes, almost nothing seems documented for c-interop
Like, in-code docs, not on the website
o
yeah… that’s known issue
e
Maybe I'm used to the Java world too much. I remember the C++ days where all the docs were found on cppreference. But yeah, it would make devex a lot better
o
for now, cinterop has really hard learning curve you need to know both C and Kotlin semantics and also specifics of kotlinx.cinterop package, as it’s a mix of conceptions from both languages 🙂 but after some time and reading a lot of docs, you starting to understand everything and why it works like this… Hopefully it will change some day
e
Isn't KMP going towards stable? I guess at some point docs are part of stabilization
o
cinterop != KMP 🙂 KMP goes stable this/next year (as from announcements on KotlinConf and in blog posts) in this scope K/N stdlib is stabilising and all declarations in
kotlinx.cinterop
are now marked as
Experimental
starting from 1.9 YT issue: https://youtrack.jetbrains.com/issue/KT-57728 Blog post: https://kotlinlang.org/docs/whatsnew19.html#the-kotlin-native-standard-library-s-journey-towards-stabilization Even before, as far as I understand, cinterop wasn’t marked as stable, but now it’s explicitly stated
e
They were evolved in an ad-hoc manner with a clear understanding that this is a temporary solution that will be thrown away
Related to the cinterop types. Damn 👀
So at some point native projects will have to get rewritten
o
yeap, partially 🙂 still, cinterop is not used a lot, as mostly stdlib/libraries provide good level of abstractions I think, that some migration will be available some day, if/when new API appears. Overall, creating such an interop tool is hard and need a lot of resources, but for now, there are not a lot of use cases, I mean, may be even less then 1 % of users of K/N need to interact with CInterop, as it’s low level thing.
gratitude thank you 1