Advertisement
RusNuker

Get a caller EXE name (WinAPI C)

Feb 20th, 2023
916
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4.  
  5. // Alloc bytes for chars
  6. #define challoc(x) ((char *)malloc((sizeof(char))*(x)))
  7.  
  8.  
  9. // Get a local EXE name.
  10. // Yes, you cannot do it in one WinAPI function.
  11. // Returns a naked string containing an EXE name (if successful), OR
  12. // an empty string (if GetModuleFileNameA failed), OR
  13. // an empty string (if runtime-linking failed), OR
  14. // a string with a full path (if PathFindFileNameA failed).
  15. char * GetExeName (void) {
  16.     char *result = challoc(256);
  17.     char temp[32769]; int temp1;
  18.  
  19.     HMODULE hShlwapi = LoadLibraryA("shlwapi.dll");
  20.     FARPROC PathFindFileNameA = GetProcAddress(hShlwapi, "PathFindFileNameA");
  21.     if (PathFindFileNameA == 0) return '\0';
  22.  
  23.     temp1 = GetModuleFileNameA(0, temp, 32769);
  24.     if (temp1 == 0) return '\0';
  25.  
  26.     strcpy(result, (const char *)PathFindFileNameA(temp));
  27.  
  28.     FreeLibrary(hShlwapi);
  29.     return result;
  30. }
  31.  
  32.  
  33. int main() {
  34.     printf("%s\n", GetExeName());
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement