c++ - Process snapshot can't be compared against wide strings -


i have following problem:

i want keep track of running processes using createtoolhelp32snapshot , process32first/next. want use unicode charset default.

bool active( const std::wstring& process_name ) {     handle snapshot = createtoolhelp32snapshot( th32cs_snapprocess, 0 );      if ( snapshot == invalid_handle_value )         return false;      processentry32 entry;      if ( process32first( snapshot, &entry ) )     {         if ( process_name.compare( entry.szexefile ) == 0 )         {             closehandle( snapshot );             return true;         }     }      while ( process32next( snapshot, &entry ) )     {         if ( process_name.compare( entry.szexefile ) == 0 )         {             closehandle( snapshot );             return true;         }     }      closehandle( snapshot );      return false; }  int main( ) {     setconsoletitle( l"lel" );      if ( active( l"notepad++.exe" ) )         std::cout << "hello" << std::endl;     else         std::cout << ":(" << std::endl; } 

however, if use multibyte-charset everything's working fine.

you must initialize entry , set dwsize value. dwsize value windows's idea of version control , required:

processentry32 entry = { 0 }; entry.dwsize = sizeof(processentry32); 

comparison should not case sensitive:

while (process32next(snapshot, &entry)) {     if (_wcsicmp(process_name.c_str(), entry.szexefile) == 0)     {         closehandle(snapshot);         return true;     } } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -