text - C++ (VC): How to detect whether Windows clipboard contains CF_TEXT data? -


i'm complete beginner @ c++, i've managed modify other people's code , put program writes windows clipboard text file, text encoded in codepage specified command line parameter program (for example, myprog.exe 437) write text in codepage 437. if no codepage specified, program writes text file in standard windows codepage 1252.

the trouble program crashes if clipboard contains (for example) shortcut or file, not text. can tell me how can exit gracefully if there no cf_text data in clipboard? (or have misunderstood problem?)

i've searched answer no success. here vc2010 code (which don't entirely understand, seems work, when clipboard contains cf_text; doesn't work cf_unicodetext, way - output few bytes.):

#include <stdafx.h> #include <windows.h> #include <iostream> #include <fstream> #include <codecvt> // wstring_convert #include <locale>  // codecvt_byname  using namespace std;  void bailout(char *msg) { fprintf(stderr, "exiting: %s\n", msg); exit(1); }  int main(int argc, char* argv[]) { std::string codepage = ".1252"; if (argc > 1) {     std::string cpnum = argv[1];     codepage = "."+cpnum;    }  handle clip; string clip_text = ""; // exit if clipboard not available if (!openclipboard(null))         bailout("can't open clipboard");   clip = getclipboarddata(cf_text); clip_text = (char*)clip; closeclipboard();  // create conversion routines typedef std::codecvt_byname<wchar_t,char,std::mbstate_t> codecvt; std::wstring_convert<codecvt> cp1252(new codecvt(".1252")); std::wstring_convert<codecvt> outpage(new codecvt(codepage));  std::string outfile = "#clip.txt"; // output file name ofstream outstream;  // open output stream outstream.open(outfile, ios::out | ios::trunc);  // make sure file opened if(!outstream) {     cout << "error opening file " << outfile << " writing.\n";     return 1; } // convert dos/win codepage number in "outpage" outstream << outpage.to_bytes(cp1252.from_bytes(clip_text)).c_str(); outstream << endl; outstream.close(); // close output stream return 0; }  

i'll grateful suggestions on how fix last problem.

before data clipboard using getclipboarddata function, have check clipboard contains data in format expect. can done using isclipboardformatavailable function follows:

if(isclipboardformatavailable(cf_text)) {     // clipboard contains null-terminated ansi string. } else if (isclipboardformatavailable(cf_unicodetext)) {     // clipboard contains null-terminated unicode string. } 

then have open clipboard , obtain handle clipboard buffer follows (assuming clipboard contains ansi string):

if(!openclipboard(null)) return; hglobal hglb = getclipboarddata(cf_text); 

then have obtain exclusive access data using globallock function . on success can safely access data

if (hglb != null) {     lptstr lptstr = globallock(hglb);      if (lptstr != null)      {         // read contents of lptstr pointer string.          // don't forget release lock after done.         globalunlock(hglb);     } } closeclipboard();  

the return type of globallock void pointer. depending on data format have determined using isclipboardformatavailable, have cast corresponding type. windows data types documented here.


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -