问题在标题中描述。
我有两个功能。在第一个我得到文件名(变量text
),但该函数不返回预期值。返回值text
后,它变为abrakadabra。但是在第二个函数变量text
中正确返回。任何帮助将不胜感激。
char* GetCurrentClipboardData(...)
{
char* text;
wchar_t file[MAX_PATH];
if( OpenClipboard(NULL) )
{
HGLOBAL hFile = (HGLOBAL)GetClipboardData(CF_HDROP);
if (hFile)
{
HDROP hDrop = (HDROP)GlobalLock(hFile);
DragQueryFile(hDrop, 0, file, MAX_PATH);
_bstr_t b(file);
text = b;
if (text != Text)
{
SaveDataToFile (file_path, current_time, text);
char* copy = ReadFile(shadowcopy_path);
if (copy == "1")
MakeFileShadowCopy(file, shadowcopies);
}
GlobalUnlock(hFile);
}
HBITMAP hBitmap = (HBITMAP)GetClipboardData(CF_BITMAP);
if (hBitmap)
{
text = "Изображение";
if (text != Text)
{
SaveDataToFile (image_path, current_time, text);
char* copy = ReadFile(shadowcopy_path);
if (copy == "1")
MakeImageShadowCopy(hBitmap, shadowcopies, current_date, current_time);
}
GlobalUnlock(hBitmap);
}
CloseClipboard();
}
return text;
}
答案 0 :(得分:2)
该函数返回一个指针text
,该指针在函数返回后被释放,因此它是未定义的行为。你可以使用malloc在堆上为它分配内存,但你也需要释放它。
char* text;
text = (char*)malloc(text_size * sizeof(char));
或C ++,
char* text = new char[text_size];
其中text_size
是字符串的大小。