1

hope ur having good day;

so i am using GetFullPathName to get the path of powershell.exe in C programming language :

i used this code here : parsing a path using a functoin called getfullpathname() in C? i used code here too: I have errors using GetFullPathName

whatever im using, im getting the following output:

Full path: C

if i debug it however i can see the rest of the path ...

last code i tried is:

    TCHAR* fileExt;
    TCHAR szDir[256];

    GetFullPathName("powershell.exe", 256, szDir, &fileExt);

    printf("Full path: %s\n", szDir);

i tried this for the same result:

   char filename[] = "powershell.exe";
   char fullFilename[MAX_PATH];
   GetFullPathName(filename, MAX_PATH, fullFilename, NULL);
   printf("Full path: %s\n", fullFilename);

i tried multiple code from here and there too, idk why but the output is always the same, and as i mentioned if i debug it i can see the full path, but i cant put it into the console, im using c and not cpp file , and using visual studio 2019.

CODER876
  • 11
  • 1

1 Answers1

1

You are doing mix and match between ansi and unicode.

Instead, do the following:

TCHAR* fileExt;
TCHAR szDir[256];

GetFullPathName(TEXT("powershell.exe"), 256, szDir, &fileExt);

_tprintf(TEXT("Full path: %s\n"), szDir);

The TEXT macro expands to either an ansi or wide character string literal, depending on whether or not you compile with UNICODE enabled.

_tprintf expands to either printf or wprintf depending on wether or not you compile with UNICODE enabled. For printf, %s specifies an ansi string, for wprintf, %s specifies a wide character string.

GetFullPathName expands to either GetFullPathNameA or GetFullPathNameW, again depending on wether or not you compile with UNICODE enabled. The former expects (and returns) ansi strings, the latter wide character strings.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
lulle2007200
  • 888
  • 9
  • 20
  • first thanks for ur interest u and @andreas ... i tried doing it the same way u told me, but it outputted the following link error: ```LNK2001 unresolved external symbol _tprintf``` – CODER876 Jul 27 '21 at 06:38
  • the later code will create a powershell processes using CreateProcessA, so if that can ease the way the path file must be saved or the type of it ... – CODER876 Jul 27 '21 at 06:49
  • Well, then just use *GetFullPathNameA* and don't bother with *TCHAR* and both, ansi and unicode compatibility. – lulle2007200 Jul 27 '21 at 12:57