Save data from HANDLE to a file and load data from file to a HANDLE without data loss

huangapple go评论72阅读模式
英文:

Save data from HANDLE to a file and load data from file to a HANDLE without data loss

问题

有没有方法(或函数)可以将HANDLE中的数据保存到文件中,以防数据丢失,并将文件中的数据加载到HANDLE中?假设这是从GetClipboardData获取的数据的HANDLE

如果对剪贴板格式中的所有可能的数据类型都有答案,那将会很棒,但我会优先考虑CF_LOCALECF_TEXTCF_OEMTEXTCF_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 上有文档:

标准剪贴板格式

Shell 剪贴板格式

例如,CF_LOCALEHANDLE 是分配的内存块的 LCID 标识符的 HGLOBAL 句柄,而 CF_TEXT/CF_UNICODETEXTHANDLE 是分配的内存块的实际 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:

Standard Clipboard Formats

Shell Clipboard Formats

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.

huangapple
  • 本文由 发表于 2023年2月6日 21:49:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75362152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定