I am trying to read the WM_COPYDATA message with Python, some applications (I'm trying with Spotify) to send to WindowsLiveMessenger to update the phrase "What I'm listening to ...".
From what I was able to find, COPYDATASTRUCT messages are included in COPYDATASTRUCT with the following structure:
dwData in our case 0x547 to access the listening function nowcbData with accepted string lengthlpData with a pointer to the actual string, may include Unicode characters
The string should have the following format: \0Music\0status\0format\0song\0artist\0album\0 , as indicated by ListeningNowTracker
What we get in the WM_COPYDATA event is a pointer to lParam that contains COPYDATASTRUCT .
I started messing around with pywin32 functions, and I remembered that they did not accept Unicode characters from past experience, then I switched to ctypes. Despite the fact that for me it was almost a new world for Python, I tried using POINTER() , and all that I had was an object unknown to me or access to violations.
I think the code should create COPYDATASTRUCT :
class CopyDataStruct(Structure): _fields_ = [('dwData', c_int), ('cbData', c_int), ('lpData', c_void_p)]
Then make lParam pointer to this structure, get the line pointer from lpData and finally get the line with ctypes.string_at(lpData,cbData) .
Any tips?
UPDATE 1
The WM_COPYDATA event was received by a hidden window built with win32gui just for this purpose. The copydata event is associated with a function called OnCopyData , and this is its title:
def OnCopyData(self, hwnd, msg, wparam, lparam):
The values ββthat the function executes are correct compared to the values ββfrom the Spy ++ message log.
UPDATE 2
This should be close to what I want, but gives a NULL pointer error.
class CopyDataStruct(ctypes.Structure): _fields_ = [('dwData', c_int), ('cbData', c_int), ('lpData', c_wchar_p)] PCOPYDATASTRUCT = ctypes.POINTER(CopyDataStruct) pCDS = ctypes.cast(lparam, PCOPYDATASTRUCT) print ctypes.wstring_at(pCDS.contents.lpData)