]> git.sesse.net Git - vlc/blob - activex/vlccontrol.cpp
vlccontrol.cpp: fixed bounds issue when allocating memory and iterating through a...
[vlc] / activex / vlccontrol.cpp
1 /*****************************************************************************
2  * vlccontrol.cpp: ActiveX control for VLC
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  *
6  * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 #include "plugin.h"
24 #include "vlccontrol.h"
25
26 #include "utils.h"
27
28 using namespace std;
29
30 VLCControl::~VLCControl()
31 {
32     if( _p_typeinfo )
33         _p_typeinfo->Release();
34 };
35
36 HRESULT VLCControl::getTypeInfo(void)
37 {
38     HRESULT hr = NOERROR;
39     if( NULL == _p_typeinfo )
40     {
41         ITypeLib *p_typelib;
42
43         HRESULT hr = _p_instance->getTypeLib(LOCALE_USER_DEFAULT, &p_typelib);
44         if( SUCCEEDED(hr) )
45         {
46             hr = p_typelib->GetTypeInfoOfGuid(IID_IVLCControl, &_p_typeinfo);
47             if( FAILED(hr) )
48             {
49                 _p_typeinfo = NULL;
50             }
51             p_typelib->Release();
52         }
53     }
54     return hr;
55 };
56
57 STDMETHODIMP VLCControl::GetTypeInfoCount(UINT* pctInfo)
58 {
59     if( NULL == pctInfo )
60         return E_INVALIDARG;
61
62     if( SUCCEEDED(getTypeInfo()) )
63         *pctInfo = 1;
64     else
65         *pctInfo = 0;
66
67     return NOERROR;
68 };
69
70 STDMETHODIMP VLCControl::GetTypeInfo(UINT iTInfo, LCID lcid, LPTYPEINFO* ppTInfo)
71 {
72     if( NULL == ppTInfo )
73         return E_INVALIDARG;
74
75     if( SUCCEEDED(getTypeInfo()) )
76     {
77         _p_typeinfo->AddRef();
78         *ppTInfo = _p_typeinfo;
79         return NO_ERROR;
80     }
81     *ppTInfo = NULL;
82     return E_NOTIMPL;
83 };
84
85 STDMETHODIMP VLCControl::GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames, 
86         UINT cNames, LCID lcid, DISPID* rgDispID)
87 {
88     if( SUCCEEDED(getTypeInfo()) )
89     {
90         return DispGetIDsOfNames(_p_typeinfo, rgszNames, cNames, rgDispID);
91     }
92     return E_NOTIMPL;
93 };
94
95 STDMETHODIMP VLCControl::Invoke(DISPID dispIdMember, REFIID riid,
96         LCID lcid, WORD wFlags, DISPPARAMS* pDispParams,
97         VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
98 {
99     if( SUCCEEDED(getTypeInfo()) )
100     {
101         return DispInvoke(this, _p_typeinfo, dispIdMember, wFlags, pDispParams,
102                 pVarResult, pExcepInfo, puArgErr);
103     }
104     return E_NOTIMPL;
105 };
106
107 STDMETHODIMP VLCControl::get_Visible(VARIANT_BOOL *isVisible)
108 {
109     if( NULL == isVisible )
110         return E_POINTER;
111
112     *isVisible = _p_instance->getVisible() ? VARIANT_TRUE : VARIANT_FALSE;
113
114     return NOERROR;
115 };
116         
117 STDMETHODIMP VLCControl::put_Visible(VARIANT_BOOL isVisible)
118 {
119     _p_instance->setVisible(isVisible != VARIANT_FALSE);
120
121     return NOERROR;
122 };
123
124 STDMETHODIMP VLCControl::play(void)
125 {
126     int i_vlc = _p_instance->getVLCObject();
127     if( i_vlc )
128     {
129         VLC_Play(i_vlc);
130         _p_instance->fireOnPlayEvent();
131         return NOERROR;
132     }
133     return E_UNEXPECTED;
134 };
135  
136 STDMETHODIMP VLCControl::pause(void)
137 {
138     int i_vlc = _p_instance->getVLCObject();
139     if( i_vlc )
140     {
141         VLC_Pause(i_vlc);
142         _p_instance->fireOnPauseEvent();
143         return NOERROR;
144     }
145     return E_UNEXPECTED;
146 };
147         
148 STDMETHODIMP VLCControl::stop(void)
149 {
150     int i_vlc = _p_instance->getVLCObject();
151     if( i_vlc )
152     {
153         VLC_Stop(i_vlc);
154         _p_instance->fireOnStopEvent();
155         return NOERROR;
156     }
157     return E_UNEXPECTED;
158 };
159         
160 STDMETHODIMP VLCControl::get_Playing(VARIANT_BOOL *isPlaying)
161 {
162     if( NULL == isPlaying )
163         return E_POINTER;
164
165     int i_vlc = _p_instance->getVLCObject();
166     if( i_vlc )
167     {
168         *isPlaying = VLC_IsPlaying(i_vlc) ? VARIANT_TRUE : VARIANT_FALSE;
169         return NOERROR;
170     }
171     *isPlaying = VARIANT_FALSE;
172     return E_UNEXPECTED;
173 };
174         
175 STDMETHODIMP VLCControl::get_Position(float *position)
176 {
177     if( NULL == position )
178         return E_POINTER;
179
180     int i_vlc = _p_instance->getVLCObject();
181     if( i_vlc )
182     {
183         *position = VLC_PositionGet(i_vlc);
184         return NOERROR;
185     }
186     *position = 0.0f;
187     return E_UNEXPECTED;
188 };
189         
190 STDMETHODIMP VLCControl::put_Position(float position)
191 {
192     int i_vlc = _p_instance->getVLCObject();
193     if( i_vlc )
194     {
195         VLC_PositionSet(i_vlc, position);
196         return NOERROR;
197     }
198     return E_UNEXPECTED;
199 };
200         
201 STDMETHODIMP VLCControl::get_Time(int *seconds)
202 {
203     if( NULL == seconds )
204         return E_POINTER;
205
206     int i_vlc = _p_instance->getVLCObject();
207     if( i_vlc )
208     {
209         *seconds = VLC_TimeGet(i_vlc);
210         return NOERROR;
211     }
212     *seconds = 0;
213     return E_UNEXPECTED;
214 };
215         
216 STDMETHODIMP VLCControl::put_Time(int seconds)
217 {
218     int i_vlc = _p_instance->getVLCObject();
219     if( i_vlc )
220     {
221         VLC_TimeSet(i_vlc, seconds, VLC_FALSE);
222         return NOERROR;
223     }
224     return E_UNEXPECTED;
225 };
226         
227 STDMETHODIMP VLCControl::shuttle(int seconds)
228 {
229     int i_vlc = _p_instance->getVLCObject();
230     if( i_vlc )
231     {
232         VLC_TimeSet(i_vlc, seconds, VLC_TRUE);
233         return NOERROR;
234     }
235     return E_UNEXPECTED;
236 };
237         
238 STDMETHODIMP VLCControl::fullscreen(void)
239 {
240     int i_vlc = _p_instance->getVLCObject();
241     if( i_vlc )
242     {
243         VLC_FullScreen(i_vlc);
244         return NOERROR;
245     }
246     return E_UNEXPECTED;
247 };
248         
249 STDMETHODIMP VLCControl::get_Length(int *seconds)
250 {
251     if( NULL == seconds )
252         return E_POINTER;
253
254     int i_vlc = _p_instance->getVLCObject();
255     if( i_vlc )
256     {
257         *seconds = VLC_LengthGet(i_vlc);
258         return NOERROR;
259     }
260     *seconds = 0;
261     return E_UNEXPECTED;
262 };
263         
264 STDMETHODIMP VLCControl::playFaster(void)
265 {
266     int i_vlc = _p_instance->getVLCObject();
267     if( i_vlc )
268     {
269         VLC_SpeedFaster(i_vlc);
270         return NOERROR;
271     }
272     return E_UNEXPECTED;
273 };
274         
275 STDMETHODIMP VLCControl::playSlower(void)
276 {
277     int i_vlc = _p_instance->getVLCObject();
278     if( i_vlc )
279     {
280         VLC_SpeedSlower(i_vlc);
281         return NOERROR;
282     }
283     return E_UNEXPECTED;
284 };
285         
286 STDMETHODIMP VLCControl::get_Volume(int *volume)
287 {
288     if( NULL == volume )
289         return E_POINTER;
290
291     int i_vlc = _p_instance->getVLCObject();
292     if( i_vlc )
293     {
294         *volume  = VLC_VolumeGet(i_vlc);
295         return NOERROR;
296     }
297     *volume = 0;
298     return E_UNEXPECTED;
299 };
300         
301 STDMETHODIMP VLCControl::put_Volume(int volume)
302 {
303     int i_vlc = _p_instance->getVLCObject();
304     if( i_vlc )
305     {
306         VLC_VolumeSet(i_vlc, volume);
307         return NOERROR;
308     }
309     return E_UNEXPECTED;
310 };
311         
312 STDMETHODIMP VLCControl::toggleMute(void)
313 {
314     int i_vlc = _p_instance->getVLCObject();
315     if( i_vlc )
316     {
317         VLC_VolumeMute(i_vlc);
318         return NOERROR;
319     }
320     return E_UNEXPECTED;
321 };
322
323 STDMETHODIMP VLCControl::setVariable(BSTR name, VARIANT value)
324 {
325     if( 0 == SysStringLen(name) )
326         return E_INVALIDARG;
327
328     int i_vlc = _p_instance->getVLCObject();
329     if( i_vlc )
330     {
331         int codePage = _p_instance->getCodePage();
332         char *psz_varname = CStrFromBSTR(codePage, name);
333         if( NULL == psz_varname )
334             return E_OUTOFMEMORY;
335
336         HRESULT hr = E_INVALIDARG;
337         int i_type;
338         vlc_value_t val;
339         
340         if( VLC_SUCCESS == VLC_VariableType(i_vlc, psz_varname, &i_type) )
341         {
342             VARIANT arg;
343             VariantInit(&arg);
344
345             switch( i_type )
346             {
347                 case VLC_VAR_BOOL:
348                     hr = VariantChangeType(&arg, &value, 0, VT_BOOL);
349                     if( SUCCEEDED(hr) )
350                         val.b_bool = (VARIANT_TRUE == V_BOOL(&arg)) ? VLC_TRUE : VLC_FALSE;
351                     break;
352
353                 case VLC_VAR_INTEGER:
354                 case VLC_VAR_HOTKEY:
355                     hr = VariantChangeType(&arg, &value, 0, VT_I4);
356                     if( SUCCEEDED(hr) )
357                         val.i_int = V_I4(&arg);
358                     break;
359
360                 case VLC_VAR_FLOAT:
361                     hr = VariantChangeType(&arg, &value, 0, VT_R4);
362                     if( SUCCEEDED(hr) )
363                         val.f_float = V_R4(&arg);
364                     break;
365
366                 case VLC_VAR_STRING:
367                 case VLC_VAR_MODULE:
368                 case VLC_VAR_FILE:
369                 case VLC_VAR_DIRECTORY:
370                 case VLC_VAR_VARIABLE:
371                     hr = VariantChangeType(&arg, &value, 0, VT_BSTR);
372                     if( SUCCEEDED(hr) )
373                     {
374                         i_type = VLC_VAR_STRING;
375                         val.psz_string = CStrFromBSTR(codePage, V_BSTR(&arg));
376                         VariantClear(&arg);
377                     }
378                     break;
379
380                 case VLC_VAR_TIME:
381                     // use a double value to represent time (base is expressed in seconds)
382                     hr = VariantChangeType(&arg, &value, 0, VT_R8);
383                     if( SUCCEEDED(hr) )
384                         val.i_time = (signed __int64)(V_R8(&arg)*1000000.0);
385                     break;
386
387                 default:
388                     hr = DISP_E_TYPEMISMATCH;
389             }
390         }
391         else {
392             // no defined type, defaults to VARIANT type
393             hr = NO_ERROR;
394             switch( V_VT(&value) )
395             {
396                 case VT_BOOL:
397                     val.b_bool = (VARIANT_TRUE == V_BOOL(&value)) ? VLC_TRUE : VLC_FALSE;
398                     i_type = VLC_VAR_BOOL;
399                     break;
400                 case VT_I4:
401                     val.i_int = V_I4(&value);
402                     i_type = VLC_VAR_INTEGER;
403                     break;
404                 case VT_R4:
405                     val.f_float = V_R4(&value);
406                     i_type = VLC_VAR_FLOAT;
407                     break;
408                 case VT_BSTR:
409                     val.psz_string = CStrFromBSTR(codePage, V_BSTR(&value));
410                     i_type = VLC_VAR_STRING;
411                     break;
412                 case VT_R8:
413                     // use a double value to represent time (base is expressed in seconds)
414                     val.i_time = (signed __int64)(V_R8(&value)*1000000.0);
415                     i_type = VLC_VAR_TIME;
416                     break;
417                 default:
418                     hr = DISP_E_TYPEMISMATCH;
419             }
420         }
421         if( SUCCEEDED(hr) )
422         {
423             hr = (VLC_SUCCESS == VLC_VariableSet(i_vlc, psz_varname, val)) ? NOERROR : E_FAIL;
424
425             if( (VLC_VAR_STRING == i_type) && (NULL != val.psz_string) )
426                 CoTaskMemFree(val.psz_string);
427         }
428         CoTaskMemFree(psz_varname);
429
430         return hr;
431     }
432     return E_UNEXPECTED;
433 };
434
435 STDMETHODIMP VLCControl::getVariable( BSTR name, VARIANT *value)
436 {
437     if( NULL == value )
438         return E_POINTER;
439
440     VariantInit(value);
441
442     if( 0 == SysStringLen(name) )
443         return E_INVALIDARG;
444
445     int i_vlc = _p_instance->getVLCObject();
446     if( i_vlc )
447     {
448         UINT codePage = _p_instance->getCodePage();
449         char *psz_varname = CStrFromBSTR(codePage, name);
450         if( NULL == psz_varname )
451             return E_OUTOFMEMORY;
452
453         HRESULT hr = E_INVALIDARG;
454
455         vlc_value_t val;
456         int i_type;
457
458         if( (VLC_SUCCESS == VLC_VariableGet(i_vlc, psz_varname, &val))
459          && (VLC_SUCCESS == VLC_VariableType(i_vlc, psz_varname, &i_type)) )
460         {
461             hr = NOERROR;
462             switch( i_type )
463             {
464                 case VLC_VAR_BOOL:
465                     V_VT(value) = VT_BOOL;
466                     V_BOOL(value) = val.b_bool ? VARIANT_TRUE : VARIANT_FALSE;
467                     break;
468
469                 case VLC_VAR_INTEGER:
470                 case VLC_VAR_HOTKEY:
471                     V_VT(value) = VT_I4;
472                     V_I4(value) = val.i_int;
473                     break;
474
475                 case VLC_VAR_FLOAT:
476                     V_VT(value) = VT_R4;
477                     V_R4(value) = val.f_float;
478                     break;
479
480                 case VLC_VAR_STRING:
481                 case VLC_VAR_MODULE:
482                 case VLC_VAR_FILE:
483                 case VLC_VAR_DIRECTORY:
484                 case VLC_VAR_VARIABLE:
485                     V_VT(value) = VT_BSTR;
486                     V_BSTR(value) = BSTRFromCStr(codePage, val.psz_string);
487                     if( NULL != val.psz_string)
488                         free(val.psz_string);
489                     break;
490
491                 case VLC_VAR_TIME:
492                     // use a double value to represent time (base is expressed in seconds)
493                     V_VT(value) = VT_R8;
494                     V_R8(value) = ((double)val.i_time)/1000000.0;
495                     break;
496
497                 default:
498                     hr = DISP_E_TYPEMISMATCH;
499             }
500         }
501         CoTaskMemFree(psz_varname);
502         return hr;
503     }
504     return E_UNEXPECTED;
505 };
506
507 static void freeTargetOptions(char **cOptions, int cOptionCount)
508 {
509     // clean up 
510     if( NULL != cOptions )
511     {
512         for( int pos=0; pos<cOptionCount; ++pos )
513         {
514             char *cOption = cOptions[pos];
515             if( NULL != cOption )
516                 CoTaskMemFree(cOption);
517             else
518                 break;
519         }
520         CoTaskMemFree(cOptions);
521     }
522 };
523
524 static HRESULT createTargetOptions(int codePage, VARIANT *options, char ***cOptions, int *cOptionCount)
525 {
526     HRESULT hr = E_INVALIDARG;
527     if( VT_ERROR == V_VT(options) )
528     {
529         if( DISP_E_PARAMNOTFOUND == V_ERROR(options) )
530         {
531             // optional parameter not set
532             *cOptions = NULL;
533             *cOptionCount = 0;
534             return NOERROR;
535         }
536     }
537     else if( (VT_EMPTY == V_VT(options)) || (VT_NULL == V_VT(options)) )
538     {
539         // null parameter
540         *cOptions = NULL;
541         *cOptionCount = 0;
542         return NOERROR;
543     }
544     else if( VT_DISPATCH == V_VT(options) )
545     {
546         // collection parameter
547         VARIANT colEnum;
548         V_VT(&colEnum) = VT_UNKNOWN;
549         hr = GetObjectProperty(V_DISPATCH(options), DISPID_NEWENUM, colEnum);
550         if( SUCCEEDED(hr) )
551         {
552             IEnumVARIANT *enumVar;
553             hr = V_UNKNOWN(&colEnum)->QueryInterface(IID_IEnumVARIANT, (LPVOID *)&enumVar);
554             if( SUCCEEDED(hr) )
555             {
556                 long pos = 0;
557                 long capacity = 16;
558                 VARIANT option;
559
560                 *cOptions = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
561                 if( NULL != *cOptions )
562                 {
563                     ZeroMemory(*cOptions, sizeof(char *)*capacity);
564                     while( SUCCEEDED(hr) && (S_OK == enumVar->Next(1, &option, NULL)) )
565                     {
566                         if( VT_BSTR == V_VT(&option) )
567                         {
568                             char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
569                             (*cOptions)[pos] = cOption;
570                             if( NULL != cOption )
571                             {
572                                 ++pos;
573                                 if( pos == capacity )
574                                 {
575                                     char **moreOptions = (char **)CoTaskMemRealloc(*cOptions, (capacity+16)*sizeof(char *));
576                                     if( NULL != moreOptions )
577                                     {
578                                         ZeroMemory(moreOptions+capacity, sizeof(char *)*16);
579                                         capacity += 16;
580                                         *cOptions = moreOptions;
581                                     }
582                                     else
583                                         hr = E_OUTOFMEMORY;
584                                 }
585                             }
586                             else
587                                 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
588                                     E_OUTOFMEMORY : E_INVALIDARG;
589                         }
590                         else
591                             hr = E_INVALIDARG;
592
593                         VariantClear(&option);
594                     }
595                     *cOptionCount = pos;
596                     if( FAILED(hr) )
597                     {
598                         // free already processed elements
599                         freeTargetOptions(*cOptions, *cOptionCount);
600                     }
601                 }
602                 else
603                     hr = E_OUTOFMEMORY;
604
605                 enumVar->Release();
606             }
607         }
608     }
609     else if( V_ISARRAY(options) )
610     {
611         // array parameter
612         SAFEARRAY *array = V_ISBYREF(options) ? *V_ARRAYREF(options) : V_ARRAY(options);
613
614         if( SafeArrayGetDim(array) != 1 )
615             return E_INVALIDARG;
616
617         long lBound = 0;
618         long uBound = 0;
619         SafeArrayGetLBound(array, 1, &lBound);
620         SafeArrayGetUBound(array, 1, &uBound);
621
622         // have we got any options
623         if( uBound >= lBound )
624         {
625             VARTYPE vType;
626             hr = SafeArrayGetVartype(array, &vType);
627             if( FAILED(hr) )
628                 return hr;
629
630             long pos;
631
632             // marshall options into an array of C strings
633             if( VT_VARIANT == vType )
634             {
635                 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
636                 if( NULL == *cOptions )
637                     return E_OUTOFMEMORY;
638
639                 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
640                 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
641                 {
642                     VARIANT option;
643                     hr = SafeArrayGetElement(array, &pos, &option);
644                     if( SUCCEEDED(hr) )
645                     {
646                         if( VT_BSTR == V_VT(&option) ) 
647                         {
648                             char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
649                             (*cOptions)[pos-lBound] = cOption;
650                             if( NULL == cOption )
651                                 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
652                                     E_OUTOFMEMORY : E_INVALIDARG;
653                         }
654                         else
655                             hr = E_INVALIDARG;
656                         VariantClear(&option);
657                     }
658                 }
659             }
660             else if( VT_BSTR == vType )
661             {
662                 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
663                 if( NULL == *cOptions )
664                     return E_OUTOFMEMORY;
665
666                 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
667                 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
668                 {
669                     BSTR option;
670                     hr = SafeArrayGetElement(array, &pos, &option);
671                     if( SUCCEEDED(hr) )
672                     {
673                         char *cOption = CStrFromBSTR(codePage, option);
674
675                         (*cOptions)[pos-lBound] = cOption;
676                         if( NULL == cOption )
677                             hr = ( SysStringLen(option) > 0 ) ?
678                                 E_OUTOFMEMORY : E_INVALIDARG;
679                         SysFreeString(option);
680                     }
681                 }
682             }
683             else 
684             {
685                 // unsupported type
686                 return E_INVALIDARG;
687             }
688
689             *cOptionCount = pos-lBound;
690             if( FAILED(hr) )
691             {
692                 // free already processed elements
693                 freeTargetOptions(*cOptions, *cOptionCount);
694             }
695         }
696         else
697         {
698             // empty array
699             *cOptions = NULL;
700             *cOptionCount = 0;
701             return NOERROR;
702         }
703     }
704     return hr;
705 };
706
707 /*
708 ** use VARIANT rather than a SAFEARRAY as argument type
709 ** for compatibility with some scripting language (JScript)
710 */
711
712 STDMETHODIMP VLCControl::addTarget( BSTR uri, VARIANT options, enum VLCPlaylistMode mode, int position)
713 {
714     if( 0 == SysStringLen(uri) )
715         return E_INVALIDARG;
716
717     HRESULT hr = E_UNEXPECTED;
718
719     int i_vlc = _p_instance->getVLCObject();
720     if( i_vlc )
721     {
722         char *cUri = CStrFromBSTR(CP_UTF8, uri);
723         if( NULL == cUri )
724             return E_OUTOFMEMORY;
725
726         int cOptionsCount;
727         char **cOptions;
728
729         if( FAILED(createTargetOptions(CP_UTF8, &options, &cOptions, &cOptionsCount)) )
730             return E_INVALIDARG;
731
732         if( VLC_SUCCESS <= VLC_AddTarget(i_vlc, cUri, (const char **)cOptions, cOptionsCount, mode, position) )
733         {
734             hr = NOERROR;
735             if( mode & PLAYLIST_GO )
736                 _p_instance->fireOnPlayEvent();
737         }
738         else
739         {
740             hr = E_FAIL;
741             if( mode & PLAYLIST_GO )
742                 _p_instance->fireOnStopEvent();
743         }
744
745         freeTargetOptions(cOptions, cOptionsCount);
746         CoTaskMemFree(cUri);
747     }
748     return hr;
749 };
750         
751 STDMETHODIMP VLCControl::get_PlaylistIndex(int *index)
752 {
753     if( NULL == index )
754         return E_POINTER;
755
756     int i_vlc = _p_instance->getVLCObject();
757     if( i_vlc )
758     {
759         *index = VLC_PlaylistIndex(i_vlc);
760         return NOERROR;
761     }
762     *index = 0;
763     return E_UNEXPECTED;
764 };
765         
766 STDMETHODIMP VLCControl::get_PlaylistCount(int *count)
767 {
768     int i_vlc = _p_instance->getVLCObject();
769     if( i_vlc )
770     {
771         *count = VLC_PlaylistNumberOfItems(i_vlc);
772         return NOERROR;
773     }
774     *count = 0;
775     return E_UNEXPECTED;
776 };
777         
778 STDMETHODIMP VLCControl::playlistNext(void)
779 {
780     int i_vlc = _p_instance->getVLCObject();
781     if( i_vlc )
782     {
783         VLC_PlaylistNext(i_vlc);
784         return NOERROR;
785     }
786     return E_UNEXPECTED;
787 };
788         
789 STDMETHODIMP VLCControl::playlistPrev(void)
790 {
791     int i_vlc = _p_instance->getVLCObject();
792     if( i_vlc )
793     {
794         VLC_PlaylistPrev(i_vlc);
795         return NOERROR;
796     }
797     return E_UNEXPECTED;
798 };
799         
800 STDMETHODIMP VLCControl::playlistClear(void)
801 {
802     int i_vlc = _p_instance->getVLCObject();
803     if( i_vlc )
804     {
805         VLC_PlaylistClear(i_vlc);
806         return NOERROR;
807     }
808     return E_UNEXPECTED;
809 };
810         
811 STDMETHODIMP VLCControl::get_VersionInfo(BSTR *version)
812 {
813     if( NULL == version )
814         return E_POINTER;
815
816     const char *versionStr = VLC_Version();
817     if( NULL != versionStr )
818     {
819         *version = BSTRFromCStr(_p_instance->getCodePage(), versionStr);
820         
821         return NULL == *version ? E_OUTOFMEMORY : NOERROR;
822     }
823     *version = NULL;
824     return E_FAIL;
825 };
826  
827 STDMETHODIMP VLCControl::get_MRL(BSTR *mrl)
828 {
829     if( NULL == mrl )
830         return E_POINTER;
831
832     *mrl = SysAllocStringLen(_p_instance->getMRL(),
833                 SysStringLen(_p_instance->getMRL()));
834     return NOERROR;
835 };
836
837 STDMETHODIMP VLCControl::put_MRL(BSTR mrl)
838 {
839     _p_instance->setMRL(mrl);
840
841     return S_OK;
842 };
843
844 STDMETHODIMP VLCControl::get_AutoPlay(VARIANT_BOOL *autoplay)
845 {
846     if( NULL == autoplay )
847         return E_POINTER;
848
849     *autoplay = _p_instance->getAutoPlay() ? VARIANT_TRUE: VARIANT_FALSE;
850     return S_OK;
851 };
852
853 STDMETHODIMP VLCControl::put_AutoPlay(VARIANT_BOOL autoplay)
854 {
855     _p_instance->setAutoPlay((VARIANT_FALSE != autoplay) ? TRUE: FALSE);
856     return S_OK;
857 };
858
859 STDMETHODIMP VLCControl::get_AutoLoop(VARIANT_BOOL *autoloop)
860 {
861     if( NULL == autoloop )
862         return E_POINTER;
863
864     *autoloop = _p_instance->getAutoLoop() ? VARIANT_TRUE: VARIANT_FALSE;
865     return S_OK;
866 };
867
868 STDMETHODIMP VLCControl::put_AutoLoop(VARIANT_BOOL autoloop)
869 {
870     _p_instance->setAutoLoop((VARIANT_FALSE != autoloop) ? TRUE: FALSE);
871     return S_OK;
872 };
873