INTERACT FORUM

Windows => Plug-in Development => Topic started by: buhrmi on July 23, 2004, 02:14:58 am

Title: Plugins with dev-c++
Post by: buhrmi on July 23, 2004, 02:14:58 am
Hi,

I want to get started writing a plug-in in dev-c++. Could somebody provide a minimalistic plugin source to begin with? I am not able to get running since all sample plugins are very very VC++ specific.

Thanks,
buhrmi
Title: Re:Plugins with dev-c++
Post by: Zoner on July 23, 2004, 03:40:45 am
What's dev-c++?  Actually now I found it - it's some sort of GNU C++ variant.  Sorry - can't help you with that one.
Title: Re:Plugins with dev-c++
Post by: buhrmi on July 23, 2004, 03:49:42 am
It's a very popular free C++ IDE for Windows.

http://www.bloodshed.net/devcpp.html
Title: Re:Plugins with dev-c++
Post by: buhrmi on July 25, 2004, 03:41:28 pm
Ok after switching over to VC++ I have a problem now with the plugin I am trying to create (I never did ANY C++ programming before):

I get an error in this line of code
char* sFilename = pFile->GetFilename;
e:\Projekte\h140 playlist exporter\int_iriverexporter\iriverexporterCtrl.cpp(38): error C2440: 'Initialisation: '_bstr_t (__thiscall IMJFileAutomation::* )(void)' cannot be converted to 'char *'

What I am trying to do is to develop a plugin that automaticly detects the iriver and copies the files of a selected playlist to it and generate the .m3u playlist. But I am having problems getting information (Filename) out of MJFileAutomation. Everything else works quite well.

Here is the rest of the code (so you can see the context. maybe its very bad syntax cause i am new to programming and did not do things like this before).

Code: [Select]
pPlaylists = m_pMJ->GetPlaylists();
int i = ::SendMessage(GetDlgItem(IDC_COMBO1), CB_GETCURSEL, 0, 0);
pPlaylist = pPlaylists->GetPlaylist(i);
pFiles = pPlaylist->GetFiles();
int NumberFiles = pFiles->GetNumberFiles();
IMJFileAutomationPtr pFile;
for (i = 0; i < NumberFiles; i++)
{
   pFile = pFiles->GetFile(i);
   char* sFilename = pFile->GetName;  // <-- Error
   fprintf(fNewplaylist, "%s\n", sFilename);
}
Title: Re:Plugins with dev-c++
Post by: RhinoBanga on July 26, 2004, 02:58:43 am
GetFile() returns a bstr which is not the same as a char*.

Do a google search for "convert bstr to char*" and you will find loads of solutions, e.g.:


inline std::string WideToAnsi(const std::wstring & str)
{
  char * sz = new char[str.length()+1];
  ::WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1,
    sz, str.length()+1, NULL, NULL);
  std::string s = sz;
  delete[] sz;
  return s;
}

char * BSTRToCharStar(BSTR bstr)
{
  static std::string str = WideToAnsi(bstr);
  return str.c_str();
}




(Note the above is untested and has loads of limitations)


Also you really should stick with using bstr for internationalization.
Title: Re:Plugins with dev-c++
Post by: buhrmi on July 26, 2004, 01:40:44 pm
Please excuse my not-knowing, but how do I write to a file without using char*s?
Title: Re:Plugins with dev-c++
Post by: RhinoBanga on July 27, 2004, 02:23:19 am
Use CreateFile to create the file.
Use WriteFile to write out the contents.
Use CloseHandle to close the file.

Information on these can be found at msdn.microsoft.com.
Title: Re:Plugins with dev-c++
Post by: Zoner on July 27, 2004, 06:24:35 am
Actually that's not the problem.  The problem is that you forgot the brackets.  You wrote:

pFile->GetName

But you should have written:

pFile->GetName()

The brackets indicate a function call.  The lack of brackets indicate the function itself.  So what you are doing with the code you wrote is to set sFilename to the address of the function GetName.  I'm very surprised that this compiled - you should have gotten error C2664: cannot convert from '(something)' to char *.  Which version of VC++ are you using?

What RhinoBanga wrote about converting BSTR to char * is correct, but in this case it is a _bstr_t object, which contains an operator char * to automatically handle the conversion for you.  Note that you have to be careful using those operators - some of then can cause memory leaks.  In any case, I would declare sFilename as const char * since you are not modifying it.
Title: Re:Plugins with dev-c++
Post by: RhinoBanga on July 27, 2004, 10:12:07 am
Are you sure it does automatic conversion?   According to MSDN:

Extract the pointers to the encapsulated Unicode or multibyte BSTR object.

operator const wchar_t*( ) const throw( );
operator wchar_t*( ) const throw( );
operator const char*( ) const;
operator char*( ) const;
Remarks
These operators can be used to extract raw pointers to the encapsulated Unicode or multibyte BSTR object. The operators return the pointer to the actual internal buffer, so the resulting string cannot be modified.

Title: Re:Plugins with dev-c++
Post by: Zoner on July 27, 2004, 05:01:15 pm
Right - you've got to be very careful with them.  I do this, which seems to work fine:

char Artist[200];

strncpy(Artist, File->GetArtist(), sizeof(Artist)-1);
Artist[sizeof(Artist)-1] = '\0';

This does *not* work, presumably because the char * pointer returned by the operator char *() is only temporary, but I haven't checked it out:

const char * Artist = File->Artist();

In fact I haven't tried this, but it probably would work:

std::string Artist = File->GetArtist();