strstr on a wchar array

This is a discussion on strstr on a wchar array within the C++ Programming forums, part of the General Programming Boards category; Hey all, Im trying to write some code that checks if a certain process is running which uses the PROCESSENTRY32 ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    strstr on a wchar array

    Hey all,

    Im trying to write some code that checks if a certain process is running which uses the PROCESSENTRY32 structure. The variable szExeFile is returning a WCHAR (even though msdn says its a supposed to be a TCHAR). This is my code:

    Code:
    HANDLE hSnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
    	PROCESSENTRY32* processInfo=new PROCESSENTRY32;
    	processInfo->dwSize=sizeof(PROCESSENTRY32W);
    	while(Process32Next(hSnapShot,processInfo)!=FALSE)
    	{		
    	
    		if((strstr(processInfo->szExeFile, "myprocess"))!=NULL)
    		{		
    			MessageBox(NULL, processInfo->szExeFile, _T("hi"), MB_OK);
    			delete[] processInfo;
    			CloseHandle(hSnapShot);			
    			return true;
    		}
    	}	
    	delete[] processInfo;
    	CloseHandle(hSnapShot);
    	return false;
    I get an error when I compile saying

    error C2665: 'strstr' : none of the 2 overloads could convert all the argument types
    d:\program files\microsoft visual studio 8\vc\include\string.h(163): could be 'const char *strstr(const char *,const char *)'
    d:\program files\microsoft visual studio 8\vc\include\string.h(187): or 'char *strstr(char *,const char *)'
    while trying to match the argument list '(WCHAR [260], const char [8])'
    I then change the strstr command to

    Code:
    if((strstr((char*)processInfo->szExeFile, "myprocess"))!=NULL)
    it compiles fine but the strstr always returns NULL even though im 100% sure that the process myprocess is running. Therefore the (char*)processInfo-szExeFile must be returning junk values.

    Any idea how to solve this?

    Thanks

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    It seems you're compiling with UNICODE and _UNICODE defined (msvc2005 by any chance?) - strstr works on char arrays, not wchar_t(WCHAR) arrays; try wcsstr instead. And use the _T macro on all string literals.

    However, since you're posting this on the c++ board, perhaps converting to a std::wstring would be more appropriate; you can then use the std::wstring::find member function to find the first occurrence of "myprocess" therein.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    thanks for the reply

    You are right about VS2005. The wcsstr worked perfectly. I know this is probably a dumb question but is there anyway to reconfigure VS2005 to work the same as VS6 so i dont have to keep using _T and wchar as i'm not used to it and its really slowing me down.

    Thanks

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Yes, but you should get used to using unicode sooner rather than later.

    project menu --> project properties, configuration properties-general; change 'character set' to 'not set'
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    what is the advantage to using unicode then? Thanks

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    It's used by default on windows since win2000. If you use chars or their equivalent, the system has to translate them to unicode before it can do anything with them. More importantly, use of unicode permits you to represent every character in every written language.

    For more information, here's google: unicode; lots of good reading there.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  3. Quick Ques on String Manipulation
    By ckuttruff in forum C Programming
    Replies: 8
    Last Post: 06-22-2008, 10:32 PM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21