xdeletex has asked for the wisdom of the Perl Monks concerning the following question:

I need some of you guru's help i'm working on apart of a script that just will not work. It doesn't spit out errors or even data. I'm not really sure what else to do. Take a look and see if you can point out any problems. It use's the Win32::api module.
#!/usr/bin/perl -w use strict; use diagnostics; use Win32::API; my($format,$data,$open,$close,$count,$name,$window,$pointer,$test, $te +st2,$test3); $open = new Win32::API('user32', 'OpenClipboard', 'L', 'I'); if(not defined $open) { die "Can't import API OpenClipboard: $!\n"; } $open->Call(0); $format = new Win32::API('user32', 'EnumClipboardFormats','I','I'); if(not defined $format) { die "Can't import API EnumClipboardFormats: $!\n" } $format->Call(0); $count = new Win32::API('user32', 'CountClipboardFormats','','V'); if(not defined $count) { die "Can't import API CountClipboardFormats: $!\n" } $count->Call(); $name = new Win32::API('user32', 'GetClipboardFormatName','ICI','I'); if(not defined $name) { die "Can't import API GetClipboardFormatName: $!\n" } $pointer= " " x 24; $test = 0; $name->Call($test, $pointer, 24); $close = new Win32::API('user32', 'CloseClipboard', '', 'V'); if(not defined $close) { die "Can't import API CloseClipboard: $!\n" } $close->Call();

Replies are listed 'Best First'.
Re: Windows Clipboard
by BrowserUk (Patriarch) on Aug 28, 2004 at 10:30 UTC

    The first question is: What output do you expect to see from your program when there are no print statements?

    Using the raw version of Win32::API is harder work than necessary. Win32::API::Prototype makes life a little easier (though it still has problems).

    Finally, the clipboard api set is very badly designed.

    Many things about it are very strange. The first is that most of the clipboard formats you will encounter are so-called "pre-defined formats". These do not have a name, just an ID. Attempting to get the name for these formats using GetClipboardName() returns and error. This is documented (kind of).

    The CountClipboardFormats() api doesn't tell you how many formats are known to the system. It tells you how many formats the current contents can be converted to.

    The EnumClipboardFormats() enumerates the clipboard format IDs that the current contents can be rendered into, though the results I'm getting are...um...strange. This could be my code in error, or my misunderstanding of the api, or an error in the api. I'm not sure which.

    Here is a little code for you to play with. Try cutting a few different types of things (some text, a bitmap, some files or directories in the Explorer, an icon) and then run the code.

    #! perl -slw use strict; use Win32::API::Prototype; { ## Read the apis from DATA and import them. local $/ = ''; ## Para mode while( <DATA> ) { my( $dll, $proto, $api ) = m[(\w+)\n(\w+\s+(\w+)\(.*)$]s; # print "$dll:[$api]\n'$proto'"; ApiLink( $dll, $proto ) or die "$api : $^E"; } close DATA; } OpenClipboard( 0 ); my $fmts = CountClipboardFormats( 0 ); print "Formats currently available on clipboard: $fmts"; my $formatID = 0; ## To get the first for( 0 .. $fmts ) { my $formatID = EnumClipboardFormats( $formatID ) or die $^E; last unless $formatID; my $fmtName = ' ' x 254; my $length = 254; GetClipboardFormatName( $formatID, $fmtName, $length ) or warn "Format $formatID is pre-defined and does not have an +name.\n" and next; print "Format $formatID is called: $fmtName"; } __DATA__ user32 BOOL OpenClipboard( HWND hWndNewOwner ) user32 int CountClipboardFormats( VOID ) user32 UINT EnumClipboardFormats( UINT format ) user32 int GetClipboardFormatName( UINT format, LPTSTR lpszFormatName, int cchMaxCount ) user32 BOOL CloseClipboard( VOID )

    All in all. Using Win32::Clipboard is probably easier for the simple case.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: Windows Clipboard
by Mr. Muskrat (Canon) on Aug 28, 2004 at 03:50 UTC
Re: Windows Clipboard
by qumsieh (Scribe) on Aug 28, 2004 at 06:04 UTC
    Your question is too vague. What do you mean by "will not work"? Please tell us what you expect it to do, and what it ends up doing.

    That being said, the code you show doesn't do much. It opens the clipboard, attempts to get a format name, and closes the clipboard. You don't attempt to print anything or retrieve any information. Personally, when working with Win32::API, I prefer to print the return value of my Call() invocations, and compare it with the expected return value. This way I know where the program is failing. In your specific code, your call to GetClipboardFormatName returns 0 which means that it either failed, or the format is predefined. I suspect the latter.

    One more thing, the second argument to GetClipboardFormatName is a pointer to a string. You specify C in your Win32::API::new. I would think that P is better.

Re: Windows Clipboard
by xdeletex (Novice) on Aug 28, 2004 at 15:38 UTC
    thanks for all the help guys. i originally had print statements in the snippet, but i took them out cause some would print nothing and others just 0. So i was searching for information in how to tretriee the data i was looking for such as packing it and then printing it or something.