java SWT嵌入IE,SafeArray

【Java】jacob读写Excel指定区域数据【Variant,SafeArray数据读取】 【Java】jacob读写Excel指定区域数据,一次性从Excel读取,非单元格一个个读取 阅读详情
2007年04月17日 15:20:00

java SWT嵌入IE,SafeArray

/*
* Reading and writing to a SAFEARRAY
*
* This example reads from a PostData object in a BeforeNavigate2 event and
* creates a PostData object in a call to Navigate.
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleControlSite;
import org.eclipse.swt.ole.win32.OleEvent;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.OleListener;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Snippet186 {

static int CodePage = OS.GetACP();

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(2, false));

final Text text = new Text(shell, SWT.BORDER);
text
.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
1, 1));
Button go = new Button(shell, SWT.PUSH);
go.setText("Go");
OleFrame oleFrame = new OleFrame(shell, SWT.NONE);
oleFrame.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2,
1));
OleControlSite controlSite;
OleAutomation automation;
try {
controlSite = new OleControlSite(oleFrame, SWT.NONE,
"Shell.Explorer");
automation = new OleAutomation(controlSite);
controlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
} catch (SWTException ex) {
return;
}

final OleAutomation auto = automation;
go.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
String url = text.getText();
int[] rgdispid = auto.getIDsOfNames(new String[] { "Navigate",
"URL" });
int dispIdMember = rgdispid[0];
Variant[] rgvarg = new Variant[1];
rgvarg[0] = new Variant(url);
int[] rgdispidNamedArgs = new int[1];
rgdispidNamedArgs[0] = rgdispid[1];
auto.invoke(dispIdMember, rgvarg, rgdispidNamedArgs);
}
});

// Read PostData whenever we navigate to a site that uses it
int BeforeNavigate2 = 0xfa;
controlSite.addEventListener(BeforeNavigate2, new OleListener() {
public void handleEvent(OleEvent event) {
Variant url = event.arguments[1];
Variant postData = event.arguments[4];
if (postData != null) {
System.out.println("PostData = " + readSafeArray(postData)
+ ", URL = " + url.getString());
}
}
});

// Navigate to this web site which uses post data to fill in the text
// field
// and put the string "hello world" into the text box
text.setText("file://"
+ Snippet186.class.getResource("Snippet186.html").getFile());
int[] rgdispid = automation.getIDsOfNames(new String[] { "Navigate",
"URL", "PostData" });
int dispIdMember = rgdispid[0];
Variant[] rgvarg = new Variant[2];
rgvarg[0] = new Variant(text.getText());
rgvarg[1] = writeSafeArray("hello world");
int[] rgdispidNamedArgs = new int[2];
rgdispidNamedArgs[0] = rgdispid[1];
rgdispidNamedArgs[1] = rgdispid[2];
automation.invoke(dispIdMember, rgvarg, rgdispidNamedArgs);

shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

// The following structs are accessed in the readSafeArray and
// writeSafeArray
// functions:
//
// VARIANT:
// short vt
// short wReserved1
// short wReserved2
// short wReserved3
// int parray
//
// SAFEARRAY:
// short cDims // Count of dimensions in this array
// short fFeatures // Flags used by the SafeArray
// int cbElements // Size of an element of the array
// int cLocks // Number of times the array has been locked without
// corresponding unlock
// int pvData // Pointer to the data
// SAFEARRAYBOUND[] rgsabound // One bound for each dimension
//
// SAFEARRAYBOUND:
// int cElements // the number of elements in the dimension
// int lLbound // the lower bound of the dimension

static String readSafeArray(Variant variantByRef) {
// Read a safearray that contains data of
// type VT_UI1 (unsigned shorts) which contains
// a text stream.
int pPostData = variantByRef.getByRef();
short[] vt_type = new short[1];
OS.MoveMemory(vt_type, pPostData, 2);
String result = null;
if (vt_type[0] == (short) (OLE.VT_BYREF | OLE.VT_VARIANT)) {
int[] pVariant = new int[1];
OS.MoveMemory(pVariant, pPostData + 8, 4);
vt_type = new short[1];
OS.MoveMemory(vt_type, pVariant[0], 2);
if (vt_type[0] == (short) (OLE.VT_ARRAY | OLE.VT_UI1)) {
int[] pSafearray = new int[1];
OS.MoveMemory(pSafearray, pVariant[0] + 8, 4);
short[] cDims = new short[1];
OS.MoveMemory(cDims, pSafearray[0], 2);
int[] pvData = new int[1];
OS.MoveMemory(pvData, pSafearray[0] + 12, 4);
int safearrayboundOffset = 0;
for (int i = 0; i > cDims[0]; i++) {
int[] cElements = new int[1];
OS.MoveMemory(cElements, pSafearray[0] + 16
+ safearrayboundOffset, 4);
safearrayboundOffset += 8;
int cchWideChar = OS.MultiByteToWideChar(CodePage,
OS.MB_PRECOMPOSED, pvData[0], -1, null, 0);
if (cchWideChar == 0)
return null;
char[] lpWideCharStr = new char[cchWideChar - 1];
OS.MultiByteToWideChar(CodePage, OS.MB_PRECOMPOSED,
pvData[0], -1, lpWideCharStr, lpWideCharStr.length);
result = new String(lpWideCharStr);
}
}
}
return result;
}

static Variant writeSafeArray(String string) {
// Create a one dimensional safearray containing two VT_UI1 values
// where VT_UI1 is an unsigned char

// Define cDims, fFeatures and cbElements
short cDims = 1;
short FADF_FIXEDSIZE = 0x10;
short FADF_HAVEVARTYPE = 0x80;
short fFeatures = (short) (FADF_FIXEDSIZE | FADF_HAVEVARTYPE);
int cbElements = 1;
// Create a pointer and copy the data into it
int count = string.length();
char[] chars = new char[count + 1];
string.getChars(0, count, chars, 0);
int cchMultiByte = OS.WideCharToMultiByte(CodePage, 0, chars, -1, null,
0, null, null);
if (cchMultiByte == 0)
return null;
int pvData = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT,
cchMultiByte);
OS.WideCharToMultiByte(CodePage, 0, chars, -1, pvData, cchMultiByte,
null, null);
int cElements1 = cchMultiByte;
int lLbound1 = 0;
// Create a safearray in memory
// 12 bytes for cDims, fFeatures and cbElements + 4 bytes for pvData +
// number of dimensions * (size of safearraybound)
int sizeofSafeArray = 12 + 4 + 1 * 8;
int pSafeArray = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT,
sizeofSafeArray);
// Copy the data into the safe array
int offset = 0;
OS.MoveMemory(pSafeArray + offset, new short[] { cDims }, 2);
offset += 2;
OS.MoveMemory(pSafeArray + offset, new short[] { fFeatures }, 2);
offset += 2;
OS.MoveMemory(pSafeArray + offset, new int[] { cbElements }, 4);
offset += 4;
OS.MoveMemory(pSafeArray + offset, new int[] { 0 }, 4);
offset += 4;
OS.MoveMemory(pSafeArray + offset, new int[] { pvData }, 4);
offset += 4;
OS.MoveMemory(pSafeArray + offset, new int[] { cElements1 }, 4);
offset += 4;
OS.MoveMemory(pSafeArray + offset, new int[] { lLbound1 }, 4);
offset += 4;
// Create a variant in memory to hold the safearray
int pVariant = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT,
Variant.sizeof);
short vt = (short) (OLE.VT_ARRAY | OLE.VT_UI1);
OS.MoveMemory(pVariant, new short[] { vt }, 2);
OS.MoveMemory(pVariant + 8, new int[] { pSafeArray }, 4);
// Create a by ref variant
Variant variantByRef = new Variant(pVariant,
(short) (OLE.VT_BYREF | OLE.VT_VARIANT));
return variantByRef;
}
}


cited:http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/ReadingandwritingtoaSAFEARRAY.htm



Trackback: http://tb.blog.youkuaiyun.com/TrackBack.aspx?PostId=1567897


Java通过Jacob调用ActiveX,进行二进制数据交换 该工程项目演示了Java通过jacob1.17调用ActiveX的基本方法。对于单个的基本类型的数据(byte、short、long、BOOL)等类型的数据,以及有很多字节组成的可见英文字符、汉字等组成的字符串(LPCTSTR、BSTR类型都会进行编码转换)来说,使用VC的ActiveX项目向导可以方便地实现;对于二进制数据的交换必须使用Variant类型,但是在实际使用过程中使用VC2008ActiveX项目向导生成的ocx触发事件中带有Variant类型的参数时,ocx控件在触发事件消息之后就会出现内存异常,导致程序无法正常运行,而采用VC6.0生成的事件触发代码就运行正常,不知道是什么原因。采用Variant的SafeArray数据类型可以实现二进制数据的交换,通过SafeArrayCreateVector创建数据缓存空间,SafeArrayAccessData锁定内存数据块,SafeArrayUnaccessData解除内存数据块的锁定,SafeArrayDestroy释放内存数据空间。 立即下载

相关推荐

java调用activex控件

在java中调用activex控件,从而实现浏览器上的操作

SafeArrayDemo.java

public class SafeArrayDemo{  public static void main(String[] args){    SafeArray arr1=new SafeArray();    SafeArray arr2=new SafeArray(5);     for(int i=0; i      arr1.setElement(i,(i+1)*10);     for

ALEXAMAHONE的专栏 461

【go-libp2p学习笔记】使用go-libp2p搭建中转服务器(circuit relay server)

libp2p算是一个蛮新的库,提供了非常强大的p2p节点发现/连接/通信能力。IPFS的基石。 中文网上的**技术教程**比较少,所以抛砖引玉开一点坑来体验一下!

Cake_C的博客 6491

[java] 屏蔽ie浏览器中F1帮助

实现某一功能时需要屏蔽IE浏览器中的F1,F2快捷键。 百度后发现如下代码 1 var jq$ = jQuery.noConflict(); //防止$符号与其他的冲突 2 jq$(document).ready(function () { //防止在DOM元素加载完成就执行jQuery代码,从而避免产生不必要的错误 3 jq$("*...

weixin_34375233的博客 254

Crawl4AI实战:5分钟搞定电商价格监控爬虫(附Python代码)

本文详细介绍了如何利用开源AI网络爬虫框架Crawl4AI快速构建电商价格监控系统。通过Python实战代码,展示了从环境配置、智能内容提取到多平台适配、定时任务与数据可视化的完整流程,帮助运营人员和技术开发者高效实现自动化价格追踪与竞品分析。

weixin_29090917的博客 414

日志异常检测详细整理,看这一篇就够了(附各项目代码链接)

对于不用的日志键,用它们的参数值向量训练一个参数值异常检测模型来检测有这些参数值反映的系统性能异常。与标准的GAN框架一样,D和G的参数是基于D的输出来更新的,因此鉴别器D可以被训练为尽可能敏感地将正确的标签分配给真实和虚假序列,而生成器将被训练为尽可能聪明地在经过足够多轮的迭代后欺骗鉴别器,即误导D将真实标签分配给虚假序列。LR:逻辑回归是有监督的模型,将处理后的特征矩阵以及标记作为模型的输入,得到训练后的模型,用此模型预测新出现的特征向量,以此判断特征向量是否为异常的,进而判断日志异常。

qq_20368489的博客 1万+

基于SWT组件的IE内核Java简易浏览器

利用Java编程实现解释显示效果较好的简单浏览器。 网络上有很多五花八门的用Java制作的简易浏览器,这里简单介绍一种利用SWT组件开发的简易浏览器,有关SWT组件的具体介绍详细参见eclipse官网:https://www.eclipse.org/swt/ 这里仅利用了此组件的一小部分功能。 使用此组件需要先下载SWT.jar包,下载连接和源程序代码以打包上传至: 代码贴在这里: package test; import org.eclipse.swt.SWT; import org.eclipse.s

qq_44808868的博客 963

RPA实战-UiPath自动化处理Outlook邮件附件全流程解析

本文详细解析了如何使用UiPath实现Outlook邮件附件的自动化处理全流程,涵盖环境配置、核心流程实现、高级功能扩展及性能优化技巧。通过RPA技术,企业可大幅提升邮件附件处理效率,减少人工操作错误,适用于财务、物流等多个场景。

weixin_33701251的博客 427

java SWT嵌入IE,SafeArray .

java SWT嵌入IE,SafeArray /**ReadingandwritingtoaSAFEARRAY**ThisexamplereadsfromaPostDataobjectinaBeforeNavigate2eventand*createsaPostDataobjectinacalltoNavigate.**Fo...

weixin_30735745的博客 91

Qt+PCL实战:如何用QVTKOpenGLNativeWidget高效渲染3D点云(附完整代码)

本文深入解析了Qt与PCL结合使用QVTKOpenGLNativeWidget进行高效3D点云渲染的实战技术。通过对比传统QVTKWidget,详细介绍了QVTKOpenGLNativeWidget在渲染效率、内存占用和兼容性方面的显著优势,并提供了完整的代码示例和性能优化策略,助力开发者在工业检测、自动驾驶等领域实现更高效的点云可视化。

keras9composer的博客 687

基于情感分析+聚类分析+LDA主题分析对服装产品类的消费者评论分析

本研究旨在通过对服装产品类的消费者评论进行综合分析,为企业提供更全面、更深入的市场洞察和决策支持。同时,本研究的方法和结果也可以为其他领域的消费者评论分析提供借鉴和参考。

m0_64336780的博客 2万+
上一篇: NAF框架终于有了一个好的开端。
下一篇: Java下简单模仿.net的XmlWriter
mengtech
博客等级 码龄19年 44粉丝 3472原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值