C # C ++ Java interface type conversion,
I have worked as a part-time engineer in the last few days. I mainly used C language and C # language-encapsulated dll to pass through C ++ to support Java. The main problem is type conversion, list the conversion process of common types here.
1 # include "stdafx. h "2 # include <cstdlib> 3 # include <cstdio> 4 # include <iostream> 5 # include" www_cnblogs_com \ cy1_dm0503.h "6 # include <malloc. h> 7 # include <Windows. h> 8 9 using namespace std; 10 using namespace System: Runtime: InteropServices; 11 using namespace System; 12 13 // char *-> c #14 System :: string ^ charptrtoCstring (const char * str) 15 {16 System: String ^ str1 = Marshal: PtrToStringAnsi (IntPtr) (char *) str); 17 return str1; 18} 19 20 21 // C #-> char * 22 char * Cstringtocharptr (System: String ^ s) 23 {24 char * buffer = (char *) (void *) marshal: StringToHGlobalAnsi (s); 25 return buffer; 26} 27 28 29 // jstring-> char * 30 char * jstringTocharptr (JNIEnv * env, jstring jstr) 31 {// UTF8/16 convert to gb231232 int length = (env)-> GetStringLength (jstr); 33 const jchar * jcstr = (env)-> GetStringChars (jstr, 0); 34 char * rtn = (char *) malloc (length * 2 + 1); 35 int size = 0; 36 size = WideCharToMultiByte (CP_ACP, 0, (LPCWSTR) jcstr, length, rtn, (length * 2 + 1), NULL, NULL); 37 if (size <= 0) 38 return NULL; 39 (env) -> ReleaseStringChars (jstr, jcstr); 40 rtn [size] = 0; 41 return rtn; 42} 43 44 // char *-> jstring45 jstring charptrTojstring (JNIEnv * env, const char * str) 46 {// gb2312 to utf8/1647 jstring rtn = 0; 48 int slen = strlen (str); 49 unsigned short * buffer = 0; 50 if (slen = 0) 51 rtn = (env)-> NewStringUTF (str); 52 else53 {54 int length = MultiByteToWideChar (CP_ACP, 0, (LPCSTR) str, slen, NULL, 0); 55 buffer = (unsigned short *) malloc (length * 2 + 1); 56 if (MultiByteToWideChar (CP_ACP, 0, (LPCSTR) str, slen, (LPWSTR) buffer, length)> 0) 57 rtn = (env)-> NewString (jchar *) buffer, length); 58} 59 if (buffer) 60 free (buffer); 61 return rtn; 62} 63 64 // jchar-> char65 char jchartochar (jchar c) 66 {67 char ch = (char) (c & 0x00ff); 68 return ch; 69} 70 71 // char *-> jchar72 jchar charTojchar (char c) 73 {74 jchar ch = (jchar) (c | 0xff00); 75 return ch; 76} 77 78 // char-> C #79 System: Char chartoChar (char c) 80 {81 System :: char C = (System: Char) (int) (c); 82 return C; 83} 84 85 // C #-> char86 char Chartochar (System :: char c) 87 {88 char C = (char) (int) (c); 89 return C; 90}