LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

get executable filename

I need to programatically get the release executable filename at run-time. I need this filename to get version number information to display in the help menu of my application. Is there anyway for an executable file to know its own filename? Or, is there a way to programtically get the version info that is set in "Build | Target Settings... | Version Info" of the project window? Thanks!
0 Kudos
Message 1 of 6
(6,520 Views)
The executable's file name is the first argument of the main function (argv[0]) and can be retrieved without any problem. Other command-line arguments appear as subsequent indices (the first argument as argv[1] and so on).

To retrieve version information I assume is necessary to use some windows API, but I don't know.

Hope this helps
Roberto


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 6
(6,520 Views)
You should call GetFileVersionInfoSize and GetFileVersionInfo (Windows
SKD).
For example:

DWORD size;
char* ver;
size = GetFileVersionInfoSize (argv[0], 0);
ver = calloc( size + 1 , sizeof(char) );
GetFileVersionInfo ( argv[0], 0, size, ver);

Then use VerQueryValue to arrange information. See Windows SDK help
for more details (you can find an example there).
Regards.
Message 3 of 6
(6,520 Views)

I was recently trying to do this, and the example in the Windows SDK is less than clear - so here's what I ended up with that gets the version information from the FileInfo Structure. 

 

DWORD size;
LPTSTR *version;
UINT versionLen = 0;
BOOL status = FALSE;
UINT ver1 = 0;
UINT ver2 = 0;
UINT ver3 = 0;
UINT ver4 = 0;
 

size = GetFileVersionInfoSize (argv[0], 0);
if (!size)
{
    error.flag = TRUE;
    error.code = GetLastError ();
    goto Error;
}
    
version = calloc (size + 1, sizeof (char));
    
status = GetFileVersionInfo (argv[0], 0, size, version);
    
status = VerQueryValue (version, "\\", (LPVOID*)&fileInfo, &versionLen);
if (status)
{
   ver1 = HIWORD(fileInfo->dwFileVersionMS);
   ver2 = LOWORD(fileInfo->dwFileVersionMS);
   ver3 = HIWORD(fileInfo->dwFileVersionLS);
   ver4 = LOWORD(fileInfo->dwFileVersionLS);

}

Hope this help those who don't use the SDK often...

 

-Jack

0 Kudos
Message 4 of 6
(6,157 Views)

beware of the current path when using argv[0]. sometimes the filename specified in argv[0] does not include the full path (executable launched from console, ...). in this case, GetFileVersionInfo will search the file on the current path, which may no more be the path where the file resides (expecially if you used the CVI open file dialog).

 

to get the fully qualified path to the current executable, you should use GetModuleFileName( NULL ).

Message 5 of 6
(6,146 Views)

Thanks for the clarification - Just for completness I also forgot to add the definition of the variable "fileInfo"; it's:

 

VS_FIXEDFILEINFO *fileInfo;

 

Sorry about that.

 

-Jack

0 Kudos
Message 6 of 6
(6,132 Views)