Hey all, I was wondering how to import some Win32 ...
# kotlin-native
s
Hey all, I was wondering how to import some Win32 APIs in Kotlin/Native. Basically, I need to convert this C call to a Kotlin/Native one, but I can’t seem to understand if those headers are included in
platform.windows.*
or
platform.posix.*
.
Copy code
#include <Winsock2.h>
#include <Ws2bth.h>
#include <BluetoothAPIs.h>
Those are standard Win32 headers but I can’t find them anywhere on Kotlin/Native. I am developing on macOS 12.4 with an M1 processor, if it matters. Do I need to use cinterop and bind them manually? Or am I looking in the wrong place? Thanks in advance!
l
You can always add a .def file in the nativeInterop/cinterop folder giving K/N the header files and link options if it is not included by default. https://kotlinlang.org/docs/native-c-interop.html#create-bindings-for-a-new-library
s
Do I need to have them in my path in order to use them? Do I need to specify the full path? I’m not familiar with C development and the documentation does not answer those questions. Say I want to have a cinterop for BluetoothAPIs.h, which is a windows-only library. Do I simply add
Copy code
headers = BluetoothAPIs.h
to my cinterop file? Or do I need to use a specific path? Will I be able to use this cinterop on windows only?
l
It works the same as in C. There's two useful properties, linker path and header path. Each can have multiple paths to search in. It will search the first path and see if it finds the file, if not, it will search the second, and so on. If the header is in the system search path (mainly system libraries or installed packages if your OS has a package manager), you shouldn't need to add a search path, but you will if you have a custom location.
For non-system libraries, I usually create a prebuilts folder and put the headers and binaries in that.
s
So if I want to use windows-specific headers, I have to compile the project on a windows machine? For instance
Winsock2.h
won’t be available on the path of a macOS machine. But if I need to use it in my mingwMain, I have to compile and develop the project on a windows machine?
Those questions are also because I could not find those Win32 APIs on
platform.windows.*
. If they are available, please correct me.
Or maybe I need to use Visual Studio to have those APIs available in macOS?
l
If you build on anything other than Windows, Windows-only headers and binaries won’t exist. I personally would make a prebuilts folder that has them in your repo. If you put the prebuilts folder on the search path, it should still search the normals Windows place first, then search your folder if it can’t find it.
But others may have better ways to do this. I don’t think building a Windows-only library on a non-Windows machine has been a super common use case before cross-platform started to exist.
s
Alright, I’ll try and do that. I’ll grab a windows machine and create a prebuilt folder (or maybe use Parallels if I can). Thank you for your time.