Winamp basic controls

A quick tutorial about controling winamp from c# using DllImport's.

This is a little tutorial on using the DllIport to allow your app to interact with winamp.

You can download the Winamp 5 SDK here: http://forums.winamp.com

The information is all for c++ and is in headers but you can use this info to help develop more of the advanced controls.

The interaction with winamp is done using a function called SendMessage in c++ there are premade libarys that will have this all done for you. In c# however theres no inbuilt support for it so after a little digging around and some google researching I found the solution lies in the User32.dll.

To be able to use the SendMessage function in the User32.dll we have to use DllImport:

[DllImport("user32.dll", CharSet=CharSet.Auto)]

public static extern IntPtr FindWindow(

[MarshalAs(UnmanagedType.LPTStr)] string lpClassName, 

[MarshalAs(UnmanagedType.LPTStr)] string lpWindowName

);


[DllImport("user32.dll", CharSet=CharSet.Auto)]

static extern int SendMessageA(

IntPtr hwnd, 

int wMsg, 

int wParam, 

uint lParam

);

FindWindow is used to get the Window Handler, this will be put into an unsafe pointer and provides the target (winamp) for us to aim our messages at.

SendMessageA is the function that will send the commands to winamp.

The window name for winamp will always be a certain value so we define this as our first constant:

private const string m_windowName = "Winamp v1.x";

To tell winamp that we are sending it a command it needs a code for the type of command being sent so our next constant is:

const int WM_COMMAND = 0x111;

(Note the more advanced command use two other command types but these are both beyond the scope of this tutorial)

Next we need a list of all the command codes that we can retrive from the winamp SDK, for this example we just want the basic ones so we put these in as a list of constants too:

const int WA_NOTHING = 0;

const int WA_PREVTRACK = 40044;

const int WA_PLAY = 40045;

const int WA_PAUSE = 40046;

const int WA_STOP = 40047; 

const int WA_NEXTTRACK = 40048;

const int WA_VOLUMEUP = 40058; 

const int WA_VOLUMEDOWN = 40059;

const int WINAMP_FFWD5S = 40060;

const int WINAMP_REW5S = 40061;

I’ve put pretty self explanetry names on all these commands

And finally we need to bring it all together in some functions that we can call to get the required results:

public void Stop()

{

IntPtr hwnd = FindWindow(m_windowName, null); 

SendMessageA(hwnd, WM_COMMAND, WA_STOP, WA_NOTHING);

}



public void Play() 

{ 

IntPtr hwnd = FindWindow(m_windowName, null); 

SendMessageA(hwnd, WM_COMMAND, WA_PLAY, WA_NOTHING);

}



public void Pause()

{

IntPtr hwnd = FindWindow(m_windowName, null); 

SendMessageA(hwnd, WM_COMMAND, WA_PAUSE, WA_NOTHING);

}



public void PrevTrack()

{

IntPtr hwnd = FindWindow(m_windowName, null); 

SendMessageA(hwnd, WM_COMMAND, WA_PREVTRACK, WA_NOTHING);

}



public void NextTrack()

{

IntPtr hwnd = FindWindow(m_windowName, null); 

SendMessageA(hwnd, WM_COMMAND, WA_NEXTTRACK, WA_NOTHING);

}



public void VolumeUp()

{

IntPtr hwnd = FindWindow(m_windowName, null); 

SendMessageA(hwnd, WM_COMMAND, WA_VOLUMEUP, WA_NOTHING);

}



public void VolumeDown()

{

IntPtr hwnd = FindWindow(m_windowName, null); 

SendMessageA(hwnd, WM_COMMAND, WA_VOLUMEDOWN, WA_NOTHING);

}



public void Forward5Sec()

{

IntPtr hwnd = FindWindow(m_windowName, null); 

SendMessageA(hwnd, WM_COMMAND, WINAMP_FFWD5S, WA_NOTHING);

}



public void Rewind5Sec()

{

IntPtr hwnd = FindWindow(m_windowName, null); 

SendMessageA(hwnd, WM_COMMAND, WINAMP_REW5S, WA_NOTHING);

}

As you can see all these commands are used in the same format, you use Findwindow to get the WIndow handler for winamp, the you feed that in along with the command type WM_COMMAND(0x111), the command you want WA_STOP(40047) and finally just send it the WM_NOTHING(0) since for this type of interaction this perameter isn’t needed.

From here you can reference the rest of the SDK and do a little research yourself and you’ll soon have a wide range of commands and be able to do some pretty funky stuff!!

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top