Get path of executable

前端 未结 23 1534
清歌不尽
清歌不尽 2020-11-22 07:04

I know this question has been asked before but I still haven\'t seen a satisfactory answer, or a definitive \"no, this cannot be done\", so I\'ll ask again!

All I wa

23条回答
  •  无人及你
    2020-11-22 07:49

    I'm not sure about Linux, but try this for Windows:

    #include 
    #include 
    
    using namespace std ;
    
    int main()
    {
         char ownPth[MAX_PATH]; 
    
         // When NULL is passed to GetModuleHandle, the handle of the exe itself is returned
         HMODULE hModule = GetModuleHandle(NULL);
         if (hModule != NULL)
         {
             // Use GetModuleFileName() with module handle to get the path
             GetModuleFileName(hModule, ownPth, (sizeof(ownPth))); 
             cout << ownPth << endl ;
             system("PAUSE");
             return 0;
         }
         else
         {
             cout << "Module handle is NULL" << endl ;
             system("PAUSE");
             return 0;
         }
    }
    

提交回复
热议问题