[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 945
  • Last Modified:

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
0
PMH4514
Asked:
PMH4514
  • 5
1 Solution
 
AxterCommented:
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!
0
 
AxterCommented:
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 :-}
0
 
AxterCommented:
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 :-}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AxterCommented:
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 :-}
0
 
AxterCommented:
I just notice I posted the wrong example.
Since you want the full executable path and executable name, the following would be a more approriate and simplified wrapper function:

CString GetExecutableName()
{
    CString strFileName;
    GetModuleFileName(NULL, strFileName.GetBuffer(_MAX_PATH), _MAX_PATH);
    strFileName.ReleaseBuffer();
    return strFileName;
}
0
 
PMH4514Author Commented:
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.



0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now