0

My Java code is using native method (win API) for opening a file where that native method is implemented in C++. I dont have any idea about win API. When I try to compile that C++ project, I am getting the above mentioned error.

The code responsible for error as follows:

std::wstring getUnicodeString(JNIEnv *env, jstring str)
{
    jboolean isCopy;
    jsize len = env->GetStringLength(str);
    LPTSTR unicodeString = (LPTSTR) malloc (2 * len + 2);
    memset (unicodeString, 0, 2 * len + 2);
    LPCWSTR tempStr = env->GetStringChars(str, &isCopy); // <--- this line
    memcpy(unicodeString, tempStr, 2 * len );
    std::wstring result = unicodeString;
    env->ReleaseStringChars(str, tempStr);
    delete unicodeString;
    return result;
}

I am getting error in the highlighted line. Initially I thought this must be a conversion error so I did typecasting. After typecasting, the error in that particular line got resolved but I got so many errors based on the dependency.

I think there is no problem with the code because this code got compiled several times previously. I think this might be some unicode settings issue in Visual Studio. Please help

zenzelezz
  • 813
  • 2
  • 10
  • 26
arun kumar
  • 59
  • 1
  • 1
  • 3
  • if you use tempStr only once for memcpy, why can't you declare it as const jchar * or auto. Could you add the original error message and error messages that occurs after resolving the first one? – VinSmile Jul 08 '15 at 06:53
  • Well, I didn't notice the second usage of tempStr at first glance, but const jchar * is still ok for the ReleaseStringChars – VinSmile Jul 08 '15 at 06:59
  • Btw, [this answer](http://stackoverflow.com/a/9100041/1322972) does exactly what you seem to be trying to do (two different ways). – WhozCraig Jul 08 '15 at 07:09
  • Is this your exact code? You are allocating memory with `malloc()`, but releasing with `delete` instead of `free()`... I believe you should choose either the `malloc()/free()` or `new/delete` pair, not mix them up. – zenzelezz Jul 08 '15 at 07:31
  • i think there is no problem with the code...it has been build so many times before....i think there is some configuration i am missing...any settings i have to do in visual studio project??pls help – arun kumar Jul 13 '15 at 04:01

1 Answers1

0

Project Properties>C/C++/Language/Treat WChar_t as Builtin type" Set to No.

Lefteris E
  • 2,806
  • 1
  • 24
  • 23