Guest User

Untitled

a guest
Feb 20th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Runtime.InteropServices;
  9.  
  10. namespace winampsongDB {
  11. public partial class Form1 : Form {
  12. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  13. private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
  14.  
  15. [DllImport("user32.dll", SetLastError = true)]
  16. static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  17.  
  18. [DllImport("user32.dll", SetLastError = true)]
  19. static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
  20.  
  21. [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  22. static extern uint RegisterWindowMessage(string lpString);
  23.  
  24. [DllImport("kernel32.dll")]
  25. public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, uint dwProcessId);
  26.  
  27. [DllImport("kernel32.dll", SetLastError = true)]
  28. [return: MarshalAs(UnmanagedType.Bool)]
  29. static extern bool CloseHandle(IntPtr hObject);
  30.  
  31. [DllImport("kernel32.dll", SetLastError = true)]
  32. static extern bool ReadProcessMemory(
  33. IntPtr hProcess,
  34. IntPtr lpBaseAddress,
  35. [Out] byte[] lpBuffer,
  36. int dwSize,
  37. out int lpNumberOfBytesRead
  38. );
  39.  
  40. private static string ReadWinampString(IntPtr hwnd, IntPtr location) {
  41. uint pid;
  42. IntPtr process;
  43.  
  44. GetWindowThreadProcessId(hwnd, out pid);
  45. if (pid == 0)
  46. return string.Empty;
  47.  
  48. if ((process = OpenProcess(0x0008 | 0x0010, false, pid)) == IntPtr.Zero)
  49. return string.Empty;
  50.  
  51. int bytesRead;
  52. byte[] buffer = new byte[1024];
  53.  
  54. try {
  55. if (!ReadProcessMemory(process, location, buffer, buffer.Length, out bytesRead))
  56. return string.Empty;
  57. } finally {
  58. CloseHandle(process);
  59. }
  60.  
  61. string str = Encoding.GetEncoding(1252 /* windows-1252 - windows extended ASCII */).GetString(buffer);
  62. return str.Contains("\0") ? str.Substring(0,str.IndexOf('\0')) : str;
  63. }
  64.  
  65. uint WinampSongChangeMsg = 0xFFFFFFF;
  66.  
  67. string title, file;
  68.  
  69. public Form1() {
  70. WinampSongChangeMsg = RegisterWindowMessage("WinampSongChange");
  71.  
  72. InitializeComponent();
  73. }
  74.  
  75. public void UpdateSong() {
  76. IntPtr hwnd;
  77. if ((hwnd = GetWinampHandle()) == null)
  78. return;
  79.  
  80. int plPos = (int)SendMessage(hwnd, 0x0400 /* WM_USER/WM_WA_IPC */, 0, 125 /*IPC_GETLISTPOS*/);
  81.  
  82. IntPtr strPtr = SendMessage(hwnd, 0x0400 /* WM_USER/WM_WA_IPC */, plPos, 211 /*IPC_GETPLAYLISTFILE*/);
  83. file = ReadWinampString(hwnd, strPtr);
  84.  
  85. strPtr = SendMessage(hwnd, 0x0400 /* WM_USER/WM_WA_IPC */, plPos, 212 /*IPC_GETPLAYLISTTITLE*/);
  86. title = ReadWinampString(hwnd, strPtr);
  87.  
  88. songTitle.Text = title;
  89. UpdateDisp();
  90. }
  91.  
  92. private static IntPtr GetWinampHandle() {
  93. return FindWindow("Winamp v1.x", null);
  94. }
  95.  
  96. protected override void WndProc(ref Message m) {
  97. if (m.Msg == WinampSongChangeMsg) // songchanged
  98. UpdateSong();
  99.  
  100. base.WndProc(ref m);
  101. }
  102. }
  103. }
Add Comment
Please, Sign In to add comment