-2

I have a method that uses ShellExecute to open files passed in by the user. It is written to accept file names with unicode characters. It works with txt and pdf documents, but when I test with an image file that contains an unicode character, I get the FILE NOT FOUND error. I am testing images with the following filenames: ams’.jpg and ams’.png ( is 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));
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Amber
  • 17
  • 1
  • Could you give us an SSCCE. Nobody wants to wade through this wall of code. – David Heffernan Apr 04 '14 at 18:24
  • One obvious first step is to bisect the problem. Is the problem in the code that parses the `params` and tries to convert them into `operation, `file`, `parameters,` `directory`, and `howToShow`? Or is the problem in the `ShellExecute` part? if it's in the `ShellExecute` part, then you can delete all the parsing steps from your SSSCE and replace it with hard-coded values. – Raymond Chen Apr 04 '14 at 23:18

1 Answers1

1

I did a quick test and the following works perfectly fine for me:

LONG_PTR returnValue = (LONG_PTR) ShellExecuteW(NULL, L"open", L"C:\\ams’.jpg", NULL, NULL, SW_SHOW);

So the problem has to be related to the parameter values you are passing to ShellExecute(), not with the filename containing Unicode characters.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770