Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Java String to CFUrlRef via JNI?




On Jan 18, 2006, at 7:16 PM, Doug Zwick wrote:

Michael Hall wrote:

On Jan 18, 2006, at 5:39 AM, Mike P wrote:

I'm need to pass a sting into some c++ code and convert the string
to a CFUrlRef.  Does anyone have experience doing such a thing?  It
doesn't need to be passed back.  The c++ code will do what it needs
without returning any value.

I think the usual approach would be to convert the string to a char
or byte array which ever works better with what the native method
expects.

IIRC there is a version of the native routine to create a CFUrlRef
from a unicode character sequence directly, without having to muck

Working with JNIDirect I generally don't pass java string arguments directly for use with the...

const jchar *chars = (*env)->GetStringChars(env, my_jstring, NULL);

method mentioned by Matt. 
It is usually easier to work with either char or byte arrays and the method usually dictates this. Working with char arrays does seem to work fine when UniChar or UniChar array or pointer is called for...

From macnative.jnidirect.functions.CFStringFunctions

//CF_EXPORT
//CFStringRef CFStringCreateWithCharacters(CFAllocatorRef alloc, const UniChar *chars, CFIndex numChars);
public static native int CFStringCreateWithCharacters(int alloc, char [] chars, int numChars);

Used as in...

public static int applicationForInfo(OSType creator,FSRef appref,int [] urlref,char [] extchars) {
try {
JDirectLocking.acquire();
int extension = -1;
if (extchars != null)
extension = CFStringFunctions.CFStringCreateWithCharacters(0,extchars,extchars.length);

To get the java int value of the CFStringRef to work with. 
I usually try to model the linkage to match the native method signatures and there just aren't any non-JNI API's that match java String's.
The char's being 2 bytes I think usually have worked fine for the equivalent unicode character sequences.
I haven't been bitten yet that I'm remembering by any non-16 bit characters mentioned by Greg. I would almost have to guess that UniChar's are actually based off a 16-bit per character assumption as well. Ditto for wchar's would be my guess although the OS/X API's seem to favor the UniChar's over the wide characters type for some reason. 
Of course I could be wrong on all of this and just have been fortunate not to encounter any related problems so far. Not a particular expertise, I just have been going with what seems to work so far when I run into these.


Mike Hall        mikehall at spacestar dot net



Attachment: smime.p7s
Description: S/MIME cryptographic signature

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden

References: 
 >Re: Java String to CFUrlRef via JNI? (From: Doug Zwick <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.