GetThisPath.h
/// dest is expected to be MAX_PATH in length.
/// returns dest
/// TCHAR dest[MAX_PATH];
/// GetThisPath(dest, MAX_PATH);
TCHAR* GetThisPath(TCHAR* dest, size_t destSize);
GetThisPath.cpp
#include <Shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
TCHAR* GetThisPath(TCHAR* dest, size_t destSize)
{
if (!dest) return NULL;
if (MAX_PATH > destSize) return NULL;
DWORD length = GetModuleFileName(NULL, dest, destSize);
PathRemoveFileSpec(dest);
return dest;
}
mainProgram.cpp
TCHAR dest[MAX_PATH];
GetThisPath(dest, MAX_PATH);
Actualización: PathRemoveFileSpec
está en desuso en Windows 8. Sin embargo, el reemplazo, PathCchRemoveFileSpec
, está disponible en Windows 8 + solamente. (Gracias a @askalee por el comentario)
Creo que por debajo de este código podría funcionar, pero lo estoy dejando el código anterior hasta allí hasta que se vetado el código de abajo. No tengo un compilador configurado para probar esto en este momento. Si usted tiene la oportunidad de probar este código, por favor enviar un comentario diciendo si este código a continuación trabajó y en qué sistema operativo que la prueba. ¡Gracias!
GetThisPath.h
/// dest is expected to be MAX_PATH in length.
/// returns dest
/// TCHAR dest[MAX_PATH];
/// GetThisPath(dest, MAX_PATH);
TCHAR* GetThisPath(TCHAR* dest, size_t destSize);
GetThisPath.cpp
#include <Shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
TCHAR* GetThisPath(TCHAR* dest, size_t destSize)
{
if (!dest) return NULL;
DWORD length = GetModuleFileName(NULL, dest, destSize);
#if (NTDDI_VERSION >= NTDDI_WIN8)
PathCchRemoveFileSpec(dest, destSize);
#else
if (MAX_PATH > destSize) return NULL;
PathRemoveFileSpec(dest);
#endif
return dest;
}
mainProgram.cpp
TCHAR dest[MAX_PATH];
GetThisPath(dest, MAX_PATH);
Algo más sofisticado que usar argv [0] que es un parámetro para int main (int argc, char * argv []) y analizarlo? – Minok