c++ - GetShortPathNameW returns the file extension. I want it to ignore the .exe -


i have getshortpathnamew in c++ msvc 2008 free edition. want use can search or return filename without .exe extension. chance me that? code below have:

wchar buffer[max_path];     lpcwstr datafilepath = wargv[datafile_argv];          // hack windows in case there unicode chars in path.     // normal argv[] array has ????? instead of unicode chars     // , fails, instead manually short file name,     // using ansi chars.     if (wcschr(datafilepath, '\\') == null)     {         getcurrentdirectoryw(max_path, buffer);         wcscat(buffer, l"\\");         wcscat(buffer, datafilepath);         datafilepath = &buffer[0];      }      if (getshortpathnamew(datafilepath, directorypathbuffer, max_path) == 0)     {         platform->displayalert("unable determine startup path: getshortpathnamew failed.");         game_file_name = null;         return;     } 

i want use can search or return filename without .exe extension.

sure, use pathremoveextension function. pass buffer of length max_path containing string, , strips off extension (if 1 present) in-place.

std::wstring path(max_path, l'\0'); if (!getcurrentdirectoryw(max_path, &path[0])) {     throw std::runtime_error("the getcurrentdirectory function failed"); } pathremoveextension(&path[0]); 

and recommend using either pathcombine or pathappend concatenate path elements, rather c-style string manipulation functions. these functions designed work paths, , automatically add backslash (or whatever other path separator character) you.


don't understand code you've posted in question, starting comment:

// hack windows in case there unicode chars in path. // normal argv[] array has ????? instead of unicode chars // , fails, instead manually short file name, // using ansi chars. 

perhaps simpler way of explaining standard argv[] array, passed parameter entry point of c program, required array of type char. on windows, means not support unicode characters. support unicode requires use wchar_t (or 1 of equivalent macros).

you can work around problem in windows using wmain entry point, instead of main. function, argv[] array of wide characters (wchar_t). assume you're cross-compiling code multiple operating systems, in case you'll need use preprocessor magic ensure right entry point when targeting windows.

if absolutely can't that, call getcommandline , commandlinetoargv functions retrieve command line arguments, , convert them argv[] style array contains wide characters.

as alternative this, alf suggested , call getmodulefilename function, passing null first argument, retrieve path of executable file.

either way approach it, important realize converting short path really ugly hack , 1 subject breaking if underlying file system has short names disabled. since code using unicode apis everywhere, there should no problem deal unicode characters in path, long obtain valid path string in first place.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -