Problem using WinExec to exec a file with the same name as the currently running program??

Problem using WinExec to exec a file with the same name as the currently running program??

Post by eva.. » Wed, 14 Jul 1993 03:25:59



Hi all,
    Here's my problem.  I want to exec a windows program located on the network
drive.  Here is what I currently do:
        -change the working drive to a NFS mounted drive

        -change the working directory to the directory where the executable to
         exec is located

        -call WinExec with the full path of the file to execute as the first
         parameter

    The problem:
        -My program must have the same name as the program to be executed(i.e.
         wpwin.exe)

        -the program to be executed is located at l:\wpwin\wpwin.exe I pass
         "l:\\wpwin\\wpwin.exe"(double \\'s stand for one \ in C) as the first
         parameter to WinExec.  I've checked this by popping up a message box
         before I call WinExec.

        -Instead of executing l:\wpwin\wpwin.exe I end up executing
         c:\scico\wpwin.exe which is My application not the real Word Perfect
         which is located at l:\wpwin\wpwin.exe

        (REALLY WIERD PART)
        -Wile my program(called wpwin.exe, but written by me) is running if I
         try to run l:\wpwin\wpwin(actual copy of word perfect for windows)
         using the Windows Program Manager run menu item the program that
         actually runs is the one that I wrote that is located at
         c:\scico\wpwin.exe.  So instead of getting an instance of word perfect
         for windows execed by my program I get another copy of my program
         execed by the copy of my program which was currently running?!?  
         If on the other hand the program written by me is not currently running
         I can successfully use the run menu option to run l:\wpwin\wpwin.exe.

  Quoted from "Programmers Reference Volume 2:Functions" on page 983
describing the function WinExec:
    "If the string does not contain a path, Windows searches the directories in
this order:
    1. The current directory."
(rest shortened)
    2. Windows directory

    3. windows system directory

    4. directory containing executable file for current task

    5. directories listed in the path environment.

    6. directories mapped to a network.

  Doesn't "l:\wpwin\wpwin.exe" contain a path??

  Is there some sort of undocumented rule that if something of the same name is
currently running then WinExec execs that file instead of the file whose name
and path are given?

Thanks for any help,
    Jeff Evans

P.S. using windows3.1, Microsoft C/C++ 7.0, and FTP Softwares pctcp version2.05.

_______________________________________________________________________________

what follows is the function I wrote to execute any programs I need to execute.

_______________________________________________________________________________

int exec_program()
{
char directory[GENERAL_STRING_LEN];      /* directory where program to be    */

                                         /* execed is located                */

struct _stat buffer;                     /* meaningless buffer for stat call */

int stat_result;                         /* result of _stat call             */

char tmp_msg[GENERAL_STRING_LEN];

char cmd_line[GENERAL_STRING_LEN];       /* command line of program to be    */

                                         /* executed                         */

WORD win_exec_return;                    /* return value from WinExec.  Also */

                                         /* application instance if execed   */

                                         /* with no errors                   */

TASKENTRY Task;                          /* generic task entry variable      */

int counter;                             /* counter                          */

struct APP_ARRAY initial_list[MAX_APPS]; /* List of applications running     */

                                         /* before we executed the program   */

char local_program_name[GENERAL_STRING_LEN];

if (_chdrive(scico_drive) < 0)
    {
    disconnect_from_server();

    net_phase=CHECKIN;

    generic_phase=GEN_PHASE1;

    sprintf(tmp_msg, "Unable to change to Drive %c.", scico_drive+64);
    MessageBox(window_handle, tmp_msg, "SCICO ERROR", MB_ICONSTOP |
               MB_SYSTEMMODAL);

    return FAILURE;
    }

sprintf(directory, "%c:\\", scico_drive+64);
if (_chdir(directory) < 0)
    {
    sprintf(tmp_msg, "Unable to change to directory %s.", directory);
    MessageBox(window_handle, tmp_msg, "SCICO ERROR", MB_ICONSTOP |
               MB_SYSTEMMODAL);
    }

sprintf(directory, "%c:\\%s", scico_drive+64, program_name);

if (_chdir(directory) < 0)
    {
    disconnect_from_server();

    net_phase=CHECKIN;

    generic_phase=GEN_PHASE1;

    sprintf(tmp_msg, "Unable to change to directory %s error %s.",
            directory, strerror(errno));
    MessageBox(window_handle, tmp_msg, "SCICO ERROR", MB_ICONSTOP |
               MB_SYSTEMMODAL);

    return FAILURE;
    }

/*******************************************/
/* exec the program with extension in the  */
/* order of 'com' then 'exe' then 'bat'    */
/* then 'pif'.                             */
/*******************************************/

/**********************************************/
/* Use a stat call to check for existance of  */
/* file with extension .pif .com .exe and     */
/* .bat.  Exec in order that windows does it. */
/**********************************************/
sprintf(local_program_name, "%c:\\%s\\%s.com", scico_drive+64, program_name,
        program_name);
stat_result = _stat(local_program_name, &buffer);

if (stat_result < 0)
    {
    sprintf(local_program_name, "%c:\\%s\\%s.exe", scico_drive+64, program_name,

            program_name);
    stat_result = _stat(local_program_name, &buffer);
    }

if (stat_result < 0)
    {
    sprintf(local_program_name, "%c:\\%s\\%s.bat", scico_drive+64, program_name,

            program_name);
    stat_result = _stat(local_program_name, &buffer);
    }

if (stat_result < 0)
    {
    sprintf(local_program_name, "%c:\\%s\\%s.pif", scico_drive+64, program_name,

            program_name);
    stat_result = _stat(local_program_name, &buffer);
    }

if (stat_result < 0)
    {
    disconnect_from_server();

    net_phase=CHECKIN;

    generic_phase=GEN_PHASE1;

    MessageBox(window_handle, "Program executable file not found.",
               "SCICO ERROR",  MB_ICONSTOP | MB_SYSTEMMODAL);

    return FAILURE;
    }

/*************************/
/* copy the program name */
/* into the commane line */
/* string                */
/*************************/
strcpy(cmd_line, local_program_name);

/*********************************/
/* tack on all arguments for the */
/* command line                  */
/*********************************/
for(counter=1; counter < __argc; counter++)
    {
    strcat(cmd_line, " ");

    strcat(cmd_line, __argv[counter]);
    }

old_mouse_pointer = SetCursor(hour_glass);

get_app_list(initial_list);

win_exec_return = WinExec(cmd_line, SW_SHOW);

if(win_exec_return < 32)
    {
    disconnect_from_server();

    net_phase=CHECKIN;

    generic_phase=GEN_PHASE1;

    sprintf(tmp_msg, "Unable to execute program %s.", cmd_line);

    MessageBox(window_handle, tmp_msg, "SCICO ERROR",  MB_ICONSTOP |
               MB_SYSTEMMODAL);

    return FAILURE;
    }
return SUCCESS;

}

--
----------------------------------------------------------------------------
Jeff Evans - eva...@crs.cl.msu.edu
----------------------------------------------------------------------------
 
 
 

Problem using WinExec to exec a file with the same name as the currently running program??

Post by Chris Marrio » Thu, 15 Jul 1993 02:46:03



>Hi all,
>    Here's my problem.  I want to exec a windows program located on the network
>drive.  Here is what I currently do:
>        -change the working drive to a NFS mounted drive

>        -change the working directory to the directory where the executable to
>         exec is located

>        -call WinExec with the full path of the file to execute as the first
>         parameter

>        -Instead of executing l:\wpwin\wpwin.exe I end up executing
>         c:\scico\wpwin.exe which is My application not the real Word Perfect
>         which is located at l:\wpwin\wpwin.exe

Congratulations, Jeff - you've found one of the wierder "features" of
Windows :-)

If you run a program whose FILE name is "x.exe", then any attempt to
execute another program called "x.exe", no matter WHAT directory or drive
it's in, will launch another instance of the first one!

Try this:

1.  Type the following command:

        copy \windows\calc.exe \tmp\clock.exe

2.  Run the Clock from "Accessories".

3.  Use "File/Run" to run "\tmp\clock.exe".  You'll get a second instance
    of the clock, NOT a calculator!

One might be tempted to call this a bug :-)

Chris
--
--------------------------------------------------------------------------

| Warrington, UK                           | BIX: cmarriott              |
| Save the whales.  Collect the whole set. | CompuServe: 100113,1140     |
--------------------------------------------------------------------------

 
 
 

Problem using WinExec to exec a file with the same name as the currently running program??

Post by Jeffrey Shneidm » Tue, 20 Jul 1993 23:34:41



: >Hi all,
: >    Here's my problem.  I want to exec a windows program located on the network
: >drive.  Here is what I currently do:
: >        -change the working drive to a NFS mounted drive
: >
: >        -change the working directory to the directory where the executable to
: >         exec is located
: >
: >        -call WinExec with the full path of the file to execute as the first
: >         parameter
: >
: >        -Instead of executing l:\wpwin\wpwin.exe I end up executing
: >         c:\scico\wpwin.exe which is My application not the real Word Perfect
: >         which is located at l:\wpwin\wpwin.exe
: >

: Congratulations, Jeff - you've found one of the wierder "features" of
: Windows :-)

: If you run a program whose FILE name is "x.exe", then any attempt to
: execute another program called "x.exe", no matter WHAT directory or drive
: it's in, will launch another instance of the first one!

: Try this:

: 1.  Type the following command:

:         copy \windows\calc.exe \tmp\clock.exe

: 2.  Run the Clock from "Accessories".

: 3.  Use "File/Run" to run "\tmp\clock.exe".  You'll get a second instance
:     of the clock, NOT a calculator!

: One might be tempted to call this a bug :-)

: Chris
: --
: --------------------------------------------------------------------------

: | Warrington, UK                           | BIX: cmarriott              |
: | Save the whales.  Collect the whole set. | CompuServe: 100113,1140     |
: --------------------------------------------------------------------------
:  
So I guess the solve would be to rename one of the programs (ie: clock)
to CLOCK2EXE or something.. :)