From 3d296d0ef1e1b3994f61fd8e141daac1ae18ab62 Mon Sep 17 00:00:00 2001 From: Zhen Tang Date: Mon, 8 Jul 2013 02:18:15 +0800 Subject: [PATCH] chinese characters can be handled correctly. --- WindowsMonitor/Native/Common.cpp | 25 +++++++++++++------ WindowsMonitor/Native/Common.h | 3 ++- WindowsMonitor/Native/LogicalDiskMonitor.cpp | 4 +-- WindowsMonitor/Native/MemoryMonitor.cpp | 3 ++- .../Native/NetworkInterfaceMonitor.cpp | 4 +-- src/main/java/org/bench4q/monitor/Main.java | 2 +- 6 files changed, 26 insertions(+), 15 deletions(-) diff --git a/WindowsMonitor/Native/Common.cpp b/WindowsMonitor/Native/Common.cpp index 1730984e..b913c7f0 100644 --- a/WindowsMonitor/Native/Common.cpp +++ b/WindowsMonitor/Native/Common.cpp @@ -1,17 +1,26 @@ #include "stdafx.h" #include -char * GetJString(JNIEnv * environment, jstring jstr) +char * GetJString(JNIEnv * environment, jstring str) { - jsize length = environment->GetStringLength(jstr); - const jchar * jcstr = environment->GetStringChars( jstr, NULL ); + jsize length=environment->GetStringLength(str); + const jchar * jcstr=environment->GetStringChars(str,NULL); int size=0; - char *str=(char *)malloc(length*2+1); - if((size=WideCharToMultiByte(CP_ACP,0,LPCWSTR(jcstr),length,str,length*2+1,NULL,NULL))==0) + char * ret=(char *)malloc(length*2+1); + if((size=WideCharToMultiByte(CP_ACP,0,LPCWSTR(jcstr),length,ret,length*2+1,NULL,NULL))==0) { return NULL; } - environment->ReleaseStringChars(jstr,jcstr); - str[size] = 0; - return str; + environment->ReleaseStringChars(str,jcstr); + ret[size]='\0'; + return ret; +} + +char * GetUTF8String(const wchar_t * str) +{ + int length=WideCharToMultiByte(CP_UTF8,NULL,str,(long)wcslen(str),NULL,0,NULL,NULL); + char * ret=new char[length + 1]; + WideCharToMultiByte(CP_UTF8,NULL,str,(long)wcslen(str),ret,length,NULL,NULL); + ret[length]='\0'; + return ret; } \ No newline at end of file diff --git a/WindowsMonitor/Native/Common.h b/WindowsMonitor/Native/Common.h index 147804d7..7697271f 100644 --- a/WindowsMonitor/Native/Common.h +++ b/WindowsMonitor/Native/Common.h @@ -2,4 +2,5 @@ #include -char * GetJString(JNIEnv * environment, jstring jstr); \ No newline at end of file +char * GetJString(JNIEnv * environment, jstring jstr); +char * GetUTF8String(const wchar_t * str); \ No newline at end of file diff --git a/WindowsMonitor/Native/LogicalDiskMonitor.cpp b/WindowsMonitor/Native/LogicalDiskMonitor.cpp index 42988abc..91ad09d3 100644 --- a/WindowsMonitor/Native/LogicalDiskMonitor.cpp +++ b/WindowsMonitor/Native/LogicalDiskMonitor.cpp @@ -14,7 +14,7 @@ JNIEXPORT jobjectArray JNICALL Java_org_bench4q_monitor_performance_windows_Logi for(iter=instances.begin();iter!=instances.end();iter++) { environment->SetObjectArrayElement(array,i - ,environment->NewStringUTF(Common::WideStringToString(*iter).c_str())); + ,environment->NewStringUTF(GetUTF8String((*iter).c_str()))); i++; } return array; @@ -32,7 +32,7 @@ JNIEXPORT jobjectArray JNICALL Java_org_bench4q_monitor_performance_windows_Logi for(iter=counterList.begin();iter!=counterList.end();iter++) { environment->SetObjectArrayElement(array,i - ,environment->NewStringUTF(Common::WideStringToString(*iter).c_str())); + ,environment->NewStringUTF(GetUTF8String((*iter).c_str()))); i++; } return array; diff --git a/WindowsMonitor/Native/MemoryMonitor.cpp b/WindowsMonitor/Native/MemoryMonitor.cpp index bda16258..657a5511 100644 --- a/WindowsMonitor/Native/MemoryMonitor.cpp +++ b/WindowsMonitor/Native/MemoryMonitor.cpp @@ -1,5 +1,6 @@ #include "stdafx.h" #include "MemoryMonitor.h" +#include "Common.h" JNIEXPORT jobjectArray JNICALL Java_org_bench4q_monitor_performance_windows_MemoryMonitor_getCounterList (JNIEnv * environment, jobject object) @@ -13,7 +14,7 @@ JNIEXPORT jobjectArray JNICALL Java_org_bench4q_monitor_performance_windows_Memo for(iter=counterList.begin();iter!=counterList.end();iter++) { environment->SetObjectArrayElement(array,i - ,environment->NewStringUTF(Common::WideStringToString(*iter).c_str())); + ,environment->NewStringUTF(GetUTF8String((*iter).c_str()))); i++; } return array; diff --git a/WindowsMonitor/Native/NetworkInterfaceMonitor.cpp b/WindowsMonitor/Native/NetworkInterfaceMonitor.cpp index 2139a8f5..ffaa2db9 100644 --- a/WindowsMonitor/Native/NetworkInterfaceMonitor.cpp +++ b/WindowsMonitor/Native/NetworkInterfaceMonitor.cpp @@ -14,7 +14,7 @@ JNIEXPORT jobjectArray JNICALL Java_org_bench4q_monitor_performance_windows_Netw for(iter=instances.begin();iter!=instances.end();iter++) { environment->SetObjectArrayElement(array,i - ,environment->NewStringUTF(Common::WideStringToString(*iter).c_str())); + ,environment->NewStringUTF(GetUTF8String((*iter).c_str()))); i++; } return array; @@ -32,7 +32,7 @@ JNIEXPORT jobjectArray JNICALL Java_org_bench4q_monitor_performance_windows_Netw for(iter=counterList.begin();iter!=counterList.end();iter++) { environment->SetObjectArrayElement(array,i - ,environment->NewStringUTF(Common::WideStringToString(*iter).c_str())); + ,environment->NewStringUTF(GetUTF8String((*iter).c_str()))); i++; } return array; diff --git a/src/main/java/org/bench4q/monitor/Main.java b/src/main/java/org/bench4q/monitor/Main.java index 7bfe7a99..ec6ca8be 100644 --- a/src/main/java/org/bench4q/monitor/Main.java +++ b/src/main/java/org/bench4q/monitor/Main.java @@ -76,7 +76,7 @@ public class Main { for (String elem : networkInterfaceInstances) { System.out.println(elem); System.out.println("bandwidth:" - + networkInterfaceMonitor.getCurrentBandwidth("Broadcom 802.11n ÍøÂçÊÊÅäÆ÷")); + + networkInterfaceMonitor.getCurrentBandwidth(elem)); } // String[] networkInterfaceCounter = networkInterfaceMonitor