英文:
Save data from HANDLE to a file and load data from file to a HANDLE without data loss
问题
有没有方法(或函数)可以将HANDLE
中的数据保存到文件中,以防数据丢失,并将文件中的数据加载到HANDLE
中?假设这是从GetClipboardData
获取的数据的HANDLE
。
如果对剪贴板格式中的所有可能的数据类型都有答案,那将会很棒,但我会优先考虑CF_LOCALE
、CF_TEXT
、CF_OEMTEXT
和CF_UNICODETEXT
。
我尝试将HANDLE
转换为指针,但我不知道如何正确获取数据(因为句柄只指向第一个数据,没有进一步的信息)。
我尝试获取一些有用的句柄信息以了解可能包含的内容,但我无法检索到。
试图解决这个问题的尝试太少了,几乎没有人问过这个问题...
英文:
Are there any ways (or functions) to save data from HANDLE
to a file without data loss and load data from file to a HANDLE
? Assuming that it's a HANDLE
to a data obtained from GetClipboardData
.
It would be great if there are answers for all possible data types in clipboard formats but I would look by piority for CF_LOCALE
, CF_TEXT
, CF_OEMTEXT
and CF_UNICODETEXT
.
I tried to convert from HANDLE
to pointer but I don't know how to get the data properly (as the handle only point to the first data without further information)
I tried to obtain some useful handle information to know what is possibly inside, but I cannot retrieve it.
There are too few attempts to try as very few (or no one) asked about this question...
答案1
得分: 1
没有现成的解决方案来处理这个任务。您将不得不手动完成所有操作。
使用 EnumClipboardFormats()
来查找当前在剪贴板上可用的格式,以及使用 GetClipboardData()
来访问每种格式的数据。
或者,使用 OleGetClipboard()
来获取包含剪贴板内容的 IDataObject
,然后使用其 EnumFormatEtc()
和 GetData()
方法来枚举和访问其数据格式。
然后,您可以根据需要手动将每种格式的数据写入您自己的文件,然后稍后从文件中读取数据,并根据每种格式类型使用 SetClipboardData()
或 OleSetClipboard()
将数据放回剪贴板。
标准剪贴板格式的数据格式在 MSDN 上有文档:
例如,CF_LOCALE
的 HANDLE
是分配的内存块的 LCID
标识符的 HGLOBAL
句柄,而 CF_TEXT
/CF_UNICODETEXT
的 HANDLE
是分配的内存块的实际 ANSI/Unicode 文本字符的 HGLOBAL
句柄。
如果您想保存/加载非标准剪贴板格式的数据,除非您对这些数据在剪贴板内存中的格式有深入了解,否则无法完成。
英文:
There is no pre-existing solution to handle this task for you. You will have to do everything manually.
Use EnumClipboardFormats()
to find which formats are currently available on the clipboard, and GetClipboardData()
to access the data of each format.
Or, use OleGetClipboard()
to get an IDataObject
wapping the clipboard's content, and then enumerate and access its data formats using its EnumFormatEtc()
and GetData()
methods.
Then, you can manually write each format's data to your own file as needed, and then later read the data back from your file and put it back on the clipboard according to each format type using SetClipboardData()
or OleSetClipboard()
.
The data formats of the standard clipboard formats are documented on MSDN:
For example, a HANDLE
for a CF_LOCALE
is an HGLOBAL
handle to an allocated block of memory holding a LCID
identifier, whereas a HANDLE
for a CF_TEXT
/CF_UNICODETEXT
is an HGLOBAL
handle to an allocated block of memory holding the actual ANSI/Unicode text characters.
If you want to save/load data for non-standard clipboard formats, you won't be able to do that unless you have intimate knowledge of how their data is formatted in memory for the clipboard to hold.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论