Im running a few tests for creating an MP3 player (very basic) and i'm having a few issues that I'm hoping somebody can help with.
Right.....

Im using mci for playback but what i need is the file path to be input by the user via a textbox. Ive created 3 strings that are eventually joined to create the full command and filename i.e L"play bad.mp3".
It is hoped that the textbox input will supply Sting^ spcom2 variable, but for testing purposes i've currently predefined String^ spcom2.

String^ spcom1 = "L\" play ";
String^ spcom2 = "bad.mp3";
String^ spcom3 = " \" ";
String^ spcomfull = spcom1+spcom2+spcom3;

My Version of MCI with string input (doesn't work)

mciSendString(spcomfull, NULL, 0, 0);


The problem is that mciSendString wont accept the string input, I know that it'll accept TCHAR
i.e TCHAR cpcomfull[MAX_PATH] = L"play bad.mp3"; The problem I have with this method is converting String^ to TCHAR.

So my questions are:

a) If it is possible to, what is the simplest method of converting a String^ into TCHAR (in its simplest form).

b) As i eventually want to implement a listbox/listview for the user to select and play audio tracks from (like itunes style), should I use a different method to pass filenames/paths to mciSendString?


Also, Im using Windows XP and Microsoft Visual Studio C++ 2010

Do you really think I've not spent hours trawling through books, google and msdn???
Although I quite enjoyed the automation into google...

Your question how to convert String^ to TCHAR is answered in the very first link that I gave you. Don't be so quick to dismiss google.

Right, I get your point... Yes there is a method, the only issue is the conversion of this code from old style syntax.

Heres my updated attempt:-

System::String^ mstr = "L\"play bad.mp3\"";
  TCHAR *ustr;
  #ifdef _UNICODE
ustr = new TCHAR[mstr->get_Length()+1];
 pin_ptr<const wchar_t> umstring = PtrToStringChars(mstr);
   
  #else
  const char* umstring = (const char*)Marshal::StringToHGlobalAnsi(mstr).ToPointer();
  ustr = new TCHAR[strlen(umstring)+1];
  #endif
  _tcscpy
  #ifndef _UNICODE
  Marshal::FreeHGlobal(IntPtr((void*)umstring));
  #endif

  // do something with the unmanaged string
  
  delete [] ustr;

the problem is this line:

ustr = new TCHAR[mstr->get_Length()+1];

which produces this error:-

error C2039: 'get_Length' : is not a member of 'System::String'

And this line:

which produces this error:-

_tcscpy(ustr, umstring);


warning C4996: 'wcscpy': This function or variable may be unsafe. Consider using wcscpy_s instead.


Im not asking for anybody to do the code for me, what i am asking for is an explanation of whats going on inside the code. Then maybe with that explanation I would be able to understand examples and techniques found from google.

System::String^ mstr = "L\"play bad.mp3\"";
  TCHAR *ustr;
  #ifdef _UNICODE
ustr = new TCHAR[19];
 pin_ptr<const wchar_t> umstring = PtrToStringChars(mstr);
   
  #else
  const char* umstring = (const char*)Marshal::StringToHGlobalAnsi(mstr).ToPointer();
  ustr = new TCHAR[strlen(umstring)+1];
  #endif
  #define _CRT_SECURE_NO_WARNINGS
_tcscpy(ustr, umstring);
  #ifndef _UNICODE
  Marshal::FreeHGlobal(IntPtr((void*)umstring));
  #endif

  // do something with the unmanaged string
  
  delete [] ustr;

//Sorry previous code was wrong. Line 11 should of been _tcscpy(ustr, umstring);

sorry the codes above were wrong again, Im blaming it on tiredness :)

System::String^ mstr = "L\"play bad.mp3\"";
  TCHAR *ustr;
  #ifdef _UNICODE
ustr = new TCHAR[mstr->get_Length()+1];
 pin_ptr<const wchar_t> umstring = PtrToStringChars(mstr);
   
  #else
  const char* umstring = (const char*)Marshal::StringToHGlobalAnsi(mstr).ToPointer();
  ustr = new TCHAR[strlen(umstring)+1];
  #endif
_tcscpy(ustr, umstring);
  #ifndef _UNICODE
  Marshal::FreeHGlobal(IntPtr((void*)umstring));
  #endif

  // do something with the unmanaged string
  
  delete [] ustr;

System::String does not have a method called get_Length() -- Look up the methods available to that class and you will see that its a property (int) named String.

Edited 5 Years Ago by Ancient Dragon: n/a

This article has been dead for over six months. Start a new discussion instead.