'Copy Text To ClipBoard Win32Api GUI

This Code Give Me This Warnings - Warning C6387 'buffer' could be '0': this does not adhere to the specification for the function 'memcpy' and Warning C28183 'CopyData' could be '0', and is a copy of the value found in 'GlobalAlloc()'648': this does not adhere to the specification for the function 'GlobalLock', but Code Works.

I Am Working On VisualStudio 2022.

How To Fix This Errors or Hide, I have to need clean Debug Log.

#define MAX_NAME_STRING 256

    TCHAR tsource[MAX_NAME_STRING] = L"This is Test\r\n
    Second Line\r\n
    Bye\r\n";

    if (OpenClipboard(hMainWindow)) {
        HGLOBAL CopyData = { 0 };
        void *buffer = NULL;
        EmptyClipboard();
        CopyData = GlobalAlloc(GMEM_FIXED, sizeof(TCHAR) * MAX_NAME_STRING);
        if (CopyData != NULL) { //FIX THIS WARNING - Warning C28183  'CopyData' could be '0', and is a copy of the value found in 'GlobalAlloc()'648':  this does not adhere to the specification for the function 'GlobalLock'
            buffer = GlobalLock(CopyData);
        }
        if (buffer != NULL) { //FIX THIS WARNING - "Warning  C6387 'buffer' could be '0':  this does not adhere to the specification for the function 'memcpy'"
            memcpy(buffer, (void*)tsource, sizeof(TCHAR) * MAX_NAME_STRING);
            GlobalUnlock(CopyData);
            SetClipboardData(CF_UNICODETEXT, CopyData);
            CloseClipboard();
        }
    }

vvvWorking Function to Copy Text into Clipboard - Use it if neadedvvv
Without Warnings

    bool copyTextToClipboard(HWND hwnd, LPWSTR text, INT textLength) {

        /// <summary>
        /// This Function Puts Text into ClipBoard
        /// </summary>
        /// <param name="hwnd">newClipBoardOwner</param>
        /// <param name="text">Text into ClipBoard</param>
        /// <param name="textLength">TextLength</param>
        /// <returns>If Succeeded Returns True, but If not False</returns>

        if (OpenClipboard(hwnd)) {
            HGLOBAL CopyData = { 0 };
            void *buffer = NULL;
            EmptyClipboard();
            CopyData = GlobalAlloc(GMEM_FIXED, sizeof(TCHAR) * textLength);
            if (CopyData != NULL) {
                buffer = GlobalLock(CopyData);
            }
            if (buffer != NULL) {
                memcpy(buffer, (void*)text, sizeof(TCHAR) * textLength);
                GlobalUnlock(CopyData);
                SetClipboardData(CF_UNICODETEXT, CopyData);
                CloseClipboard();
                return true;
            }
            CloseClipboard();
            return false;
        }
        return false;

    }



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source