I have code that calls JNI functions from both a Silver/Island DLL and a Silver/Cocoa shared library. One of my functions receives a String and needs to pass it as an UnsafePointer to a JNI function:
In the Silver/Cocoa project I simply call someString.UTF8String and all is good. I haven’t figured out the equivalent call in the Silver/Island project. Am I missing something obvious?
You can use Elements RTL’s cross-platform Encoding class, Encoding.UTF8.GetBytes(string). Or the lower-level Island RTL’s TextConvert.StringToUTF8(string) to get a byte array. you’ll have to padd a #0 char, and then you can pass that as C-compatible char*.
Both of these provide me with a UInt8[], which is the same thing that would get returned by String.ToByteArray() - how would I get from the UInt8[] to the UnsafePointer?
I would suggest to write a small xml-comment over that function, that one can immediately see, that he needs to add the “0\”, would surely save a lot of threads here I guess
It’s defined as a String. If I click it in Water it says it’s an Object.
Here’s a minimalistic test solution. It doesn’t do anything but you may observe this behaviour if you open it in Water and look at the SharedProject code.
It compiles but String shouldn’t be seen as an Object, I believe. I should have put in the body of that function but just try and access value.ToAnsiChars() and you’ll see there’s something amiss.
The problem is you’re using Elements RTL, so your String is an Elements RTL String, not a platform string. even though thats a mapped type, the mapping hides the platform-speciifc APIs and only the cross-platform members defined in Elements RTL String are accessible. You must cast it to RemObjects.Elements.System.String or PlatformString to access platform-speiifc members — which isToAnsiChars is.
This didn’t; change recently though, this has always been the case.
Thanks for clearing this up. I was confused because if I try to edit this using Fire on macOS, it didn’t complain when ‘building’ or edition the Windows DLL project (not that I expect to build binaries for Windows on the Mac, mind you).
strange. it fails for me in Fire the same way. and fwiw, yeah, you can build Island/Windows apps on the Mac just fine. you can even debug them via SSH, if you like