Tech Tips
edited by George Frazier

Listing One

#!/usr/local/bin/python
import time
from win32clipboard import *

id = 0
clipboard_file = r'\\home\Clipboard\clipboard.txt'
prev_data = ''

while (True):
   time.sleep(1)
   if OpenClipboard() != None:
      print 'OpenClipboard() failed'
      continue
   try:
      data = prev_data
      if    IsClipboardFormatAvailable(CF_TEXT):
         data = GetClipboardData()
      if data != prev_data:
         open(clipboard_file, 'w').write(data)
         print 'writing %s to file' % data
         prev_data = data
      else:
         data = open(clipboard_file, 'r').read()
         if data != prev_data:
            EmptyClipboard()
            SetClipboardData(CF_TEXT, data)
            print 'putting %s in clipboard' % data
            prev_data = data

      CloseClipboard()
   except:
      pass


Listing Two
#include <fstream>
#include <iostream>
#include <string>

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                     LPTSTR lpCmdLine, int nCmdShow)
{
   const char * clipboard_file = "\\\\home\\Clipboard\\clipboard.txt";
   std::string prev_data;
   std::string data;
   while (true)
   {
      ::Sleep(1000);
      if (::OpenClipboard(0) == FALSE)
         continue;
      data = prev_data;
      if (::IsClipboardFormatAvailable(CF_TEXT) != FALSE)
      {
         HANDLE h = ::GetClipboardData(CF_TEXT);
         if (h)
         {
            const char * buf = (const char *)::GlobalLock(h);
            data = buf;
            if (!data.empty())
               ::GlobalUnlock(h);
         }
      }
      if (data != prev_data)
      {
         std::ofstream f(clipboard_file);
         if (!f.is_open())
         {
            ::CloseClipboard();
            continue;
         }
         f << data;
         f.close();
         prev_data = data;
      }
      else
      {
         std::ifstream f(clipboard_file);
         if (!f.is_open() || f.eof())
         {
            ::CloseClipboard();
            continue;
         }
         data = "";
         char c;
         f.get(c);
         while(!f.eof())
         {
            data += c;
            f.get(c);
         }
         f.close();
         if (data != prev_data)
         {
            BOOL rc = ::EmptyClipboard();
            if (rc == FALSE)
            {
               ::CloseClipboard();
               continue;
            }
            HGLOBAL hMem = ::GlobalAlloc(GMEM_MOVEABLE, data.length()+1);
            void * buf = ::GlobalLock(hMem);
            ::memcpy(buf, (const void *)data.c_str(), data.length()+1);
            ::GlobalUnlock(hMem);
            HANDLE h = ::SetClipboardData(CF_TEXT, hMem);
            if (h == NULL)
            {
               ::CloseClipboard();
               continue;
            }
            prev_data = data;
         }
      }
      ::CloseClipboard();
   }
}


Listing Three

(a)
using System::String;
using System::Runtime::InteropServices::Marshal;
String  *s = ... // get a string from somewhere
char  *ansi = (char*)(Marshal::StringToHGlobalAnsi(s).ToPointer());
puts(ansi);
Marshal::FreeHGlobal(ansi);

(b)
using dotnetstl::c_string_accessor;
String  *s = ... // get a string from somewhere
puts(c_string_accessor<char>(s));


Listing Four


template <typename C>
class c_string_accessor
{
public:
  typedef C                     char_type;
  typedef C                     *pointer;
  typedef C const               *const_pointer;
  typedef c_string_accessor<C>  class_type;

// Construction
public:
  /// 
  explicit c_string_accessor(System::String *s)
    : m_s(_get_cstring(s))
  {}

  ~c_string_accessor()
  {
    System::Runtime::InteropServices::Marshal::FreeHGlobal(m_s);
  }

// Implementation
public:
  operator const_pointer() const
  {
    return m_s;
  }

// Implementation
private:
  pointer _get_cstring(System::String *s);

// Members
private:
  pointer m_s;

// Not to be implemented
private:
  c_string_accessor(class_type const &);
  c_string_accessor &operator =(class_type const &);
};

// Specialization for char
template <>
inline c_string_accessor<char>::pointer
    c_string_accessor<char>::_get_cstring(System::String *s)
{
  return reinterpret_cast<char*>(System::Runtime::
      InteropServices::Marshal::StringToHGlobalAnsi(s).ToPointer());
}

// Specialization for wchar_t
template <>
inline c_string_accessor<wchar_t>::pointer
    c_string_accessor<wchar_t>::_get_cstring(System::String *s)
{
  return reinterpret_cast<wchar_t*>(System::Runtime::
      InteropServices::Marshal::StringToHGlobalUni(s).ToPointer());
}





4



