Thanks everyone.
Provided solution worked perfectly. I modified it a little bit like this:
1 2 3 4
|
const char * _filename = "E:\\test\\folder_µ_1\\Backup-0008.txt";
const TCHAR * file = _T(_filename);
WIN32_FILE_ATTRIBUTE_DATA fileAttrs;
BOOL result = GetFileAttributesEx(file, GetFileExInfoStandard, &fileAttrs);
|
I tested it in a simple C++ application and it worked perfectly. It worked for both ascii filepath and Unicode filepath. I also tried to get file size from fileAttrs structure and it was correct.
Now let me explain the situation in more details. I created a function using the code above in a C++ dll like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
JNIEXPORT jlong JNICALL Java_FileAttrs_getAttrib(JNIEnv *env, jobject obj, jstring filename)
{
const char * _filename = env->GetStringUTFChars(filename, 0);
const TCHAR * file = _T(_filename);
printf("\n>>>_filename: "); printf(_filename);
printf("\n>>> file: "); printf(file);
WIN32_FILE_ATTRIBUTE_DATA fileAttrs;
BOOL result = GetFileAttributesEx(file, GetFileExInfoStandard, &fileAttrs);
if (!result) {
return -1;
}
return fileAttrs.dwFileAttributes;
}
|
This dll function is called from a Java application using jni (for Java, I’m using Eclipse). This java app sends filepath as a parameter to this dll function. Java has no problem with ascii and Unicode file paths. Now if the filepath has only ascii characters in it, C++ function works correctly and returns file attributes but if the filepath contains Unicode characters C++ function fails and return -1. I hard-coded the value of _filename variable for testing purpose and then called the function from Java using Eclipse and it worked perfectly.
1 2
|
//const char * _filename = env->GetStringUTFChars(filename, 0);
const char * _filename = "E:\\test\\folder_µ_1\\Backup-0008.txt";
|
So I suppose that
env->GetStringUTFChars(filename, 0)
function is creating problem.
The printf() statements in the code produced following output (captured from Eclipse output window):
When using env->GetStringUTFChars(filename, 0):
const char * _filename = env->GetStringUTFChars(filename, 0);
>>>_filename: E:\test\folder_µ_1\Backup-0008.txt
>>> file: E:\test\folder_µ_1\Backup-0008.txt
When using hard-coded value:
const char * _filename = "E:\\test\\folder_µ_1\\Backup-0008.txt";
>>>_filename: E:\test\folder_µ_1\Backup-0008.txt
>>> file: E:\test\folder_µ_1\Backup-0008.txt
You can see that printf() displayed correct filepath when hard-coded.
Maybe I need to convert utf chars to Unicode/ascii chars somehow.
Sorry if it is not related to the forum anymore.