Repository: kivy/kivy

Issue:
CTRL-C in a textinput crashes the app
<!--
The issue tracker is a tool to address bugs.
Please use the #kivy IRC channel on freenode or Stack Overflow for
support questions, more information at https://git.io/vM1yQ.

Before opening a new issue, make sure you do the following:
    * check that your issue isn't already filed: https://git.io/vM1iE
    * prepare a short, runnable example that reproduces the issue
    * reproduce the problem with the latest development version of Kivy
    * double-check that the issue is indeed a bug and not a support request
-->

### Versions

* Python: 3.6.3
* OS: Windows 10
* Kivy: 1.10.0
* Kivy installation method: using pip, followed the steps shown in the guide

### Description

Trying to use CTRL-C to copy text from a textinput, and the program crashes.

### Code and Logs

```python
import kivy
from kivy.uix.textinput import TextInput
from kivy.app import App

class program(App):
    def build(self):
        return TextInput()

prog = program()
prog.run()


Traceback (most recent call last):
   File "E:/pycharm/MethasiaChatSurelyUnstable/vladegay.py", line 12, in <module>
     prog.run()
   File "C:\Users\hellNo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\app.py", line 828, in run
     runTouchApp()
   File "C:\Users\hellNo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\base.py", line 504, in runTouchApp
     EventLoop.window.mainloop()
   File "C:\Users\hellNo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\core\window\window_sdl2.py", line 663, in mainloop
     self._mainloop()
   File "C:\Users\hellNo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\core\window\window_sdl2.py", line 602, in _mainloop
     self.modifiers):
   File "kivy\_event.pyx", line 714, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8146)
   File "kivy\_event.pyx", line 1225, in kivy._event.EventObservers.dispatch (kivy\_event.c:14035)
   File "kivy\_event.pyx", line 1149, in kivy._event.EventObservers._dispatch (kivy\_event.c:13564)
   File "C:\Users\hellNo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\core\window\__init__.py", line 159, in _on_window_key_down
     return self.dispatch('on_key_down', keycode, text, modifiers)
   File "kivy\_event.pyx", line 714, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8146)
   File "kivy\_event.pyx", line 1225, in kivy._event.EventObservers.dispatch (kivy\_event.c:14035)
   File "kivy\_event.pyx", line 1149, in kivy._event.EventObservers._dispatch (kivy\_event.c:13564)
   File "C:\Users\hellNo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\uix\textinput.py", line 2404, in keyboard_on_key_down
     self.copy()
   File "C:\Users\hellNo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\uix\textinput.py", line 1712, in copy
     return Clipboard.copy(self.selection_text)
   File "C:\Users\hellNo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\core\clipboard\__init__.py", line 73, in copy
     self._copy(data)
   File "C:\Users\hellNo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\core\clipboard\__init__.py", line 87, in _copy
     self.put(data, self._clip_mime_type)
   File "C:\Users\hellNo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\core\clipboard\clipboard_winctypes.py", line 55, in put
     msvcrt.wcscpy_s(c_wchar_p(hCd), len(text), c_wchar_p(text))
 ValueError: embedded null character

```



Discussion:
👋 Thanks for opening your first issue here! Be sure to follow the issue template!

Most likely related to our ctypes clipboard error on Py 3.6 Appveyor tests.
I am running into this issue as well - Any timeframe on a fix? Users cannot copy text using either ctrl+c or by using the bubble.

Resolving patch:
diff --git a/kivy/core/clipboard/clipboard_winctypes.py b/kivy/core/clipboard/clipboard_winctypes.py
--- a/kivy/core/clipboard/clipboard_winctypes.py
+++ b/kivy/core/clipboard/clipboard_winctypes.py
@@ -56,7 +56,9 @@ def put(self, text, mimetype='text/plain'):
         user32.OpenClipboard(user32.GetActiveWindow())
         user32.EmptyClipboard()
         hCd = GlobalAlloc(0, len(text) * ctypes.sizeof(ctypes.c_wchar))
-        msvcrt.wcscpy_s(c_wchar_p(hCd), len(text), c_wchar_p(text))
+
+        # ignore null character for strSource pointer
+        msvcrt.wcscpy_s(c_wchar_p(hCd), len(text), c_wchar_p(text[:-1]))
         SetClipboardData(CF_UNICODETEXT, hCd)
         user32.CloseClipboard()
 


Test patch: