示例#1
0
HANDLE WINAPI OpenW(const struct OpenInfo *Info)
{
    const int CUR_DIR_SIZE = 100000;
    char* filename = 0;

    switch( Info->OpenFrom )
    {
    case OPEN_COMMANDLINE:
        {
            OpenCommandLineInfo* cinfo = (OpenCommandLineInfo*)Info->Data;

            const wchar_t* cmdline = cinfo->CommandLine;
            if( !cmdline )
                return INVALID_HANDLE_VALUE;

            while( *cmdline && *cmdline <= ' ' )
                cmdline++;

            if( *cmdline )
                filename = w2a( cmdline );
            else
                return 0;
            break;
        }
    case OPEN_PLUGINSMENU:
        {
            FarGetPluginPanelItem pinfo;
            PluginPanelItem* pitem = (PluginPanelItem*)malloc(CUR_DIR_SIZE);
            ZeroMemory(&pinfo, sizeof(pinfo));
            ZeroMemory(pitem, CUR_DIR_SIZE);
            pinfo.StructSize = sizeof(pinfo);
            pinfo.Size = CUR_DIR_SIZE;
            pinfo.Item = pitem;

            if(InfoW.PanelControl( PANEL_ACTIVE, FCTL_GETCURRENTPANELITEM, 0, &pinfo ))
            {
                filename = w2a( pinfo.Item->FileName );
                free(pitem);
            }
            else
            {
                free(pitem);
                return 0;
            }
            break;
        }
    default:
        return 0;
    }
    
    if( !filename )
        return 0;

    wchar_t comspec[MAX_PATH * 2];
    if( !GetEnvironmentVariableW( L"COMSPEC", comspec, sizeofa( comspec ) ) )
        lstrcpyW( comspec, L"cmd.exe" );

    char pipename[100];
    wsprintf( pipename, "\\\\.\\pipe\\FarCall%ul", GetCurrentProcessId() );

    char* batchstr = (char*)malloc( 10000 );
    wsprintf( batchstr, batch, filename, ModuleName, pipename );

    // obtaining temp file name
    wchar_t tmp[MAX_PATH * 10];
    GetTempPathW( sizeofa( tmp ), tmp );
    GetTempFileNameW( tmp, L"", 0, tmp );
    DeleteFileW( tmp );

    lstrcatW( tmp, L".bat" );

    HANDLE file = CreateFileW( tmp, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, CREATE_ALWAYS, 0, 0 );
    if( !file || file == INVALID_HANDLE_VALUE )
    {
        DeleteFileW( tmp );
        free( filename );
        free( batchstr );
        return INVALID_HANDLE_VALUE;
    }

    DWORD written;
    WriteFile( file, batchstr, lstrlen( batchstr ), &written, 0 );
    CloseHandle( file );

    wchar_t cmd[MAX_PATH * 10] = L"\"";
    lstrcatW( lstrcatW( lstrcatW( lstrcatW( cmd, comspec ), L"\" /c \"" ), tmp ), L"\"");

    STARTUPINFOW sinfo;
    ZeroMemory( &sinfo, sizeof( sinfo ) );
    sinfo.cb = sizeof( sinfo );

    PROCESS_INFORMATION pinfo;

    Handle np( CreateNamedPipe( pipename, PIPE_ACCESS_DUPLEX, PIPE_WAIT,
                                PIPE_UNLIMITED_INSTANCES, 100, 100, 0, 0 ) );

    connected = false;

    DWORD id;
    Handle thread( CreateThread( 0, 0, ListenEnv, np, 0, &id ) );
    
    while( !connected )
        Sleep( 100 );
    
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &csbi );
    
#ifndef LIGHTGRAY
#define LIGHTGRAY 7
#endif

    wchar_t Blank[1024];
    FSFW.sprintf(Blank,L"%*s",csbi.dwSize.X,L"");
    FarColor fc = {FCF_NONE, LIGHTGRAY, 0, 0};
    for (int Y=0;Y<csbi.dwSize.Y;Y++)
        InfoW.Text(0,Y,&fc,Blank);
    InfoW.Text(0,0,0,NULL);
    
    COORD C;
    C.X=0;
    C.Y=csbi.dwCursorPosition.Y;
    SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), C );

    wchar_t* curr_dir = (wchar_t*)malloc(CUR_DIR_SIZE);
    
    FSFW.GetCurrentDirectory(CUR_DIR_SIZE, curr_dir);

    if( np && CreateProcessW( NULL, cmd, 0, 0, TRUE, 0, 0, curr_dir[0] ? curr_dir : 0, &sinfo, &pinfo ) )
    {

        HANDLE ar[] = {pinfo.hProcess, np};

        WaitForMultipleObjects( 2, ar, TRUE, INFINITE );
        CloseHandle(pinfo.hProcess);
        CloseHandle(pinfo.hThread);
        SMALL_RECT src;
        COORD dest;
        CHAR_INFO fill;
        src.Left=0;
        src.Top=2;
        src.Right=csbi.dwSize.X;
        src.Bottom=csbi.dwSize.Y;
        dest.X=dest.Y=0;
        fill.Char.AsciiChar=' ';
        fill.Attributes=7;
        ScrollConsoleScreenBuffer( GetStdHandle( STD_OUTPUT_HANDLE ), &src, NULL, dest, &fill);

        InfoW.AdvControl(0, ACTL_REDRAWALL, 0, 0);
    }
    else
        Handle onp( CreateFile( pipename, GENERIC_WRITE,
            FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0 ) );

    
    free( filename );
    free( batchstr );
    free( curr_dir );
    DeleteFileW( tmp );
    
    return 0;
}
示例#2
0
HANDLE	WINAPI	EXP_NAME(OpenPlugin)(int OpenFrom, INT_PTR Item) {
//	Options.Read();

	AutoUTF	cline;
	if (OpenFrom == OPEN_PLUGINSMENU) {
		FarPnl	pi(PANEL_ACTIVE);
		if (pi.IsOK()) {
			AutoUTF	buf(MAX_PATH_LEN, L'\0');
			fsf.GetCurrentDirectory(buf.capacity(), (PWSTR)buf.c_str());
			if (!buf.empty())
				::PathAddBackslash((PWSTR)buf.c_str());

			PluginPanelItem &PPI = pi[pi.CurrentItem()];
			buf += PPI.FindData.lpwszFileName;
			cline = buf;
		}
	} else if (OpenFrom == OPEN_COMMANDLINE) {
		cline = (PCWSTR)Item;
	}
	FarList	users;
	if (InitUsers(users)) {
		enum {
			HEIGHT = 14,
			WIDTH = 48,
		};
		InitDialogItemF	Items[] = {
			{DI_DOUBLEBOX, 3, 1, WIDTH - 4, HEIGHT - 2, 0, (PCWSTR)DlgTitle},
			{DI_TEXT,      5, 2, 0,  0,  0, (PCWSTR)MUsername},
			{DI_COMBOBOX,  5, 3, 42, 0,  DIF_SELECTONENTRY, L""},
			{DI_TEXT,      5, 4, 0,  0,  0, (PCWSTR)MPasword},
			{DI_PSWEDIT,   5, 5, 42, 0,  0, L""},
			{DI_CHECKBOX , 5, 6, 42, 0,  0, (PCWSTR)MRestricted},
			{DI_TEXT,      0, 7, 0,  0,  DIF_SEPARATOR, L""},
			{DI_TEXT,      5, 8, 0,  0,  0, (PCWSTR)MCommandLine},
			{DI_EDIT,      5, 9, 42, 0,  DIF_HISTORY, cline.c_str()},
			{DI_TEXT,      0,  HEIGHT - 4, 0,  0,  DIF_SEPARATOR,   L""},
			{DI_BUTTON,    0,  HEIGHT - 3, 0,  0,  DIF_CENTERGROUP, (PCWSTR)txtBtnOk},
			{DI_BUTTON,    0,  HEIGHT - 3, 0,  0,  DIF_CENTERGROUP, (PCWSTR)txtBtnCancel},
		};
		size_t	size = sizeofa(Items);
		FarDialogItem FarItems[size];
		InitDialogItemsF(Items, FarItems, size);
		FarItems[size - 2].DefaultButton = 1;
		FarItems[2].ListItems = &users;
		FarItems[8].History = L"runas.comline";

		FarDlg hDlg;
		if (hDlg.Init(psi.ModuleNumber, -1, -1, WIDTH, HEIGHT, L"Contents", FarItems, size)) {
			HRESULT	err = NO_ERROR;
			while (true) {
				int		ret = hDlg.Run();
				if (ret > 0 && Items[ret].Data == (PCWSTR)txtBtnOk) {
					AutoUTF	cmd(hDlg.Str(8));
					if (hDlg.Check(5)) {
						err = ExecRestricted(cmd.c_str());
					} else {
						AutoUTF	user(hDlg.Str(2));
						AutoUTF	pass(hDlg.Str(4));
						err = ExecAsUser(cmd.c_str(), user.c_str(), pass.c_str());
					}
					if (err == NO_ERROR) {
						break;
					} else {
						PCWSTR Msg[] = {GetMsg(MError), cmd.c_str(), L"", GetMsg(txtBtnOk), };
						::SetLastError(err);
						psi.Message(psi.ModuleNumber, FMSG_WARNING | FMSG_ERRORTYPE,
									L"Contents", Msg, sizeofa(Msg), 1);
					}
				} else {
					break;
				}
			}
		}
		FreeUsers(users);
	}
	return	INVALID_HANDLE_VALUE;
}