2013年7月25日星期四

GetStringUTFChars (env, str, NULL) return value operational problems

See this post http://topic.csdn.net/t/20020917/14/1030420.html # r_achor
operate in accordance with that last example
using c + + files, connecting to another library is also achieved through code
 HINSTANCE HDll; 
lpTrimFun Trim; 
HDll = LoadLibrary (".. here is the path .. \ \ Trim_Dll.dll"); 
Trim = (lpTrimFun) GetProcAddress (HDll, "Trim"); 

landlord did not use the way: cl-Id: \ jdk13 \ include-Id: \ jdk13 \ include \ win32-LD java2dll.c myDll.lib-Fejava2dll.dll

In this case, because GetStringUTFChars () return value is const char * type, so Trim (strTemp); would have been an error: Unable to convert const char * char *

The landlord did not figure out there is how to achieve.
Also would like to ask how to convert GetStringUTFChars () return value in c + + as a normal string will be handled?
------ Solution ---------------------------------------- ----
You can then open a char *, then memcpy

------ Solution ------------------------------------ --------
Java version of what people understand C + +?
------ For reference only -------------------------------------- -

In http://www.blogjava.net/china-qd/archive/2006/04/29/44002.html to find a solution, and your idea is the same, do not use memcpy simple replication, but the use WideCharToMultiByte () were converted copy, because the encoding is inconsistent, uncertain direct memcpy will not appear on the Chinese garbled question, one would test the next.
now mainly do not understand, http://topic.csdn.net/t/20020917/14/1030420.html # r_achor This post was last given that example, why his trim can directly manipulate GetStringUTFChars () in return value?
------ For reference only -------------------------------------- -

test, the code is as follows:
 int length = (env) -> GetStringLength (str); 
const jchar * jcstr = (env) -> GetStringChars (str, 0); 
char * rtn = (char *) malloc (length * 2 +1); 
memcpy (rtn, jcstr, length); 
/ / Trim (rtn); 
printf ("% d \ n", length); 
printf ("% s \ n", rtn); 


str content is "i am the spider man!", but this copy output after only a i, the other nothing, length is 20, stands to reason that memcpy is reproduced in full, not because encountered string ends '\ 0' and the end, but here is very strange, but it seems the need for transcoding.
------ For reference only -------------------------------------- -

are aware of points does not hurt it
The problem is caused due jni, c + + is not a simple problem
------ For reference only --------------------- ------------------
 int length = (env) -> GetStringUTFLength (str ); 
const char * jcstr = (env) -> GetStringUTFChars (str, 0); 
char * rtn = (char *) malloc (length); 
memcpy (rtn, jcstr, length); 
printf ("% d \ n", length); 
printf ("% s \ n", rtn); 


this happen
------ For reference only -------------------------------- -------
estimate i in the second byte unicode encoding code is 0, memcpy stop copying when it is encountered, and all only i ...
------ For reference only ---------------------------------------
"How to convert GetStringUTFChars ( ) return value in c + + as a normal string will be to deal with? "
In fact, this layer does not need to have to JNI with jchar, char * can also be used directly.
char * str = (char *) env-> GetStringUTFChars (env, s, 0);
After this reading is right on the str incoming jString parameters read. But note, java in a String can not be changed, so str write operation does not return to the java layer s of. . .
------ For reference only -------------------------------------- -

add, but why use memcpy it, you can not directly use strcpy. . .

没有评论:

发表评论