Friday, 15 January 2010

delphi - Save text from the clipboard to a file -



delphi - Save text from the clipboard to a file -

i trying out below code should save clipboard text text file in delphi xe6. code runs fine generates junk values in output file, when clipboard contains copied text fragment. how can code changed work properly?

function saveclipboardtextdatatofile( sfileto : string ) : boolean; var ps1, ps2 : pchar; dwlen : dword; tf : textfile; hdata : thandle; begin result := false; clipboard begin seek open; if( hasformat( cf_text ) ) begin hdata := getclipboarddata( cf_text ); ps1 := globallock( hdata ); dwlen := globalsize( hdata ); ps2 := stralloc( 1 + dwlen ); strlcopy( ps2, ps1, dwlen ); globalunlock( hdata ); assignfile( tf, sfileto ); rewrite( tf ); write( tf, ps2 ); closefile( tf ); strdispose( ps2 ); result := true; end; close; end; end; end;

you see junk because cf_text ansi. request ansi text, os converts clipboard contents ansi, , set in unicode string. utilize cf_unicodetext unicode applications.

also consider points raised in comments question.

delphi delphi-xe6

No comments:

Post a Comment