Old 14th February 2010, 17:34   #1
Junior Member
Join Date: Feb 2010
Posts: 2
SendMessage Enque Song in Playlist


I'm trying to write a program in C# to do some basic Winamp functions (a functional SDK port instead of that which seems to be availalble for .net at SourceForge).

The really basic functions work (play, pause, stop, etc.) but I'm having some trouble with Enqueuing a song / sending a string.

The command arrives in Winamp, and winamp adds something to the playlist - unfortunatly not the string i wanted. It adds a bunch of garbage, and the garbage is always the same no matter what string i send (So I dont think it has to do with encoding)...

Following code:

public static extern int SendMessageA(int hwnd, int wMsg, int wParam, COPYDATASTRUCT lParam);

public struct COPYDATASTRUCT
public long dwData;
public long cbData;
public long lpData;

IntPtr temp;

cds.dwData = (long)IPC_PLAYFILE;
cds.lpData = (long)Marshal.StringToHGlobalUni(strURL);
cds.cbData = (long)strURL.Length + 1;

temp = Marshal.AllocHGlobal(Marshal.SizeOf(cds));
Marshal.StructureToPtr(cds, temp, false);

SendMessage(hwnd, WM_COPY_DATA, 0, (uint)temp);

Any suggestions?

Thanks in advance,

corey.hannum is offline   Reply With Quote
Old 15th February 2010, 23:09   #2
Join Date: Sep 2003
Posts: 27,873
not knowing all of the C# syntax, i'd suggest making sure that it's a pointer to the string you're passing and not the address of the buffer holding it. and also that it is an ansi encoded string (or change IPC_PLAYFILE to the unicode IPC_PLAYFILEW variant if it's a unicode string you're working with). other than that not too much else i can think off with my limited knowledge with C# (though how you've declared SendMessageA(..) but used SendMessage(..) is a bit baffling).

DrO is offline   Reply With Quote
Old 17th February 2010, 15:00   #3
Junior Member
Join Date: Feb 2010
Posts: 2
I copied the code together from a few places in my source - thats why you see SendMessageA - copied from the wrong place.

Manged Code seems to make working with pointers much more complicated than it used to be. I'm going to look into whether the pointer is pointing to the string or buffer..

Anyone eles have any other suggestions??
corey.hannum is offline   Reply With Quote
Old 7th September 2020, 12:46   #4
Junior Member
Join Date: Aug 2020
Posts: 6
Seems like this code works


public class copy_data_4
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

public struct COPYDATASTRUCT
public IntPtr dwData;
public int cbData;
public IntPtr lpData;

public static uint WM_COPYDATA = 0x004A;

public static void send_string(string str_path, long l_comand, IntPtr i_hWnd)
IntPtr ptrCopyData = IntPtr.Zero;

// Create the data structure and fill with data
copyData.dwData = new IntPtr(l_comand); // Just a number to identify the data type
copyData.cbData = str_path.Length + 1; // One extra byte for the \0 character
copyData.lpData = Marshal.StringToHGlobalAnsi(str_path);

// Allocate memory for the data and copy
ptrCopyData = Marshal.AllocCoTaskMem(Marshal.SizeOf(copyData));
Marshal.StructureToPtr(copyData, ptrCopyData, false);

// Send the message
SendMessage(i_hWnd, WM_COPYDATA, IntPtr.Zero, ptrCopyData);



string str_path = "C:/my.mp3";

copy_data_4.send_string(str_path, (long)IPC_PLAYFILE, hWnd_WinAmp);

aleksey_WinAmp is offline   Reply With Quote
Old 26th January 2021, 20:36   #5
Junior Member
Join Date: Jan 2002
Posts: 36
Very cool.
meltor is offline   Reply With Quote
Old 18th February 2021, 02:40   #6
Forum King
thinktink's Avatar
Join Date: May 2009
Location: No longer on the streets of Kings County, CA.
Posts: 3,219
Just so you know, I am not familiar with C#, just C++ so what I have to point out might be inapplicable.


I noticed the provided function doesn't appear to explicitly free the allocated memory for storing the struct information passed to Winamp in the SendMessage and the memory allocated by "Marshal.AllocCoTaskMem" which MSDN says must be deallocated with "Marshal.FreeCoTaskMem(IntPtr)". From my understanding of Winamp API, both can be safely deallocated/freed after the call to SendMessage returns.
thinktink is offline   Reply With Quote
Go Back   Winamp & Shoutcast Forums > Developer Center > Winamp Development

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump