Skip to content

Instantly share code, notes, and snippets.

@feuvan
Created February 16, 2011 01:54
Show Gist options
  • Select an option

  • Save feuvan/828705 to your computer and use it in GitHub Desktop.

Select an option

Save feuvan/828705 to your computer and use it in GitHub Desktop.

Revisions

  1. feuvan created this gist Feb 16, 2011.
    69 changes: 69 additions & 0 deletions LocalRun.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    /**
    * Local(e)Run: Run application in specified locale (chs as hard-coded).
    *
    * Originial purpose: run fterm.exe in non-Chinese(PRC) locale.
    *
    * Author: [email protected]
    */

    #include <stdio.h>
    #include <tchar.h>
    #include <locale.h>
    #include <process.h>
    #include <windows.h>
    #include <mbctype.h>
    #include <Shlwapi.h>
    #pragma comment(lib, "Shlwapi.lib")

    int _tmain(int argc, _TCHAR* argv[])
    {

    // Please refer to http://msdn.microsoft.com/en-us/library/39cwe7zf.aspx
    wprintf_s(
    L"The process locale is now set to %s.\n",
    _wsetlocale(LC_ALL, L"chs"));

    int ret = _setmbcp(_MB_CP_LOCALE);

    switch (ret)
    {
    case -1:
    MessageBox(NULL, L"Invalid code page value is supplied for codepage.", L"Error", MB_ICONERROR);
    return 1;
    case 0:
    break;
    default:
    MessageBox(NULL, L"Error while setting locale for MBCS app.", L"Error", MB_ICONERROR);
    return 1;
    }

    PROCESS_INFORMATION processInformation;
    STARTUPINFO startupInfo;
    memset(&processInformation, 0, sizeof(processInformation));
    memset(&startupInfo, 0, sizeof(startupInfo));
    startupInfo.cb = sizeof(startupInfo);

    if (argc != 2)
    {
    MessageBox(NULL, L"No application path specified.", L"File path parameter required", MB_ICONERROR);
    return 2;
    }

    if (!PathFileExists(argv[1]))
    {
    MessageBox(NULL, argv[1], L"File not found", MB_ICONERROR);
    return 3;
    }

    BOOL pros = CreateProcess(NULL, argv[1], NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &startupInfo, &processInformation);
    if (!pros)
    {
    MessageBox(NULL, argv[1], L"Failed to create process with required locale", MB_ICONERROR);
    return 4;
    }

    CloseHandle( processInformation.hProcess );
    CloseHandle( processInformation.hThread );

    return 0;
    }