Recent days to do a part-time, mainly the C language and C # language encapsulated DLL, after C + + to the Java language Support, the main problem is the type conversion, which lists the common types of conversion process.
1#include"stdafx.h"2#include <cstdlib>3#include <cstdio>4#include <iostream>5#include"Jstring_cstring_char_ptr.h"6#include <malloc.h>7#include <Windows.h>8 9 using namespacestd;Ten using namespacesystem::runtime::interopservices; One using namespaceSystem; A - //char*->c# -system::string^ charptrtocstring (Const Char*str) the { -system::string^ str1 = Marshal::P trtostringansi ((INTPTR) (Char*) str); - returnstr1; - } + - + //c#->char* A Char* CSTRINGTOCHARPTR (system::string^s) at { - Char* Buffer = (Char*)(void*) Marshal::stringtohglobalansi (s); - returnbuffer; - } - - in //jstring->char* - Char* JSTRINGTOCHARPTR (JNIENV *env, jstring jstr) to{//utf8/16 conversion into gb2312 + intLength = (env)getstringlength (JSTR); - Constjchar* jcstr = (env)->getstringchars (JSTR,0 ); the Char* Rtn = (Char*)malloc(length*2+1 ); * intSize =0; $Size = WideCharToMultiByte (CP_ACP,0, (LPCWSTR) jcstr, Length, RTN, (length*2+1), NULL, NULL);Panax Notoginseng if(Size <=0 ) - returnNULL; the(ENV)Releasestringchars (Jstr, jcstr); +Rtn[size] =0; A returnRtn; the } + - //char*->jstring $Jstring charptrtojstring (jnienv* env,Const Char*str) ${//gb2312 conversion into UTF8/16 -Jstring RTN =0; - intSlen =strlen (str); theUnsigned Short* Buffer =0; - if(Slen = =0 )WuyiRTN = (env)Newstringutf (str); the Else - { Wu intLength = MultiByteToWideChar (CP_ACP,0, (LPCSTR) str, slen, NULL,0 ); -Buffer = (unsigned Short*)malloc(length*2+1 ); About if(MultiByteToWideChar (CP_ACP,0, (LPCSTR) str, Slen, (LPWSTR) buffer, length) >0 ) $RTN = (env)->newstring (jchar*) buffer, length); - } - if(buffer) - Free(buffer); A returnRtn; + } the - //Jchar->char $ CharJchartochar (Jchar c) the { the CharCH = (Char) (C &0x00ff); the returnch; the } - in //char*->jchar theJchar Chartojchar (Charc) the { AboutJchar ch = (Jchar) (c |0xff00); the returnch; the } the + //Char->c# -System::char Chartochar (Charc) the {BayiSystem::char C = (System::char) ((int) (c)); the returnC; the } - - //C#->char the CharChartochar (System::char c) the { the CharC = (Char)((int) (c)); the returnC; -}
C # C + + Java interface type conversions