Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Confused about stricmp



  • There have been many threads ont he internet which have used the following and apparently is supposed to work for finding a process by name:

    @
    if (stricmp(proc.szExeFile, "test") == 0)
    @

    However that doesn't work for me, it gives an error about using WCHAR* where it should be CHAR*.

    So I tried using this function which I found:

    @
    void strcpy_wc(char *Dest, wchar_t *Src)
    {
    while (*Src)
    {
    *Dest++ = *Src++;
    }
    *Dest = 0;
    }
    @

    For this:

    @
    char *procName;
    strcpy_wc(procName, proc.szExeFile);
    if (stricmp(procName, "test") == 0)
    @

    But the app crashes, and when debugging I get segment fault error on line 5 of the function strcpy_wc()

    Any ideas?



  • If you work with unicode strings, I suggest you to use unicode version of the function that you mentioned above: wcscmp, wcscpy. However, you can transform your ansi C string in unicode format with "MultiByteToWideChar":http://msdn.microsoft.com/en-us/library/bb202786.aspx or "mbstowcs":http://msdn.microsoft.com/en-us/library/k1f9b8cy(v=vs.80).aspx



  • You can use wcscmp() instead of stricmp. Also, may I suggest using QProcess, if at all possible in your app.

    @
    if (wcscmp(proc.szExeFile, L"test") == 0)
    @



  • Thanks that worked, now to test :)



  • Just as a side note to explain the SIGSEV. You didn't allocate any memory for your Dest char*.



  • [quote author="Monkey666" date="1312207879"]There have been many threads ont he internet which have used the following and apparently is supposed to work for finding a process by name:

    @
    if (stricmp(proc.szExeFile, "test") == 0)
    @

    However that doesn't work for me, it gives an error about using WCHAR* where it should be CHAR*.

    [/quote]

    I've already used win32 API (i suppose you too). To solve the problem in Visual studio just set "Character set" within project setting to non unicode and you don't have anymore that problem


Log in to reply