CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2009
    Posts
    15

    ShellExecute is not performing as expected

    Good Morning,

    I am a newbie to Visual C++ programming. I have been trying to extract files from a CAB by use of the ShellExecute command. I have made a batch file (CABTest.bat) that does the following:

    expand Test.cab -f:*.* C:\Temp\CABStuff

    I have converted the CABTest.bat into an exe file called CABTest.exe
    I am then using the ShellExecute(...) as follows and nothing happens:

    void CInstallerDlg::OnBnClickedShell()
    {
    // TODO: Add your control notification handler code here
    HINSTANCE hInst = ShellExecute(0, L"open", L"c:\\Temp\\CABTest.exe", NULL, 0, SW_SHOW);
    }

    If I double-click the CABTest.exe file from my browser, the file are extracted. The path is correct because I've tried this out with notepad.exe from the same directory.
    Does anyone have any suggestions as to what I done wrong?

    Thanks,
    Lee...

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: ShellExecute is not performing as expected

    Where is test.cab located ?

  3. #3
    Join Date
    Feb 2005
    Posts
    2,160

    Re: ShellExecute is not performing as expected

    What do you mean by:

    I have converted the CABTest.bat into an exe file called CABTest.exe
    Did you just rename it to .EXE?

  4. #4
    Join Date
    Feb 2009
    Posts
    15

    Re: ShellExecute is not performing as expected

    Hi,

    The CAB is located in:

    C:\Temp\CABStuff

    Thanks...

  5. #5
    Join Date
    Feb 2009
    Posts
    15

    Re: ShellExecute is not performing as expected

    No,

    I did not rename the file to *.exe. There is a Convert_batch_to_exe tool that converts a batch to an exe. It looks as if the ShellExecute requires an exe file.

    Thanks

  6. #6
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: ShellExecute is not performing as expected

    Your are probably running your own program from another directory. Use SetCurrentDirectory to change to c:\temp instead of adding the path to the shellexecute.

    It looks as if the ShellExecute requires an exe file.
    Nope. It can run everything. If you run a mp3 file it will automagically start your mp3 player based on the mp3 extension.

  7. #7
    Join Date
    Feb 2009
    Posts
    15

    Re: ShellExecute is not performing as expected

    Thanks Skizmo,

    Your reply prompted me to look at my path to the CAB file. I made an absolute path, converted the batch file to an exe file, ran it and it worked.

    I still could not get it to run a bat file though.

    Anyways, thanks a bunch!!!

    Lee...

  8. #8
    Join Date
    Feb 2009
    Posts
    42

    Re: ShellExecute is not performing as expected

    You don't need to set current path because shellexecute have one parameter which is counted as current dir for executed program

    ShellExecute(NULL, "open", "c:/mydir/myprg.exe", "", "c:/mydir/", SW_SHOWNORMAL);

    you can also call batch files with this function:

    ShellExecute(NULL, "open", "c:/mydir/extract.bat", "", "c:/mydir/", SW_SHOWNORMAL);

  9. #9
    Join Date
    Jul 2002
    Posts
    372

    Re: ShellExecute is not performing as expected

    you can use CreateProcess (comment the WaitForSingleObject if you dont need it):

    Code:
    bool execute_wait( char* file )
    {
    	STARTUPINFO si;
        PROCESS_INFORMATION pi;
    
        ZeroMemory( &si, sizeof(si) );
        si.cb = sizeof(si);
        ZeroMemory( &pi, sizeof(pi) );
    
        // Start the child process. 
        if( !CreateProcess( NULL,				// No module name (use command line). 
    						file,				// Command line. 
    						NULL,				// Process handle not inheritable. 
    						NULL,				// Thread handle not inheritable. 
    						FALSE,				// Set handle inheritance to FALSE. 
    						0,					// No creation flags. 
    						NULL,				// Use parent's environment block. 
    						NULL,				// Use parent's starting directory. 
    						&si,				// Pointer to STARTUPINFO structure.
    						&pi )				// Pointer to PROCESS_INFORMATION structure.
    							) 
        {
    		return false;
        }
    
        // Wait until child process exits.
        WaitForSingleObject( pi.hProcess, INFINITE );
    
        // Close process and thread handles. 
        CloseHandle( pi.hProcess );
        CloseHandle( pi.hThread );
    
    	return true;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured