Link to home
Start Free TrialLog in
Avatar of PMH4514
PMH4514

asked on

On getting path from __targv[0]; when launched app from batch file

I have existing code that does this when the application starts:

      m_strAppLoc = __targv[0];
      m_strAppLoc = m_strAppLoc.Left ( m_strAppLoc.ReverseFind(_T('\\') ) );
      m_strAppDir = m_strAppLoc.Right( m_strAppLoc.GetLength() - m_strAppLoc.ReverseFind(_T('\\') ) );

and then I write to the registry the path to the application like this:

      _tcscpy( lpszData, m_strAppLoc);
      dwDataSize = _tcslen(lpszData) * sizeof(TCHAR);
      if (ERROR_SUCCESS != (lReturn = RegSetValueEx( hKey, _T("Location"), 0, REG_SZ, (BYTE * const)lpszData, dwDataSize)) )

this is all working just fine.

My problem is that, due to a customization we've done for a customer, we now have two .BAT files which we want to use to load the app rather than allowing them to click the EXE directly, the BAT files simply put in place a particular version of the INI.

Anyway, I'm finding that when launched from a BAT file,  my m_strAppLoc = __targv[0];  is empty, and as such, the value for path is written as empty to the registry and then the app can't load properly the next time.

Is there a way to modify my BAT file so that the path will be parsed properly by the existing code?  We need to do this w/o making a new software build.

Thanks!
-Paul
Avatar of Axter
Axter
Flag of United States of America image

Hi PMH4514,
Try the following code instead:
CString GetExecutablePath()
{
    CString strFileName;
    GetModuleFileName(NULL, strFileName.GetBuffer(_MAX_PATH), _MAX_PATH);
    strFileName.ReleaseBuffer();
    int Pos = strFileName.ReverseFind('\\');
    if (Pos != -1)
    {
        strFileName = strFileName.Left(Pos);
    }
    return strFileName;
}



David Maisonave :-)
Cheers!
PMH4514,
> >Is there a way to modify my BAT file so that the path will be parsed
> >properly by the existing code?

It's better to use the GetModuleFileName API function to determine program path, then to use argv[0] method.

The above wrapper function I posted, uses the GetModuleFileName API function.

David Maisonave :-}
PMH4514,
> >Is there a way to modify my BAT file so that the path will be parsed
> >properly by the existing code?

You could just do a CD in the bat file.
cd c:\MyAppPath
Myapp.exe

However, I recommend fixing the app, instead of trying to do a work around on the bat file.

David Maisonave :-}
FYI:
You won't get the same problem with that BAT file on all Windows OS.
If you run it in Window 95/98, you'll find that the program will still work properly.

And doing a BAT fix, may mean that you're program will work one way in one operating system, and another way in another operatiing system.

David Maisonave :-}
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PMH4514
PMH4514

ASKER

thanks everybody.. I can't do any code changes in this case. it's legacy software, and a one-off "customiztaion" that will let the user startup with one of two INI files..  the computer itself is integrated with our hardware, they won't be changing OS or anything like that. I agree with everything everybody said though generally speaking.

What I did find was that if instead of simply calling the executable from my batch file, if I did this:

COMMAND e:\pathtoapp\app.exe e:\pathtoapp\app.exe  

that is, executing the app with its full path as an argument, then everything worked out ok.