Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- namespace winampsongDB {
- public partial class Form1 : Form {
- [DllImport("user32.dll", CharSet = CharSet.Auto)]
- private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
- [DllImport("user32.dll", SetLastError = true)]
- static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
- [DllImport("user32.dll", SetLastError = true)]
- static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
- [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
- static extern uint RegisterWindowMessage(string lpString);
- [DllImport("kernel32.dll")]
- public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, uint dwProcessId);
- [DllImport("kernel32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- static extern bool CloseHandle(IntPtr hObject);
- [DllImport("kernel32.dll", SetLastError = true)]
- static extern bool ReadProcessMemory(
- IntPtr hProcess,
- IntPtr lpBaseAddress,
- [Out] byte[] lpBuffer,
- int dwSize,
- out int lpNumberOfBytesRead
- );
- private static string ReadWinampString(IntPtr hwnd, IntPtr location) {
- uint pid;
- IntPtr process;
- GetWindowThreadProcessId(hwnd, out pid);
- if (pid == 0)
- return string.Empty;
- if ((process = OpenProcess(0x0008 | 0x0010, false, pid)) == IntPtr.Zero)
- return string.Empty;
- int bytesRead;
- byte[] buffer = new byte[1024];
- try {
- if (!ReadProcessMemory(process, location, buffer, buffer.Length, out bytesRead))
- return string.Empty;
- } finally {
- CloseHandle(process);
- }
- string str = Encoding.GetEncoding(1252 /* windows-1252 - windows extended ASCII */).GetString(buffer);
- return str.Contains("\0") ? str.Substring(0,str.IndexOf('\0')) : str;
- }
- uint WinampSongChangeMsg = 0xFFFFFFF;
- string title, file;
- public Form1() {
- WinampSongChangeMsg = RegisterWindowMessage("WinampSongChange");
- InitializeComponent();
- }
- public void UpdateSong() {
- IntPtr hwnd;
- if ((hwnd = GetWinampHandle()) == null)
- return;
- int plPos = (int)SendMessage(hwnd, 0x0400 /* WM_USER/WM_WA_IPC */, 0, 125 /*IPC_GETLISTPOS*/);
- IntPtr strPtr = SendMessage(hwnd, 0x0400 /* WM_USER/WM_WA_IPC */, plPos, 211 /*IPC_GETPLAYLISTFILE*/);
- file = ReadWinampString(hwnd, strPtr);
- strPtr = SendMessage(hwnd, 0x0400 /* WM_USER/WM_WA_IPC */, plPos, 212 /*IPC_GETPLAYLISTTITLE*/);
- title = ReadWinampString(hwnd, strPtr);
- songTitle.Text = title;
- UpdateDisp();
- }
- private static IntPtr GetWinampHandle() {
- return FindWindow("Winamp v1.x", null);
- }
- protected override void WndProc(ref Message m) {
- if (m.Msg == WinampSongChangeMsg) // songchanged
- UpdateSong();
- base.WndProc(ref m);
- }
- }
- }
Add Comment
Please, Sign In to add comment