如何在 JNI 调用中使用 windows.h setClipboardContent 方法?
我正在实现一个 JNI 方法,该方法调用 windows.h 中的 setClipboardContent() 函数来设置 Windows 剪贴板内容。标题如下所示。 formatName是剪贴板格式,data是Java中的byte[]。这是您要放入剪贴板的数据。
我对如何在 JNI 方法中调用 setClipboardContent() 函数感到困惑。有人可以帮忙吗?
JNIEXPORT jboolean JNICALL Java_msoffice_MSOfficeClipboard_setClipboardContents(JNIEnv *pEnv, jobject, jstring formatName, jbyteArray data)
{
BOOL fSucces = OpenClipboard(NULL);
if (fSucces) {
EmptyClipboard();
const char *str = pEnv->GetStringUTFChars(formatName, NULL);
if (str = NULL) return false; /* OutOfMemoryError already thrown */
UINT format = RegisterClipboardFormat(str);
pEnv->ReleaseStringUTFChars(formatName, str);
// This is where I should call setClipboardContent(format, HANDLE) method. I don't know what to do here.
CloseClipboard();
}
return fSucces;
}
I am implementing a JNI method which calls setClipboardContent() function in windows.h to set windows clipboard content. The header looks as follow. The formatName is the clipboard format, The data is a byte[] in Java. It is the data you want to put in the clipboard.
I am confused about how call the setClipboardContent() function in the JNI method. Can anyone help?
JNIEXPORT jboolean JNICALL Java_msoffice_MSOfficeClipboard_setClipboardContents(JNIEnv *pEnv, jobject, jstring formatName, jbyteArray data)
{
BOOL fSucces = OpenClipboard(NULL);
if (fSucces) {
EmptyClipboard();
const char *str = pEnv->GetStringUTFChars(formatName, NULL);
if (str = NULL) return false; /* OutOfMemoryError already thrown */
UINT format = RegisterClipboardFormat(str);
pEnv->ReleaseStringUTFChars(formatName, str);
// This is where I should call setClipboardContent(format, HANDLE) method. I don't know what to do here.
CloseClipboard();
}
return fSucces;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设剪贴板格式只是一个数据块,您需要使用 GlobalAlloc(GMEM_MOVEABLE) 分配一块内存,并从数据数组中复制数据。某些剪贴板格式需要特殊行为(CF_BITMAP 需要 HBITMAP 等)。
Assuming the clipboard format is just a blob of data, you need to allocate a chunk of memory with GlobalAlloc(GMEM_MOVEABLE) and copy the data from your data array. Some clipboard formats require special behavior (CF_BITMAP requires a HBITMAP, etc).