0

I found this website from google and I assume that here people helps with coding problems.

I am creating a badword filter to an application, but I have ran into problems. Currently I am creating a thread from the applications entry point, and the thread flow goes like this:

while(true)
{

    if (!OpenClipboard(NULL))
        ExitProcess(0); //TODO: Try opening clipboard again.

    h = GetClipboardData(CF_TEXT); //h is HANDLE.

    std::string CB_Data = (char*)h;

    if(CB_Data.size() != NULL) //An attempt to check if it's not empty
    {
        if ( std::regex_search(CB_Data.c_str(), BADWORD_FILTER))
        {
            try
            {
                EmptyClipboard();
                SetClipboardData(CF_TEXT, INFORMATION); //INFORMATION is converted to char from HANDLE. INFORMATION = "Bad word filter detected forbidden words pattern."
            }
            catch(...)
            {
                //TODO: Error logging
            }
        }

        else if ( std::regex_search(CB_Data.c_str(), BADWORD_FILTER2))
        {
            try
            {
                EmptyClipboard();
                SetClipboardData(CF_TEXT, INFORMATION); //INFORMATION is converted to char from HANDLE. INFORMATION = "Bad word filter detected forbidden words pattern."
            }
            catch(...)
            {
                //TODO: Error logging
            }
            EmptyClipboard();
            SetClipboardData(CF_TEXT, INFORMATION); //INFORMATION is converted to char from HANDLE. INFORMATION = "Bad word filter detected forbidden words pattern."
        }
    }

    CloseClipboard();
    Sleep(1000); //Check every 1 second for the forbidden words.
}

So this application monitors clipboard from forbidden words. However, most of the time I run into an "Expression: Invalid null pointed" -error, and I am not familiar with Visual Studio debugger. I tried, but obviously didn't success very well.

Here is the error: https://i.stack.imgur.com/wACnA.png

Any help would be greatly appreciated, thank you.

1 Answers1

0

You get an error because GetClipboardData() does not return a const char* pointer and the std::string constructor tries to read your parameter as a const char*.

As you state in the question, GetClipboardData() returns a HANDLE. See this topic for a correct example how to use this function.

Other useful tips:

  • Press the "Retry" button and you will land in the debugger. Using the debugger you can see exactly where your program went wrong
  • CB_Data.size() is an unsigned integer value (the length of the string), not a pointer. Do not compare it with NULL!
Community
  • 1
  • 1
Csq
  • 5,775
  • 6
  • 26
  • 39