C++ | Помогите новичку

Взял этот код

#include <windows.h>
#include <string.h>

int WINAPI WinMain(HINSTANCE hThisInstance,
                   HINSTANCE hPrevInstance, 
                   LPSTR lpszArgument, 
                   int nFunsterStil)
{

    HANDLE hData;//For the data to send to the clipboard
    char szData[] = "Hello World (from the clipboard!!)",//phrase
         *ptrData = NULL;//pointer to allow char copying
    int nStrLen = strlen(szData);//length of phrase

    hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
        nStrLen + 1);//get handle to memory to hold phrase

    ptrData = (char*)GlobalLock(hData);//get pointer from handle

    memcpy(ptrData,szData,nStrLen + 1);//copy over the phrase

    GlobalUnlock(hData);//free the handle

    OpenClipboard(NULL);//allow you to work with clipboard

    EmptyClipboard();//clear previous contents

    SetClipboardData(CF_TEXT,hData);//set our data

    CloseClipboard();//finished!!


   return 0;
}

Хочу сделать как функцию setClipboard, но не выходит

введите сюда описание изображения


Ответы (1 шт):

Автор решения: Harry

Достаточно вместо szData использовать text.c_str().

Что-то вроде

char *ptrData = NULL;//pointer to allow char copying
hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
    text.size() + 1);//get handle to memory to hold phrase
ptrData = (char*)GlobalLock(hData);//get pointer from handle
memcpy(ptrData,text.c_str(),text.size() + 1);//copy over the phrase

...

И еще - передавайте строку как константную ссылку, чего ее копировать лишний раз?

void setClipboard(const string& text)
→ Ссылка