]> git.sesse.net Git - vlc/blob - projects/activex/vlccontrol.cpp
http access: Use EnsureUTF8() on the ICY strings. Avoids "illegal byte sequence"...
[vlc] / projects / 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  *          Jean-Paul Saman <jpsaman@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include "plugin.h"
25 #include "vlccontrol.h"
26
27 #include "utils.h"
28
29 using namespace std;
30
31 VLCControl::~VLCControl()
32 {
33     if( _p_typeinfo )
34         _p_typeinfo->Release();
35 };
36
37 HRESULT VLCControl::getTypeInfo(void)
38 {
39     HRESULT hr = NOERROR;
40     if( NULL == _p_typeinfo )
41     {
42         ITypeLib *p_typelib;
43
44         hr = _p_instance->getTypeLib(LOCALE_USER_DEFAULT, &p_typelib);
45         if( SUCCEEDED(hr) )
46         {
47             hr = p_typelib->GetTypeInfoOfGuid(IID_IVLCControl, &_p_typeinfo);
48             if( FAILED(hr) )
49             {
50                 _p_typeinfo = NULL;
51             }
52             p_typelib->Release();
53         }
54     }
55     return hr;
56 };
57
58 STDMETHODIMP VLCControl::GetTypeInfoCount(UINT* pctInfo)
59 {
60     if( NULL == pctInfo )
61         return E_INVALIDARG;
62
63     if( SUCCEEDED(getTypeInfo()) )
64         *pctInfo = 1;
65     else
66         *pctInfo = 0;
67
68     return NOERROR;
69 };
70
71 STDMETHODIMP VLCControl::GetTypeInfo(UINT iTInfo, LCID lcid, LPTYPEINFO* ppTInfo)
72 {
73     if( NULL == ppTInfo )
74         return E_INVALIDARG;
75
76     if( SUCCEEDED(getTypeInfo()) )
77     {
78         _p_typeinfo->AddRef();
79         *ppTInfo = _p_typeinfo;
80         return NOERROR;
81     }
82     *ppTInfo = NULL;
83     return E_NOTIMPL;
84 };
85
86 STDMETHODIMP VLCControl::GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames,
87         UINT cNames, LCID lcid, DISPID* rgDispID)
88 {
89     if( SUCCEEDED(getTypeInfo()) )
90     {
91         return DispGetIDsOfNames(_p_typeinfo, rgszNames, cNames, rgDispID);
92     }
93     return E_NOTIMPL;
94 };
95
96 STDMETHODIMP VLCControl::Invoke(DISPID dispIdMember, REFIID riid,
97         LCID lcid, WORD wFlags, DISPPARAMS* pDispParams,
98         VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
99 {
100     if( SUCCEEDED(getTypeInfo()) )
101     {
102         return DispInvoke(this, _p_typeinfo, dispIdMember, wFlags, pDispParams,
103                 pVarResult, pExcepInfo, puArgErr);
104     }
105     return E_NOTIMPL;
106 };
107
108 STDMETHODIMP VLCControl::get_Visible(VARIANT_BOOL *isVisible)
109 {
110     if( NULL == isVisible )
111         return E_POINTER;
112
113     *isVisible = _p_instance->getVisible() ? VARIANT_TRUE : VARIANT_FALSE;
114
115     return NOERROR;
116 };
117
118 STDMETHODIMP VLCControl::put_Visible(VARIANT_BOOL isVisible)
119 {
120     _p_instance->setVisible(isVisible != VARIANT_FALSE);
121
122     return NOERROR;
123 };
124
125 STDMETHODIMP VLCControl::play(void)
126 {
127     libvlc_instance_t* p_libvlc;
128     HRESULT result = _p_instance->getVLC(&p_libvlc);
129     if( SUCCEEDED(result) )
130     {
131         libvlc_exception_t ex;
132         libvlc_exception_init(&ex);
133
134         libvlc_playlist_play(p_libvlc, -1, 0, NULL, &ex);
135         if( libvlc_exception_raised(&ex) )
136         {
137             _p_instance->setErrorInfo(IID_IVLCControl,
138                 libvlc_exception_get_message(&ex));
139             libvlc_exception_clear(&ex);
140             return E_FAIL;
141         }
142         _p_instance->fireOnPlayEvent();
143         return NOERROR;
144     }
145     return result;
146 };
147
148 STDMETHODIMP VLCControl::pause(void)
149 {
150     libvlc_instance_t* p_libvlc;
151     HRESULT result = _p_instance->getVLC(&p_libvlc);
152     if( SUCCEEDED(result) )
153     {
154         libvlc_exception_t ex;
155         libvlc_exception_init(&ex);
156
157         libvlc_playlist_pause(p_libvlc, &ex);
158         if( libvlc_exception_raised(&ex) )
159         {
160             _p_instance->setErrorInfo(IID_IVLCControl,
161                 libvlc_exception_get_message(&ex));
162             libvlc_exception_clear(&ex);
163             return E_FAIL;
164         }
165         _p_instance->fireOnPauseEvent();
166         return NOERROR;
167     }
168     return result;
169 };
170
171 STDMETHODIMP VLCControl::stop(void)
172 {
173     libvlc_instance_t* p_libvlc;
174     HRESULT result = _p_instance->getVLC(&p_libvlc);
175     if( SUCCEEDED(result) )
176     {
177         libvlc_exception_t ex;
178         libvlc_exception_init(&ex);
179
180         libvlc_playlist_stop(p_libvlc, &ex);
181         if( libvlc_exception_raised(&ex) )
182         {
183             _p_instance->setErrorInfo(IID_IVLCControl,
184                 libvlc_exception_get_message(&ex));
185             libvlc_exception_clear(&ex);
186             return E_FAIL;
187         }
188         return NOERROR;
189     }
190     _p_instance->fireOnStopEvent();
191     return result;
192 };
193
194 STDMETHODIMP VLCControl::get_Playing(VARIANT_BOOL *isPlaying)
195 {
196     if( NULL == isPlaying )
197         return E_POINTER;
198
199     HRESULT result = NOERROR;
200     if( _p_instance->isRunning() )
201     {
202         libvlc_instance_t *p_libvlc;
203         result = _p_instance->getVLC(&p_libvlc);
204         if( SUCCEEDED(result) )
205         {
206             if( libvlc_playlist_isplaying(p_libvlc, NULL) )
207                 *isPlaying = VARIANT_TRUE;
208             else
209                 *isPlaying = VARIANT_FALSE;
210             return NOERROR;
211         }
212     }
213     *isPlaying = VARIANT_FALSE;
214     return result;
215 };
216
217 STDMETHODIMP VLCControl::get_Position(float *position)
218 {
219     if( NULL == position )
220         return E_POINTER;
221     *position = 0.0f;
222
223     libvlc_instance_t* p_libvlc;
224     HRESULT result = E_UNEXPECTED;
225     result = _p_instance->getVLC(&p_libvlc);
226     if( SUCCEEDED(result) )
227     {
228         libvlc_exception_t ex;
229         libvlc_exception_init(&ex);
230
231         libvlc_media_player_t *p_md;
232         p_md = libvlc_playlist_get_media_player(p_libvlc, &ex);
233         if( !libvlc_exception_raised(&ex) )
234         {
235             *position = libvlc_media_player_get_position(p_md, &ex);
236             libvlc_media_player_release(p_md);
237             if( ! libvlc_exception_raised(&ex) )
238             {
239                 return NOERROR;
240             }
241         }
242         _p_instance->setErrorInfo(IID_IVLCControl,
243                      libvlc_exception_get_message(&ex));
244         libvlc_exception_clear(&ex);
245         return E_FAIL;
246     }
247     return result;
248 };
249
250 STDMETHODIMP VLCControl::put_Position(float position)
251 {
252     HRESULT result = E_UNEXPECTED;
253     libvlc_instance_t* p_libvlc;
254     result = _p_instance->getVLC(&p_libvlc);
255     if( SUCCEEDED(result) )
256     {
257         libvlc_exception_t ex;
258         libvlc_exception_init(&ex);
259
260         libvlc_media_player_t *p_md;
261         p_md = libvlc_playlist_get_media_player(p_libvlc, &ex);
262         if( ! libvlc_exception_raised(&ex) )
263         {
264             libvlc_media_player_set_position(p_md, position, &ex);
265             libvlc_media_player_release(p_md);
266             if( ! libvlc_exception_raised(&ex) )
267             {
268                 return NOERROR;
269             }
270         }
271         _p_instance->setErrorInfo(IID_IVLCControl,
272                      libvlc_exception_get_message(&ex));
273         libvlc_exception_clear(&ex);
274         return E_FAIL;
275     }
276     return result;
277 };
278
279 STDMETHODIMP VLCControl::get_Time(int *seconds)
280 {
281     if( NULL == seconds )
282         return E_POINTER;
283
284     *seconds = 0;
285     libvlc_instance_t* p_libvlc;
286     HRESULT result = _p_instance->getVLC(&p_libvlc);
287     if( SUCCEEDED(result) )
288     {
289         libvlc_exception_t ex;
290         libvlc_exception_init(&ex);
291
292         libvlc_media_player_t *p_md;
293         p_md = libvlc_playlist_get_media_player(p_libvlc, &ex);
294         if( ! libvlc_exception_raised(&ex) )
295         {
296             *seconds = libvlc_media_player_get_time(p_md, &ex);
297             libvlc_media_player_release(p_md);
298             if( ! libvlc_exception_raised(&ex) )
299             {
300                 return NOERROR;
301             }
302         }
303         _p_instance->setErrorInfo(IID_IVLCControl,
304                      libvlc_exception_get_message(&ex));
305         libvlc_exception_clear(&ex);
306         return E_FAIL;
307     }
308     return result;
309 };
310
311 STDMETHODIMP VLCControl::put_Time(int seconds)
312 {
313     /* setTime function of the plugin sets the time. */
314     _p_instance->setTime(seconds);
315     return NOERROR;
316 };
317
318 STDMETHODIMP VLCControl::shuttle(int seconds)
319 {
320     libvlc_instance_t* p_libvlc;
321     HRESULT result = _p_instance->getVLC(&p_libvlc);
322     if( SUCCEEDED(result) )
323     {
324         libvlc_exception_t ex;
325         libvlc_exception_init(&ex);
326
327         libvlc_media_player_t *p_md;
328         p_md = libvlc_playlist_get_media_player(p_libvlc, &ex);
329         if( ! libvlc_exception_raised(&ex) )
330         {
331             if( seconds < 0 ) seconds = 0;
332             libvlc_media_player_set_time(p_md, (int64_t)seconds, &ex);
333             libvlc_media_player_release(p_md);
334             if( ! libvlc_exception_raised(&ex) )
335             {
336                 return NOERROR;
337             }
338         }
339         _p_instance->setErrorInfo(IID_IVLCControl,
340                      libvlc_exception_get_message(&ex));
341         libvlc_exception_clear(&ex);
342         return E_FAIL;
343     }
344     return result;
345
346 };
347
348 STDMETHODIMP VLCControl::fullscreen(void)
349 {
350     HRESULT result = E_UNEXPECTED;
351     if( _p_instance->isRunning() )
352     {
353         libvlc_instance_t *p_libvlc;
354         result = _p_instance->getVLC(&p_libvlc);
355         if( SUCCEEDED(result) )
356         {
357             if( libvlc_playlist_isplaying(p_libvlc, NULL) )
358             {
359                 libvlc_media_player_t *p_md =
360                     libvlc_playlist_get_media_player(p_libvlc, NULL);
361                 if( p_md )
362                 {
363                     libvlc_toggle_fullscreen(p_md, NULL);
364                     libvlc_media_player_release(p_md);
365                 }
366             }
367         }
368     }
369     return result;
370 };
371
372 STDMETHODIMP VLCControl::get_Length(int *seconds)
373 {
374     if( NULL == seconds )
375         return E_POINTER;
376     *seconds = 0;
377
378     libvlc_instance_t* p_libvlc;
379     HRESULT result = _p_instance->getVLC(&p_libvlc);
380     if( SUCCEEDED(result) )
381     {
382         libvlc_exception_t ex;
383         libvlc_exception_init(&ex);
384
385         libvlc_media_player_t *p_md;
386         p_md = libvlc_playlist_get_media_player(p_libvlc, &ex);
387         if( ! libvlc_exception_raised(&ex) )
388         {
389             *seconds = (double)libvlc_media_player_get_length(p_md, &ex);
390             libvlc_media_player_release(p_md);
391             if( ! libvlc_exception_raised(&ex) )
392             {
393                 return NOERROR;
394             }
395         }
396         _p_instance->setErrorInfo(IID_IVLCControl,
397                      libvlc_exception_get_message(&ex));
398         libvlc_exception_clear(&ex);
399         return E_FAIL;
400     }
401     return result;
402
403 };
404
405 STDMETHODIMP VLCControl::playFaster(void)
406 {
407     int32_t rate = 2;
408
409     HRESULT result = E_UNEXPECTED;
410     if( !_p_instance->isRunning() )
411         return result;
412
413     libvlc_instance_t* p_libvlc;
414     result = _p_instance->getVLC(&p_libvlc);
415     if( SUCCEEDED(result) )
416     {
417         libvlc_exception_t ex;
418         libvlc_exception_init(&ex);
419
420         libvlc_media_player_t *p_md;
421         p_md = libvlc_playlist_get_media_player(p_libvlc, &ex);
422         if( ! libvlc_exception_raised(&ex) )
423         {
424             libvlc_media_player_set_rate(p_md, rate, &ex);
425             libvlc_media_player_release(p_md);
426             if( ! libvlc_exception_raised(&ex) )
427             {
428                 return NOERROR;
429             }
430         }
431         _p_instance->setErrorInfo(IID_IVLCControl,
432                      libvlc_exception_get_message(&ex));
433         libvlc_exception_clear(&ex);
434         return E_FAIL;
435     }
436     return result;
437 };
438
439 STDMETHODIMP VLCControl::playSlower(void)
440 {
441     float rate = 0.5;
442
443     HRESULT result = E_UNEXPECTED;
444     if( !_p_instance->isRunning() )
445         return result;
446
447     libvlc_instance_t* p_libvlc;
448     result = _p_instance->getVLC(&p_libvlc);
449     if( SUCCEEDED(result) )
450     {
451         libvlc_exception_t ex;
452         libvlc_exception_init(&ex);
453
454         libvlc_media_player_t *p_md;
455         p_md = libvlc_playlist_get_media_player(p_libvlc, &ex);
456         if( ! libvlc_exception_raised(&ex) )
457         {
458             libvlc_media_player_set_rate(p_md, rate, &ex);
459             libvlc_media_player_release(p_md);
460             if( ! libvlc_exception_raised(&ex) )
461             {
462                 return NOERROR;
463             }
464         }
465         _p_instance->setErrorInfo(IID_IVLCControl,
466                      libvlc_exception_get_message(&ex));
467         libvlc_exception_clear(&ex);
468         return E_FAIL;
469     }
470     return result;
471 };
472
473 STDMETHODIMP VLCControl::get_Volume(int *volume)
474 {
475     if( NULL == volume )
476         return E_POINTER;
477
478     *volume  = _p_instance->getVolume();
479     return NOERROR;
480 };
481
482 STDMETHODIMP VLCControl::put_Volume(int volume)
483 {
484     _p_instance->setVolume(volume);
485     return NOERROR;
486 };
487
488 STDMETHODIMP VLCControl::toggleMute(void)
489 {
490     libvlc_instance_t* p_libvlc;
491     HRESULT result = _p_instance->getVLC(&p_libvlc);
492     if( SUCCEEDED(result) )
493     {
494         libvlc_exception_t ex;
495         libvlc_exception_init(&ex);
496
497         libvlc_audio_toggle_mute(p_libvlc, &ex);
498         if( libvlc_exception_raised(&ex) )
499         {
500             _p_instance->setErrorInfo(IID_IVLCControl,
501                          libvlc_exception_get_message(&ex));
502             libvlc_exception_clear(&ex);
503             return E_FAIL;
504         }
505         return NOERROR;
506     }
507     return result;
508 };
509
510 STDMETHODIMP VLCControl::setVariable(BSTR name, VARIANT value)
511 {
512     libvlc_instance_t* p_libvlc;
513     HRESULT result = _p_instance->getVLC(&p_libvlc);
514     if( SUCCEEDED(result) )
515     {
516         _p_instance->setErrorInfo(IID_IVLCControl,
517             "setVariable() is an unsafe interface to use. "
518             "It has been removed because of security implications." );
519     }
520     return E_FAIL;
521 };
522
523 STDMETHODIMP VLCControl::getVariable(BSTR name, VARIANT *value)
524 {
525     libvlc_instance_t* p_libvlc;
526     HRESULT result = _p_instance->getVLC(&p_libvlc);
527     if( SUCCEEDED(result) )
528     {
529         _p_instance->setErrorInfo(IID_IVLCControl,
530             "getVariable() is an unsafe interface to use. "
531             "It has been removed because of security implications." );
532     }
533     return E_FAIL;
534 };
535
536 void VLCControl::FreeTargetOptions(char **cOptions, int cOptionCount)
537 {
538     // clean up
539     if( NULL != cOptions )
540     {
541         for( int pos=0; pos<cOptionCount; ++pos )
542         {
543             char *cOption = cOptions[pos];
544             if( NULL != cOption )
545                 CoTaskMemFree(cOption);
546             else
547                 break;
548         }
549         CoTaskMemFree(cOptions);
550     }
551 };
552
553 static HRESULT parseStringOptions(int codePage, BSTR bstr, char*** cOptions, int *cOptionCount)
554 {
555     HRESULT hr = E_INVALIDARG;
556     if( SysStringLen(bstr) > 0 )
557     {
558         hr = E_OUTOFMEMORY;
559         char *s = CStrFromBSTR(codePage, bstr);
560         char *val = s;
561         if( val )
562         {
563             long capacity = 16;
564             char **options = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
565             if( options )
566             {
567                 int nOptions = 0;
568
569                 char *end = val + strlen(val);
570                 while( val < end )
571                 {
572                     // skip leading blanks
573                     while( (val < end)
574                         && ((*val == ' ' ) || (*val == '\t')) )
575                         ++val;
576
577                     char *start = val;
578                     // skip till we get a blank character
579                     while( (val < end)
580                         && (*val != ' ' )
581                         && (*val != '\t') )
582                     {
583                         char c = *(val++);
584                         if( ('\'' == c) || ('"' == c) )
585                         {
586                             // skip till end of string
587                             while( (val < end) && (*(val++) != c ) );
588                         }
589                     }
590
591                     if( val > start )
592                     {
593                         if( nOptions == capacity )
594                         {
595                             capacity += 16;
596                             char **moreOptions = (char **)CoTaskMemRealloc(options, capacity*sizeof(char*));
597                             if( ! moreOptions )
598                             {
599                                 /* failed to allocate more memory */
600                                 CoTaskMemFree(s);
601                                 /* return what we got so far */
602                                 *cOptionCount = nOptions;
603                                 *cOptions = options;
604                                 return NOERROR;
605                             }
606                             options = moreOptions;
607                         }
608                         *(val++) = '\0';
609                         options[nOptions] = (char *)CoTaskMemAlloc(val-start);
610                         if( options[nOptions] )
611                         {
612                             memcpy(options[nOptions], start, val-start);
613                             ++nOptions;
614                         }
615                         else
616                         {
617                             /* failed to allocate memory */
618                             CoTaskMemFree(s);
619                             /* return what we got so far */
620                             *cOptionCount = nOptions;
621                             *cOptions = options;
622                             return NOERROR;
623                         }
624                     }
625                     else
626                         // must be end of string
627                         break;
628                 }
629                 *cOptionCount = nOptions;
630                 *cOptions = options;
631                 hr = NOERROR;
632             }
633             CoTaskMemFree(s);
634         }
635     }
636     return hr;
637 }
638
639 HRESULT VLCControl::CreateTargetOptions(int codePage, VARIANT *options, char ***cOptions, int *cOptionCount)
640 {
641     HRESULT hr = E_INVALIDARG;
642     if( VT_ERROR == V_VT(options) )
643     {
644         if( DISP_E_PARAMNOTFOUND == V_ERROR(options) )
645         {
646             // optional parameter not set
647             *cOptions = NULL;
648             *cOptionCount = 0;
649             return NOERROR;
650         }
651     }
652     else if( (VT_EMPTY == V_VT(options)) || (VT_NULL == V_VT(options)) )
653     {
654         // null parameter
655         *cOptions = NULL;
656         *cOptionCount = 0;
657         return NOERROR;
658     }
659     else if( VT_DISPATCH == V_VT(options) )
660     {
661         // if object is a collection, retrieve enumerator
662         VARIANT colEnum;
663         V_VT(&colEnum) = VT_UNKNOWN;
664         hr = GetObjectProperty(V_DISPATCH(options), DISPID_NEWENUM, colEnum);
665         if( SUCCEEDED(hr) )
666         {
667             IEnumVARIANT *enumVar;
668             hr = V_UNKNOWN(&colEnum)->QueryInterface(IID_IEnumVARIANT, (LPVOID *)&enumVar);
669             if( SUCCEEDED(hr) )
670             {
671                 long pos = 0;
672                 long capacity = 16;
673                 VARIANT option;
674
675                 *cOptions = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
676                 if( NULL != *cOptions )
677                 {
678                     ZeroMemory(*cOptions, sizeof(char *)*capacity);
679                     while( SUCCEEDED(hr) && (S_OK == enumVar->Next(1, &option, NULL)) )
680                     {
681                         if( VT_BSTR == V_VT(&option) )
682                         {
683                             char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
684                             (*cOptions)[pos] = cOption;
685                             if( NULL != cOption )
686                             {
687                                 ++pos;
688                                 if( pos == capacity )
689                                 {
690                                     char **moreOptions = (char **)CoTaskMemRealloc(*cOptions, (capacity+16)*sizeof(char *));
691                                     if( NULL != moreOptions )
692                                     {
693                                         ZeroMemory(moreOptions+capacity, sizeof(char *)*16);
694                                         capacity += 16;
695                                         *cOptions = moreOptions;
696                                     }
697                                     else
698                                         hr = E_OUTOFMEMORY;
699                                 }
700                             }
701                             else
702                                 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
703                                     E_OUTOFMEMORY : E_INVALIDARG;
704                         }
705                         else
706                             hr = E_INVALIDARG;
707
708                         VariantClear(&option);
709                     }
710                     *cOptionCount = pos;
711                     if( FAILED(hr) )
712                     {
713                         // free already processed elements
714                         FreeTargetOptions(*cOptions, *cOptionCount);
715                     }
716                 }
717                 else
718                     hr = E_OUTOFMEMORY;
719
720                 enumVar->Release();
721             }
722         }
723         else
724         {
725             // coerce object into a string and parse it
726             VARIANT v_name;
727             VariantInit(&v_name);
728             hr = VariantChangeType(&v_name, options, 0, VT_BSTR);
729             if( SUCCEEDED(hr) )
730             {
731                 hr = parseStringOptions(codePage, V_BSTR(&v_name), cOptions, cOptionCount);
732                 VariantClear(&v_name);
733             }
734         }
735     }
736     else if( V_ISARRAY(options) )
737     {
738         // array parameter
739         SAFEARRAY *array = V_ISBYREF(options) ? *V_ARRAYREF(options) : V_ARRAY(options);
740
741         if( SafeArrayGetDim(array) != 1 )
742             return E_INVALIDARG;
743
744         long lBound = 0;
745         long uBound = 0;
746         SafeArrayGetLBound(array, 1, &lBound);
747         SafeArrayGetUBound(array, 1, &uBound);
748
749         // have we got any options
750         if( uBound >= lBound )
751         {
752             VARTYPE vType;
753             hr = SafeArrayGetVartype(array, &vType);
754             if( FAILED(hr) )
755                 return hr;
756
757             long pos;
758
759             // marshall options into an array of C strings
760             if( VT_VARIANT == vType )
761             {
762                 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
763                 if( NULL == *cOptions )
764                     return E_OUTOFMEMORY;
765
766                 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
767                 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
768                 {
769                     VARIANT option;
770                     hr = SafeArrayGetElement(array, &pos, &option);
771                     if( SUCCEEDED(hr) )
772                     {
773                         if( VT_BSTR == V_VT(&option) )
774                         {
775                             char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
776                             (*cOptions)[pos-lBound] = cOption;
777                             if( NULL == cOption )
778                                 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
779                                     E_OUTOFMEMORY : E_INVALIDARG;
780                         }
781                         else
782                             hr = E_INVALIDARG;
783                         VariantClear(&option);
784                     }
785                 }
786             }
787             else if( VT_BSTR == vType )
788             {
789                 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
790                 if( NULL == *cOptions )
791                     return E_OUTOFMEMORY;
792
793                 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
794                 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
795                 {
796                     BSTR option;
797                     hr = SafeArrayGetElement(array, &pos, &option);
798                     if( SUCCEEDED(hr) )
799                     {
800                         char *cOption = CStrFromBSTR(codePage, option);
801
802                         (*cOptions)[pos-lBound] = cOption;
803                         if( NULL == cOption )
804                             hr = ( SysStringLen(option) > 0 ) ?
805                                 E_OUTOFMEMORY : E_INVALIDARG;
806                         SysFreeString(option);
807                     }
808                 }
809             }
810             else
811             {
812                 // unsupported type
813                 return E_INVALIDARG;
814             }
815
816             *cOptionCount = pos-lBound;
817             if( FAILED(hr) )
818             {
819                 // free already processed elements
820                 FreeTargetOptions(*cOptions, *cOptionCount);
821             }
822         }
823         else
824         {
825             // empty array
826             *cOptions = NULL;
827             *cOptionCount = 0;
828             return NOERROR;
829         }
830     }
831     else if( VT_UNKNOWN == V_VT(options) )
832     {
833         // coerce object into a string and parse it
834         VARIANT v_name;
835         VariantInit(&v_name);
836         hr = VariantChangeType(&v_name, options, 0, VT_BSTR);
837         if( SUCCEEDED(hr) )
838         {
839             hr = parseStringOptions(codePage, V_BSTR(&v_name), cOptions, cOptionCount);
840             VariantClear(&v_name);
841         }
842     }
843     else if( VT_BSTR == V_VT(options) )
844     {
845         hr = parseStringOptions(codePage, V_BSTR(options), cOptions, cOptionCount);
846     }
847     return hr;
848 };
849
850 /*
851 ** use VARIANT rather than a SAFEARRAY as argument type
852 ** for compatibility with some scripting language (JScript)
853 */
854
855 STDMETHODIMP VLCControl::addTarget(BSTR uri, VARIANT options, enum VLCPlaylistMode mode, int position)
856 {
857     if( 0 == SysStringLen(uri) )
858         return E_INVALIDARG;
859
860     libvlc_instance_t *p_libvlc;
861     HRESULT hr = _p_instance->getVLC(&p_libvlc);
862     if( SUCCEEDED(hr) )
863     {
864         char *cUri = CStrFromBSTR(CP_UTF8, uri);
865         if( NULL == cUri )
866             return E_OUTOFMEMORY;
867
868         int cOptionsCount;
869         char **cOptions;
870
871         if( FAILED(CreateTargetOptions(CP_UTF8, &options, &cOptions, &cOptionsCount)) )
872             return E_INVALIDARG;
873
874         libvlc_exception_t ex;
875         libvlc_exception_init(&ex);
876
877         position = libvlc_playlist_add_extended(p_libvlc, cUri, cUri,
878                                                 cOptionsCount,
879                                                 const_cast<const char**>(cOptions),
880                                                 &ex);
881
882         FreeTargetOptions(cOptions, cOptionsCount);
883         CoTaskMemFree(cUri);
884
885         if( libvlc_exception_raised(&ex) )
886         {
887             _p_instance->setErrorInfo(IID_IVLCPlaylist,
888                 libvlc_exception_get_message(&ex));
889             libvlc_exception_clear(&ex);
890
891             if( mode & VLCPlayListAppendAndGo )
892                 _p_instance->fireOnStopEvent();
893             return E_FAIL;
894         }
895
896         if( mode & VLCPlayListAppendAndGo )
897             _p_instance->fireOnPlayEvent();
898         return NOERROR;
899     }
900     return hr;
901 };
902
903 STDMETHODIMP VLCControl::get_PlaylistIndex(int *index)
904 {
905     if( NULL == index )
906         return E_POINTER;
907
908     *index = 0;
909     libvlc_instance_t *p_libvlc;
910     HRESULT result = _p_instance->getVLC(&p_libvlc);
911     if( SUCCEEDED(result) )
912     {
913         libvlc_exception_t ex;
914         libvlc_exception_init(&ex);
915
916         *index = libvlc_playlist_get_current_index(p_libvlc, &ex);
917         if( libvlc_exception_raised(&ex) )
918         {
919             _p_instance->setErrorInfo(IID_IVLCControl,
920                 libvlc_exception_get_message(&ex));
921             libvlc_exception_clear(&ex);
922             return E_FAIL;
923         }
924         return NOERROR;
925     }
926     return result;
927 };
928
929 STDMETHODIMP VLCControl::get_PlaylistCount(int *count)
930 {
931     if( NULL == count )
932         return E_POINTER;
933
934     *count = 0;
935     libvlc_instance_t* p_libvlc;
936     HRESULT result = _p_instance->getVLC(&p_libvlc);
937     if( SUCCEEDED(result) )
938     {
939         libvlc_exception_t ex;
940         libvlc_exception_init(&ex);
941
942         *count = libvlc_playlist_items_count(p_libvlc, &ex);
943         if( libvlc_exception_raised(&ex) )
944         {
945             _p_instance->setErrorInfo(IID_IVLCControl,
946                 libvlc_exception_get_message(&ex));
947             libvlc_exception_clear(&ex);
948             return E_FAIL;
949         }
950         return NOERROR;
951     }
952     return result;
953 };
954
955 STDMETHODIMP VLCControl::playlistNext(void)
956 {
957     libvlc_instance_t* p_libvlc;
958     HRESULT result = _p_instance->getVLC(&p_libvlc);
959     if( SUCCEEDED(result) )
960     {
961         libvlc_exception_t ex;
962         libvlc_exception_init(&ex);
963
964         libvlc_playlist_next(p_libvlc, &ex);
965         if( libvlc_exception_raised(&ex) )
966         {
967             _p_instance->setErrorInfo(IID_IVLCControl,
968                 libvlc_exception_get_message(&ex));
969             libvlc_exception_clear(&ex);
970             return E_FAIL;
971         }
972         return NOERROR;
973     }
974     return result;
975 };
976
977 STDMETHODIMP VLCControl::playlistPrev(void)
978 {
979     libvlc_instance_t* p_libvlc;
980     HRESULT result = _p_instance->getVLC(&p_libvlc);
981     if( SUCCEEDED(result) )
982     {
983         libvlc_exception_t ex;
984         libvlc_exception_init(&ex);
985
986         libvlc_playlist_prev(p_libvlc, &ex);
987         if( libvlc_exception_raised(&ex) )
988         {
989             _p_instance->setErrorInfo(IID_IVLCControl,
990                 libvlc_exception_get_message(&ex));
991             libvlc_exception_clear(&ex);
992             return E_FAIL;
993         }
994         return NOERROR;
995     }
996     return result;
997 };
998
999 STDMETHODIMP VLCControl::playlistClear(void)
1000 {
1001     libvlc_instance_t* p_libvlc;
1002     HRESULT result = _p_instance->getVLC(&p_libvlc);
1003     if( SUCCEEDED(result) )
1004     {
1005         libvlc_exception_t ex;
1006         libvlc_exception_init(&ex);
1007
1008         libvlc_playlist_clear(p_libvlc, &ex);
1009         if( libvlc_exception_raised(&ex) )
1010         {
1011             _p_instance->setErrorInfo(IID_IVLCControl,
1012                 libvlc_exception_get_message(&ex));
1013             libvlc_exception_clear(&ex);
1014             return E_FAIL;
1015         }
1016         return NOERROR;
1017     }
1018     return result;
1019 };
1020
1021 STDMETHODIMP VLCControl::get_VersionInfo(BSTR *version)
1022 {
1023     if( NULL == version )
1024         return E_POINTER;
1025
1026     const char *versionStr = libvlc_get_version();
1027     if( NULL != versionStr )
1028     {
1029         *version = BSTRFromCStr(CP_UTF8, versionStr);
1030         return (NULL == *version) ? E_OUTOFMEMORY : NOERROR;
1031     }
1032     *version = NULL;
1033     return E_FAIL;
1034 };
1035
1036 STDMETHODIMP VLCControl::get_MRL(BSTR *mrl)
1037 {
1038     if( NULL == mrl )
1039         return E_POINTER;
1040
1041     *mrl = SysAllocStringLen(_p_instance->getMRL(),
1042                 SysStringLen(_p_instance->getMRL()));
1043     return NOERROR;
1044 };
1045
1046 STDMETHODIMP VLCControl::put_MRL(BSTR mrl)
1047 {
1048     _p_instance->setMRL(mrl);
1049
1050     return S_OK;
1051 };
1052
1053 STDMETHODIMP VLCControl::get_AutoPlay(VARIANT_BOOL *autoplay)
1054 {
1055     if( NULL == autoplay )
1056         return E_POINTER;
1057
1058     *autoplay = _p_instance->getAutoPlay() ? VARIANT_TRUE: VARIANT_FALSE;
1059     return S_OK;
1060 };
1061
1062 STDMETHODIMP VLCControl::put_AutoPlay(VARIANT_BOOL autoplay)
1063 {
1064     _p_instance->setAutoPlay((VARIANT_FALSE != autoplay) ? TRUE: FALSE);
1065     return S_OK;
1066 };
1067
1068 STDMETHODIMP VLCControl::get_AutoLoop(VARIANT_BOOL *autoloop)
1069 {
1070     if( NULL == autoloop )
1071         return E_POINTER;
1072
1073     *autoloop = _p_instance->getAutoLoop() ? VARIANT_TRUE: VARIANT_FALSE;
1074     return S_OK;
1075 };
1076
1077 STDMETHODIMP VLCControl::put_AutoLoop(VARIANT_BOOL autoloop)
1078 {
1079     _p_instance->setAutoLoop((VARIANT_FALSE != autoloop) ? TRUE: FALSE);
1080     return S_OK;
1081 };