]> git.sesse.net Git - vlc/blob - activex/utils.cpp
compilation fixes for MSVC
[vlc] / activex / utils.cpp
1 /*****************************************************************************
2  * utils.cpp: ActiveX control for VLC
3  *****************************************************************************
4  * Copyright (C) 2005 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 #include "utils.h"
24
25 /*
26 ** conversion facilities
27 */
28
29 using namespace std;
30
31 char *CStrFromBSTR(int codePage, BSTR bstr)
32 {
33     UINT len = SysStringLen(bstr);
34     if( len > 0 )
35     {
36         size_t mblen = WideCharToMultiByte(codePage,
37                 0, bstr, len, NULL, 0, NULL, NULL);
38         if( mblen > 0 )
39         {
40             char *buffer = (char *)malloc(mblen+1);
41             ZeroMemory(buffer, mblen+1);
42             if( WideCharToMultiByte(codePage, 0, bstr, len, buffer, mblen, NULL, NULL) )
43                 return buffer;
44         }
45     }
46     return NULL;
47 };
48
49 BSTR BSTRFromCStr(int codePage, const char *s)
50 {
51     int wideLen = MultiByteToWideChar(codePage, 0, s, -1, NULL, 0);
52     if( wideLen )
53     {
54         WCHAR* wideStr = (WCHAR*)malloc(wideLen*sizeof(WCHAR));
55         if( NULL != wideStr )
56         {
57             BSTR bstr;
58
59             ZeroMemory(wideStr, wideLen*sizeof(WCHAR));
60             MultiByteToWideChar(codePage, 0, s, -1, wideStr, wideLen);
61             bstr = SysAllocString(wideStr);
62             free(wideStr);
63
64             return bstr;
65         }
66     }
67     return NULL;
68 };
69
70 /*
71 **  properties
72 */
73
74 HRESULT GetObjectProperty(LPUNKNOWN object, DISPID dispID, VARIANT& v)
75 {
76     IDispatch *pDisp;
77     HRESULT hr = object->QueryInterface(IID_IDispatch, (LPVOID *)&pDisp);
78     if( SUCCEEDED(hr) )
79     {
80         DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
81         VARIANT vres;
82         hr = pDisp->Invoke(dispID, IID_NULL, LOCALE_USER_DEFAULT,
83                 DISPATCH_PROPERTYGET, &dispparamsNoArgs, &vres, NULL, NULL);
84         if( SUCCEEDED(hr) )
85         {
86             if( V_VT(&v) != V_VT(&vres) )
87             {
88                 hr = VariantChangeType(&v, &vres, 0, V_VT(&v));
89                 VariantClear(&vres);
90             }
91             else
92             {
93                 v = vres;
94             }
95         }
96         pDisp->Release();
97     }
98     return hr;
99 };
100
101 HDC CreateDevDC(DVTARGETDEVICE *ptd)
102 {
103         HDC hdc=NULL;
104         LPDEVNAMES lpDevNames;
105         LPDEVMODE lpDevMode;
106         LPTSTR lpszDriverName;
107         LPTSTR lpszDeviceName;
108         LPTSTR lpszPortName;
109
110         if (ptd == NULL) {
111                 hdc = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
112                 goto errReturn;
113         }
114
115         lpDevNames = (LPDEVNAMES) ptd; // offset for size field
116
117         if (ptd->tdExtDevmodeOffset == 0) {
118                 lpDevMode = NULL;
119         }else{
120                 lpDevMode  = (LPDEVMODE) ((LPTSTR)ptd + ptd->tdExtDevmodeOffset);
121         }
122
123         lpszDriverName = (LPTSTR) lpDevNames + ptd->tdDriverNameOffset;
124         lpszDeviceName = (LPTSTR) lpDevNames + ptd->tdDeviceNameOffset;
125         lpszPortName   = (LPTSTR) lpDevNames + ptd->tdPortNameOffset;
126
127         hdc = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, lpDevMode);
128
129 errReturn:
130         return hdc;
131 };
132
133