locked
Can't Get Module Name For Process Module RRS feed

  • Question

  • LONG findprogram()
    {
         LONG count;
         DWORD aProcesses[1024],cbNeeded,cProcesses,processID,CurrentProcessId,lpExitCode;
         unsigned int i;
         std::wstring szProcessName;
         HANDLE hProcess;
         HMODULE hMod;
         std::string namepath="PROGRAMNAME.EXE";
         std::string cmpnamepath;
         std::wstring wtxt;

         CurrentProcessId=GetCurrentProcessId();

         if(!EnumProcesses(aProcesses,sizeof(aProcesses),&cbNeeded))
         {
           return 0;
         }

         cProcesses=cbNeeded/sizeof(DWORD);
         count=1;  //number of processes running

         for(i=0;i<cProcesses;i++)
         {
           if(aProcesses[i] != 0)
           {
             processID=aProcesses[i];

             if(processID == CurrentProcessId)  //leave out current program
               continue;  

             hProcess=OpenProcess(PROCESS_QUERY_INFORMATION,FALSE,processID);

             if(hProcess)
             {
               WCHAR Buffer[MAX_PATH];

               if(GetModuleFileNameEx(hProcess,NULL,Buffer,MAX_PATH))
               {
                 szProcessName=Buffer;
               }
             }

             showtextmessagew(szProcessName);  //always blank

             wtxt=szProcessName;
             cmpnamepath=wstrtostr(wtxt);

             if(ucase(cmpnamepath) == namepath)
             {  
               count++;
               GetExitCodeProcess(hProcess,&lpExitCode);
               TerminateProcess(hProcess,lpExitCode);
             }  

             CloseHandle(hProcess);
           }
         }

         return count;
    }


    Monday, October 12, 2020 10:40 PM

All replies

  • Hi Tall_Guy_63,
    The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.
    For your question, I suggest you go to win32 Q&A forum for more professional answer.
    Thank you for your understanding.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 13, 2020 3:13 AM
  • Thanks
    Tuesday, October 13, 2020 3:57 AM