我有一个使用ShellExecute打开用户传入的文件的方法。编写它是为了接受带有unicode字符的文件名。它适用于txt和pdf文档,但是当我使用包含unicode字符的图像文件进行测试时,我得到了FILE NOT FOUND错误。我正在使用以下文件名测试图片:ams’.jpg
和ams’.png
(’
ALT + 0146 )。
void sys_ShellExecute( PA_PluginParameters params )
{
LONG_PTR returnValue = 0;
LONG_PTR len = 0;
char returnText[255]; // MWD & Mark De Wever #12225
INT_PTR howToShow;
//char *pChar;
//char *operation = NULL;
//char *file = NULL;
//char *parameters = NULL;
//char *directory = NULL;
PA_Unistring *UnistringFile;
PA_Unistring *UnistringOperation;
PA_Unistring *UnistringParameters;
PA_Unistring *UnistringDirectory;
PA_Unichar *file;
PA_Unichar *operation;
PA_Unichar *parameters;
PA_Unichar *directory;
PA_Unichar *pChar;
// Get the function parameters.
//operation = getTextParameter(params, 1);
//file = getTextParameter(params, 2);
//parameters = getTextParameter(params, 3);
//directory = getTextParameter(params, 4);
UnistringOperation = PA_GetStringParameter( params, 1);
UnistringFile = PA_GetStringParameter( params, 2);
UnistringParameters = PA_GetStringParameter( params, 3);
UnistringDirectory = PA_GetStringParameter( params, 4);
howToShow = PA_GetLongParameter( params, 5 );
operation = PA_GetUnistring(UnistringOperation); // AMS /28/14
file = PA_GetUnistring(UnistringFile); // AMS 2/28/14
len = (PA_GetUnistringLength(UnistringFile) + 1); // AMS 2/28/14
parameters = PA_GetUnistring(UnistringParameters); // AMS 2/28/14
directory = PA_GetUnistring(UnistringDirectory); // AMS 2/28/14
//if ((strcmp(_strlwr(operation), "open") != 0) &&
// (strcmp(_strlwr(operation), "explore") != 0) &&
// (strcmp(_strlwr(operation), "print") != 0) &&
// (file == NULL || strlen(file) == 0)) {
// //strcpy(returnText, "Invalid Operation");
// strncpy(returnText, message->InvalidOperation, 255); // Mark De Wever #12225 replaced the line above
//}
if((lstrcmpiW(operation, L"open") != 0) &&
(lstrcmpiW(operation, L"explore") != 0) &&
(lstrcmpiW(operation, L"print") != 0) &&
(file == NULL || len == 0)) {
strncpy(returnText, message->InvalidOperation, 255);
}
else if (howToShow > 11) {
//strcpy(returnText, "Invalid HowToShow Constant");
strncpy(returnText, message->InvalidShowConstant, 255); // Mark De Wever #12225 replaced the line above
}
else
{
pChar = file; // added 10/28/02 shellExecute wants backslashes
do {
if (*pChar == '/') {
*pChar = '\\';
}
} while (*pChar++ != '\0') ;
if (directory != NULL) {
pChar = directory;
do {
if (*pChar == '/') {
*pChar = '\\';
}
} while (*pChar++ != '\0');
}
returnValue = (LONG_PTR) ShellExecute(NULL, operation, file, parameters, directory, howToShow);
strcpy(returnText, "");
if (returnValue <= 32) { // error occurred
switch (returnValue)
{
case ERROR_FILE_NOT_FOUND :
//strcpy(returnText, "File Not Found");
strncpy(returnText, message->FileNotFound, 255); // Mark De Wever #12225 replaced line above
break;
case ERROR_PATH_NOT_FOUND :
//strcpy(returnText, "Path Not Found");
strncpy(returnText, message->PathNotFound, 255); // Mark De Wever #12225 Replaced line above
break;
case ERROR_BAD_FORMAT :
//strcpy(returnText, ".EXE File is Invalid");
strncpy(returnText, message->BadFormat, 255); // Mark De Wever #12225 Replaced line above
break;
case SE_ERR_ACCESSDENIED :
//strcpy(returnText, "OS Denied Access to File");
strncpy(returnText, message->AccessDenied, 255); // Mark De Wever #12225 Replaced line above
break;
case SE_ERR_ASSOCINCOMPLETE :
//strcpy(returnText, "File Name Association is Incomplete or Invalid");
strncpy(returnText, message->AssocIncomplete, 255); // Mark De Wever #12225 Replaced line above
break;
case SE_ERR_DDEBUSY :
case SE_ERR_DDEFAIL :
//strcpy(returnText, "DDE Transaction Could Not be Completed");
strncpy(returnText, message->DDEFail, 255); // Mark De Wever #12225 Replaced line above
break;
case SE_ERR_DDETIMEOUT :
//strcpy(returnText, "DDE Request Timed Out");
strncpy(returnText, message->DDETimeOut, 255); // Mark De Wever #12225 Replaced line above
break;
case SE_ERR_DLLNOTFOUND :
//strcpy(returnText, "DLL Libray Not Found");
strncpy(returnText, message->DLLNotFound, 255); // Mark De Wever #12225 Replaced line above
break;
case SE_ERR_NOASSOC :
//strcpy(returnText, "No Application Associated with File Extenstion");
strncpy(returnText, message->NoAssoc, 255); // Mark De Wever #12225 Replaced line above
break;
case SE_ERR_OOM :
//strcpy(returnText, "Insufficient Memory");
strncpy(returnText, message->OOM, 255); // Mark De Wever #12225 Replaced line above
break;
case SE_ERR_SHARE :
//strcpy(returnText, "Sharing Violation Occurred");
strncpy(returnText, message->ShareViolation, 255); // Mark De Wever #12225 Replaced line above
break;
default:
//strcpy(returnText, "Unknown error occurred");
strncpy(returnText, message->Default, 255); // Mark De Wever #12225 Replaced line above
break;
}
}
}
//freeTextParameter(operation);
//freeTextParameter(file);
//freeTextParameter(parameters);
//freeTextParameter(directory);
PA_ReturnText( params, returnText, strlen(returnText));
}
答案 0 :(得分:1)
我做了一个快速测试,以下对我来说非常好:
LONG_PTR returnValue = (LONG_PTR) ShellExecuteW(NULL, L"open", L"C:\\ams’.jpg", NULL, NULL, SW_SHOW);
因此,问题必须与传递给ShellExecute()
的参数值有关,而不是与包含Unicode字符的文件名相关。