Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Converting between java string and BSTR

843829Dec 7 2005 — edited Dec 8 2005
Hi, i am a newbie to native methods. Previous method tries to take the javastring, converts to BSTR and passes it to a visual basic application. Encoding is important and it must support non UTF-8 characters. The problem is that it passes only first character of all string. I couldn't comment where the problem is. Thanks in advance...
BSTR _stdcall getMessageAsText(){
	jmethodID id = env->GetMethodID(testClass, "checkSendingTextMessage", "()Ljava/lang/String;");
	jstring strJava = (jstring) env->CallObjectMethod( testObject, id );
	
	BSTR retValue = NULL;
	env->ExceptionDescribe();
	
	if (strJava != NULL){
		jboolean isCopy = 0;
		const jchar *data = env->GetStringChars(strJava, &isCopy);
		int length = env->GetStringLength(strJava);
		char* rtn = (char *)malloc( length*2+1 );
		int size = 0;
		size = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)data, length, rtn, (length*2+1), NULL, NULL );
		if( size <= 0 )
			return NULL;
		retValue = ::SysAllocString((WCHAR *)data);
		if (!isCopy)
			env->ReleaseStringChars(strJava,data);
		
		return retValue;
	}

	return NULL;
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 5 2006
Added on Dec 7 2005
3 comments
1,025 views