Skip to main content
gamedev.net
🔒 Locked

Problems with Direct Show

Started by Keba 3 replies 350+ views
Original Post
Keba
Keba
i wan''t to play mp3 with DS, and i''m using HRESULT AddSourceFilter( LPCWSTR lpwstrFileName, LPCWSTR lpwstrFilterName, IBaseFilter **ppFilter ); the problem is first and secon parameters, i wan''t to convert char * to LPCWSTR in some quick/easy/painfree way... i tried this, but i guess its wrong, because i get some memory problems: LPWSTR pfilename2 = new WCHAR[strlen(filename)]; MultiByteToWideChar(CP_ACP, 0, filename, -1, pfilename2, strlen(filename)); pfilename2[strlen(filename)] = ''\0''; please help me
JoeyBlow2
JoeyBlow2
try using the bstr macro with the parameters.

_bstr_t(char *);

I believe the _bstr_t class can return a char * or a wchar * in its class (along with taking both as a parameter), so it works well for these type of situations/conversions.
hellz
hellz
I could be wrong with what I'm about to say, but I'll say it anyway. I think the reason you get "memory problems" when using strlen(filename), is because WCHAR has a size of > 1 byte.

So for example, say your filename was, "test.mp3". strlen() would return 8, but if WCHAR was say 2 bytes wide, you'd be allocating half the size of which you required, because 8 * 2 = 16.

This might work instead:


LPWSTR pfilename2 = new WCHAR[strlen(filename) * sizeof(WCHAR)];


Feel free to correct me if I'm wrong.

-hellz

Edit: Obviously you'd have to use this same method for when you're using strlen() in those other functions as well. Would be best to store the first result of strlen(), so that it doesn't have to be calculated over and over again.

Edit 2: You'd have to add space for the terminating null character as well, like so:


LPWSTR pfilename2 = new WCHAR[strlen(filename) * sizeof(WCHAR) + sizeof(WCHAR)];


[edited by - hellz on November 12, 2003 2:05:20 PM]
Keba
Keba
hmm i tried the both way, but in the fisrt case i neded to include comutil.h which worked fine, but when compiling the compiler didn''t fin some source

and in the second case where still a memory leak.....

but i solved it in this way:

int nLen = MultiByteToWideChar(CP_ACP, 0,filename, -1, NULL, NULL);


which is the lenght of the new string, however, i have no clue what i did .....took it from the docs

hey! thanks both of you!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.