Kotlin C interop (LowLevelKeyboardProc callback function)

In which way, if possible, you can change the value of the pointer

What you mean by “change the value of the pointer” ?
C/C++ sample please.

Possible problems with the translator)
Change the value of a variable by pointer to it

pointer.pointed.somevariable = something
if it points to struct.

Or just
pointer.pointed = something
if it points to scalar.

Thank you so much, really thank you very much. Such people need “not advanced users”

Ну прочитай таки INTEROP.md

Для Котлина это CPointer<something>, для C something *

1 Like

Understood with errors, Clion helped fix Intellij was difficult.
Remains the last. I reread interop.md, did everything the same way as there and as you said.
fun LowLevelKeyboardProc(nCode:Int,wParam:WPARAM,lParam:LPARAM):LRESULT
SetWindowsHookExA(WH_KEYBOARD_LL,staticCFunction(::LowLevelKeyboardProc),hMod,0){
//TODO
return CallNextHookEx(hhk,nCode,wParam,lParam)
}

error: kotlinx.cinterop.staticCFunction must take an unbound, non-capturing function or lambda

Val cannot be reassigned

memScoped{
var = alloc()
var tid = buf.ptr
tid.pointed = <-----Val cannot be reassigned
}

Yes, in staticCFunction you can access only global immutable variables.

Do not uderstand, wrong formatting, use “```” for code.

I tried to create a function in object {}, but it did not help.

Very strange translation of the second Obzca) can be more detailed

Все равно не понял.
Вопрос то в чем?

В staticCFunction я передаю ссылку на функцию, но выходит ошибка staticCFunction must take an unbound, non-capturing function or lambda.
Что я делаю не так?

И по второму: пытаюсь по адресу памяти изменить его значение

Ну да, та функция которая передается в staticCFunction должна быть “unbound, non-capturing function or lambda”. Это написано в INTEROP.md. Только глобальные переменные верхнего уровня доступны из нее.

По второму - без примера - все равно не понял.

А в моем примере в чем проблема то не пойму, переписываю с kotlin на jvm, там все, через jna, работает. Но тут проблема появилась.
Что означает несвязная и не захватывающая?

По второму примеру вот(Delphi)
Btn1Click(Sender: TObject);
var
Buff: Byte;
RW: Cardinal;
begin
WriteProcessMemory(Application.Handle, $00B80034, @Buff, 1, RW);
end;

Something like this:

fun write(hProcess: HANDLE, Buff: LPCVOID) = memScoped {
    val Written = alloc<size_tVar>()
    WriteProcessMemory(hProcess, 0x00B80034L.toCPointer(), Buff, 1, Written.ptr);
    Written.value;
}

Or this:

fun write(hProcess: HANDLE, addr: Long, value: Byte) = memScoped {
    val Buffer = alloc<ByteVar>().also { it.value = value }
    val Written = alloc<SIZE_TVar>()
    WriteProcessMemory(hProcess, addr.toCPointer(), Buffer.ptr, 1, Written.ptr);
    Written.value;
}

Что означает несвязная и не захватывающая?
Как я если я правильно понял, я такую и передаю, или я ошибаюсь…

Ну то и значит - нельзя внутри нее обращаться ни к чему, кроме глобальных переменных.
То есть объявленных вне всех классов и объектов.
И даже их можно только читать а не писать, когда не из главного потока.

Есть вот такой вот код:
val msg= MSG.toCPointer()
while(true){
TranslateMessage(msg)
DispatchMessage!!(msg)
}

Выходит ошибка, как можно привести к типу CValuesRef<MSG /* = tagMSG */>?

error: unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public fun Long.toCPointer(): CPointer<MSG /* = tagMSG */>? defined in kotlinx.cinterop
val msg:CPointer = MSG.toCPointer()