Вот мой код на golang:
Код: Выделить всё
package main
import (
"C"
)
import (
regexp "github.com/wasilibs/go-re2"
)
//export Matches
func Matches(input *C.char, regex *C.char) *C.char {
var re *regexp.Regexp
regexBytes := []byte(C.GoString(regex))
regexClone := string(regexBytes[:])
re, _ = regexp.Compile(regexClone)
match := re.MatchString(C.GoString(input))
if match {
return C.CString("1")
} else {
return C.CString("0")
}
}
func main() {}
А для сборки DLL я установил tdm64-gcc и использовал команду:
Код: Выделить всё
go build -buildmode=c-shared -o lib/libcustom.dll .\custom.go
Код: Выделить всё
package com.example.demo;
import com.example.demo.jna.regex.RegexLibrary;
import com.sun.jna.NativeLibrary;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
public class DemoApplication {
public static void main(String[] args) {
NativeLibrary.addSearchPath("custom", "C:\\Users\\placplac\\github\\test-golang\\lib");
Pointer pr = RegexLibrary.INSTANCE.Matches("1", "[0-9]");
boolean match = pr.getString(0).matches("1");
Native.free(Pointer.nativeValue(pr));
}
}
----
package com.example.demo.jna.regex;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
public interface RegexLibrary extends Library {
RegexLibrary INSTANCE = Native.load("custom", RegexLibrary.class);
Pointer Matches(String input, String regex);
}
Простой обходной путь: запускайте Native.free(Pointer.nativeValue(pr)) только в ОС Unix, потому что я Знайте, что ОС Windows предназначена только для локальной разработки.
Кто-нибудь знает, почему это происходит только в Windows?
Подробнее здесь: https://stackoverflow.com/questions/793 ... ree-memory
Мобильная версия