|
|
<iframe align="top" marginwidth="0" marginheight="0" src="http://www.zealware.com/46860.html" frameborder="0" width="468" scrolling="no" height="60"></iframe><div class="postText">java SWT嵌入IE,SafeArray
/*
*ReadingandwritingtoaSAFEARRAY
*
*ThisexamplereadsfromaPostDataobjectinaBeforeNavigate2eventand
*createsaPostDataobjectinacalltoNavigate.
*
*ForalistofallSWTexamplesnippetssee
*http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
importorg.eclipse.swt.SWT;
importorg.eclipse.swt.SWTException;
importorg.eclipse.swt.internal.win32.OS;
importorg.eclipse.swt.layout.GridData;
importorg.eclipse.swt.layout.GridLayout;
importorg.eclipse.swt.ole.win32.OLE;
importorg.eclipse.swt.ole.win32.OleAutomation;
importorg.eclipse.swt.ole.win32.OleControlSite;
importorg.eclipse.swt.ole.win32.OleEvent;
importorg.eclipse.swt.ole.win32.OleFrame;
importorg.eclipse.swt.ole.win32.OleListener;
importorg.eclipse.swt.ole.win32.Variant;
importorg.eclipse.swt.widgets.Button;
importorg.eclipse.swt.widgets.Display;
importorg.eclipse.swt.widgets.Event;
importorg.eclipse.swt.widgets.Listener;
importorg.eclipse.swt.widgets.Shell;
importorg.eclipse.swt.widgets.Text;
publicclassSnippet186{
staticintCodePage=OS.GetACP();
publicstaticvoidmain(String[]args){
Displaydisplay=newDisplay();
Shellshell=newShell(display);
shell.setLayout(newGridLayout(2,false));
finalTexttext=newText(shell,SWT.BORDER);
text
.setLayoutData(newGridData(SWT.FILL,SWT.CENTER,true,false,
1,1));
Buttongo=newButton(shell,SWT.PUSH);
go.setText("Go");
OleFrameoleFrame=newOleFrame(shell,SWT.NONE);
oleFrame.setLayoutData(newGridData(SWT.FILL,SWT.FILL,true,true,2,
1));
OleControlSitecontrolSite;
OleAutomationautomation;
try{
controlSite=newOleControlSite(oleFrame,SWT.NONE,
"Shell.Explorer");
automation=newOleAutomation(controlSite);
controlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
}catch(SWTExceptionex){
return;
}
finalOleAutomationauto=automation;
go.addListener(SWT.Selection,newListener(){
publicvoidhandleEvent(Evente){
Stringurl=text.getText();
int[]rgdispid=auto.getIDsOfNames(newString[]{"Navigate",
"URL"});
intdispIdMember=rgdispid[0];
Variant[]rgvarg=newVariant[1];
rgvarg[0]=newVariant(url);
int[]rgdispidNamedArgs=newint[1];
rgdispidNamedArgs[0]=rgdispid[1];
auto.invoke(dispIdMember,rgvarg,rgdispidNamedArgs);
}
});
//ReadPostDatawheneverwenavigatetoasitethatusesit
intBeforeNavigate2=0xfa;
controlSite.addEventListener(BeforeNavigate2,newOleListener(){
publicvoidhandleEvent(OleEventevent){
Varianturl=event.arguments[1];
VariantpostData=event.arguments[4];
if(postData!=null){
System.out.println("PostData="+readSafeArray(postData)
+",URL="+url.getString());
}
}
});
//Navigatetothiswebsitewhichusespostdatatofillinthetext
//field
//andputthestring"helloworld"intothetextbox
text.setText("file://"
+Snippet186.class.getResource("Snippet186.html").getFile());
int[]rgdispid=automation.getIDsOfNames(newString[]{"Navigate",
"URL","PostData"});
intdispIdMember=rgdispid[0];
Variant[]rgvarg=newVariant[2];
rgvarg[0]=newVariant(text.getText());
rgvarg[1]=writeSafeArray("helloworld");
int[]rgdispidNamedArgs=newint[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();
}
//ThefollowingstructsareaccessedinthereadSafeArrayand
//writeSafeArray
//functions:
//
//VARIANT:
//shortvt
//shortwReserved1
//shortwReserved2
//shortwReserved3
//intparray
//
//SAFEARRAY:
//shortcDims//Countofdimensionsinthisarray
//shortfFeatures//FlagsusedbytheSafeArray
//intcbElements//Sizeofanelementofthearray
//intcLocks//Numberoftimesthearrayhasbeenlockedwithout
//correspondingunlock
//intpvData//Pointertothedata
//SAFEARRAYBOUND[]rgsabound//Oneboundforeachdimension
//
//SAFEARRAYBOUND:
//intcElements//thenumberofelementsinthedimension
//intlLbound//thelowerboundofthedimension
staticStringreadSafeArray(VariantvariantByRef){
//Readasafearraythatcontainsdataof
//typeVT_UI1(unsignedshorts)whichcontains
//atextstream.
intpPostData=variantByRef.getByRef();
short[]vt_type=newshort[1];
OS.MoveMemory(vt_type,pPostData,2);
Stringresult=null;
if(vt_type[0]==(short)(OLE.VT_BYREF|OLE.VT_VARIANT)){
int[]pVariant=newint[1];
OS.MoveMemory(pVariant,pPostData+8,4);
vt_type=newshort[1];
OS.MoveMemory(vt_type,pVariant[0],2);
if(vt_type[0]==(short)(OLE.VT_ARRAY|OLE.VT_UI1)){
int[]pSafearray=newint[1];
OS.MoveMemory(pSafearray,pVariant[0]+8,4);
short[]cDims=newshort[1];
OS.MoveMemory(cDims,pSafearray[0],2);
int[]pvData=newint[1];
OS.MoveMemory(pvData,pSafearray[0]+12,4);
intsafearrayboundOffset=0;
for(inti=0;i[0];i++){
int[]cElements=newint[1];
OS.MoveMemory(cElements,pSafearray[0]+16
+safearrayboundOffset,4);
safearrayboundOffset+=8;
intcchWideChar=OS.MultiByteToWideChar(CodePage,
OS.MB_PRECOMPOSED,pvData[0],-1,null,0);
if(cchWideChar==0)
returnnull;
char[]lpWideCharStr=newchar[cchWideChar-1];
OS.MultiByteToWideChar(CodePage,OS.MB_PRECOMPOSED,
pvData[0],-1,lpWideCharStr,lpWideCharStr.length);
result=newString(lpWideCharStr);
}
}
}
returnresult;
}
staticVariantwriteSafeArray(Stringstring){
//CreateaonedimensionalsafearraycontainingtwoVT_UI1values
//whereVT_UI1isanunsignedchar
//DefinecDims,fFeaturesandcbElements
shortcDims=1;
shortFADF_FIXEDSIZE=0x10;
shortFADF_HAVEVARTYPE=0x80;
shortfFeatures=(short)(FADF_FIXEDSIZE|FADF_HAVEVARTYPE);
intcbElements=1;
//Createapointerandcopythedataintoit
intcount=string.length();
char[]chars=newchar[count+1];
string.getChars(0,count,chars,0);
intcchMultiByte=OS.WideCharToMultiByte(CodePage,0,chars,-1,null,
0,null,null);
if(cchMultiByte==0)
returnnull;
intpvData=OS.GlobalAlloc(OS.GMEM_FIXED|OS.GMEM_ZEROINIT,
cchMultiByte);
OS.WideCharToMultiByte(CodePage,0,chars,-1,pvData,cchMultiByte,
null,null);
intcElements1=cchMultiByte;
intlLbound1=0;
//Createasafearrayinmemory
//12bytesforcDims,fFeaturesandcbElements+4bytesforpvData+
//numberofdimensions*(sizeofsafearraybound)
intsizeofSafeArray=12+4+1*8;
intpSafeArray=OS.GlobalAlloc(OS.GMEM_FIXED|OS.GMEM_ZEROINIT,
sizeofSafeArray);
//Copythedataintothesafearray
intoffset=0;
OS.MoveMemory(pSafeArray+offset,newshort[]{cDims},2);
offset+=2;
OS.MoveMemory(pSafeArray+offset,newshort[]{fFeatures},2);
offset+=2;
OS.MoveMemory(pSafeArray+offset,newint[]{cbElements},4);
offset+=4;
OS.MoveMemory(pSafeArray+offset,newint[]{0},4);
offset+=4;
OS.MoveMemory(pSafeArray+offset,newint[]{pvData},4);
offset+=4;
OS.MoveMemory(pSafeArray+offset,newint[]{cElements1},4);
offset+=4;
OS.MoveMemory(pSafeArray+offset,newint[]{lLbound1},4);
offset+=4;
//Createavariantinmemorytoholdthesafearray
intpVariant=OS.GlobalAlloc(OS.GMEM_FIXED|OS.GMEM_ZEROINIT,
Variant.sizeof);
shortvt=(short)(OLE.VT_ARRAY|OLE.VT_UI1);
OS.MoveMemory(pVariant,newshort[]{vt},2);
OS.MoveMemory(pVariant+8,newint[]{pSafeArray},4);
//Createabyrefvariant
VariantvariantByRef=newVariant(pVariant,
(short)(OLE.VT_BYREF|OLE.VT_VARIANT));
returnvariantByRef;
}
}
cited:http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/ReadingandwritingtoaSAFEARRAY.htm
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1567897 |
|