Re: how can i use clipboard with Jawin?

"Mcnamara, Sean D." <[email protected]> Fri, 27 Oct 2006 13:02:07 -0400
Newsgroups gmane.comp.windows.devel.jawin
Message-ID <57F935D6BB6B2543AF5D835DCE7CAF2711B310@MD61EX100.ad.honeywell-tsi.com>
Alex,
=20
This is covered in the documentation. What you're trying to do is invoke =
methods declared in the user32 dll, which is a part of the native Win32 =
API. In Jawin, you use the FuncPtr object to do this (often accompanied =
by a NakedByteStream and LittleEndianInputStream(s) and =
LittleEndianOutputStream(s).
=20
I had not previously heard of the Win32 API clipboard functions because =
I haven't used the Windows API very much in practice. But from a MSDN =
article on openClipboard(), and from the Jawin documentation, =
http://jawinproject.sourceforge.net/jawinuserguide_dll.html you can look =
at =
http://msdn.microsoft.com/library/default.asp?url=3D/library/en-us/winui/=
winui/windowsuserinterface/dataexchange/clipboard/clipboardreference/clip=
boardfunctions/openclipboard.asp and apply the same process to get to =
the Jawin/Java code needed.
=20
Applying the procedure in the jawinuserguide_dll.html to the msdn =
article referred to above, I came up with the following (untested, =
public domain, completely warranty-less) Java equivalent of BOOL =
openClipboard(HWND):
=20
import org.jawin.*;
import org.jawin.io.*;
//...
//declare classes/functions/fields/etc.
//...
FuncPtr openClipboard =3D null;
try{
openClipboard =3D new FuncPtr("USER32.DLL", "openClipboard");
//now the challenge is to invoke this since (presumably) the predefined =
invoke_I method won't work on a function returning BOOL

//You could try=20
boolean b =3D (boolean) openClipboard.invoke_I(ReturnFlags.CHECK_FALSE);

//But if that doesn't work because we're returning a BOOL, you have to =
use the generic invoke, as in:
  NakedByteStream nbs =3D new NakedByteStream();
  LittleEndianOutputStream leos =3D new LittleEndianOutputStream(nbs);
 =20
  leos.writeInt(0);
  boolean b =3D  openClipboard.invoke("I:I:", 4, nbs, null, =
ReturnFlags.CHECK_FALSE);
  //whew!
  //...
}
catch(COMException ce)
{
ce.printStackTrace();}
if(openClipboard=3D=3Dnull)
//do something because we couldn't get a valid function pointer
//...
=20
Now, what I fail to understand is why you have to do this in the first =
place. You're calling this from Java, which already has a =
fully-integrated Clipboard interface using "pure" Java. If your ultimate =
goal is to put a string (or data) on the clipboard, why don't you just =
get this string (or data) into your Java program, perhaps using Jawin to =
marshall it from PowerPoint or some other source into Java, as a String, =
then use the AWT clipboard?=20
=20
I am assuming here, that since you intend to ultimately call the =
setClipboard method using Jawin from Java, that you have somehow =
obtained the data you intend to write inside your Java program. Clearly, =
you should use the AWT clipboard, unless somehow you are restricted to a =
JRE that doesn't support this (such as an old version of libgcj). =
Calling into native APIs is bug-prone if you aren't very careful, much =
harder to do, and on top of that, it's slow through Jawin due to the =
high level of abstraction needed to permit calling native APIs without =
writing native code to talk to JNI. If the built-in Clipboard class =
doesn't satisfy you and you intend to use this in a high-performance =
application, I would suggest investigating the use of JNI directly with =
some native code you write. Alternatively, you can set up a very trivial =
interface of Jawin and a COM program or dll that you write in Visual =
Basic or C++, which will help the overhead Jawin suffers (although =
create more overhead with the external COM class.) This would be =
preferable if you have a large amount of COM code to write on the native =
side, which can be executed at once without getting input from your Java =
program.
=20
The AWT clipboard --does-- provide System-level clipboard access, =
although I think the default is that it uses an emulated "clipboard" =
whose scope is just within the current JVM. =
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/datatransfer/Clipboard.h=
tml

________________________________

From: Discussion of Java/Win32/COM integration with Jawin on behalf of =
Alex Chew
Sent: Fri 10/27/2006 2:16 AM
To: [email protected]
Subject: Re: [JAWIN] how can i use clipboard with Jawin?



Thanks for your kind explaination.

I am now  can  use PPT API under jawin as free as i want.
But, how can i call some SYSTEM method with no owner?
just like openClipboard(),setClipboardData() and closeClipboard()?

regards

//vc++ code
//start set clipboard content
Cstring source;
if(openClipboard())
{


HGLOBAL clipbuffer;
char * buffer;
EmptyClipboard();
clipbuffer =3D GlobalAlloc(GMEM_DDESHARE, dource.GetLength()+1);
buffer =3D (char*)GlobalLock(clipbuffer);
Strcpy(buffer, LPCSTR(source));
GlobalUnlock(clipbuffer);
SetClipboardData(CF_TEXT,clipbuffer);
CloseClipboard();


}

//end of set content to clipboard