]> git.sesse.net Git - vlc/blob - activex/utils.cpp
* modules/misc/xml/xtag.c: fixed memory leak.
[vlc] / activex / utils.cpp
1 /*****************************************************************************
2  * utils.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 "utils.h"
24
25 /*
26 ** conversion facilities
27 */
28
29 using namespace std;
30
31 char *CStrFromBSTR(UINT 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 *)CoTaskMemAlloc(mblen+1);
41             ZeroMemory(buffer, mblen+1);
42             if( WideCharToMultiByte(codePage, 0, bstr, len, buffer, mblen, NULL, NULL) )
43             {
44                 buffer[mblen] = '\0';
45                 return buffer;
46             }
47         }
48     }
49     return NULL;
50 };
51
52 BSTR BSTRFromCStr(UINT codePage, LPCSTR s)
53 {
54     int wideLen = MultiByteToWideChar(codePage, 0, s, -1, NULL, 0);
55     if( wideLen > 0 )
56     {
57         WCHAR* wideStr = (WCHAR*)CoTaskMemAlloc(wideLen*sizeof(WCHAR));
58         if( NULL != wideStr )
59         {
60             BSTR bstr;
61
62             ZeroMemory(wideStr, wideLen*sizeof(WCHAR));
63             MultiByteToWideChar(codePage, 0, s, -1, wideStr, wideLen);
64             bstr = SysAllocStringLen(wideStr, wideLen);
65             CoTaskMemFree(wideStr);
66
67             return bstr;
68         }
69     }
70     return NULL;
71 };
72
73 char *CStrFromGUID(REFGUID clsid)
74 {
75     LPOLESTR oleStr;
76
77     if( FAILED(StringFromIID(clsid, &oleStr)) )
78         return NULL;
79
80 #ifdef OLE2ANSI
81     return (LPCSTR)oleStr;
82 #else
83     char *pct_CLSID = NULL;
84     size_t len = WideCharToMultiByte(CP_ACP, 0, oleStr, -1, NULL, 0, NULL, NULL);
85     if( len > 0 )
86     {
87         pct_CLSID = (char *)CoTaskMemAlloc(len);
88         WideCharToMultiByte(CP_ACP, 0, oleStr, -1, pct_CLSID, len, NULL, NULL);
89     }
90     CoTaskMemFree(oleStr);
91     return pct_CLSID;
92 #endif
93 };
94
95 /*
96 **  properties
97 */
98
99 HRESULT GetObjectProperty(LPUNKNOWN object, DISPID dispID, VARIANT& v)
100 {
101     IDispatch *pDisp;
102     HRESULT hr = object->QueryInterface(IID_IDispatch, (LPVOID *)&pDisp);
103     if( SUCCEEDED(hr) )
104     {
105         DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
106         VARIANT vres;
107         VariantInit(&vres);
108         hr = pDisp->Invoke(dispID, IID_NULL, LOCALE_USER_DEFAULT,
109                 DISPATCH_PROPERTYGET, &dispparamsNoArgs, &vres, NULL, NULL);
110         if( SUCCEEDED(hr) )
111         {
112             if( V_VT(&v) != V_VT(&vres) )
113             {
114                 hr = VariantChangeType(&v, &vres, 0, V_VT(&v));
115                 VariantClear(&vres);
116             }
117             else
118             {
119                 v = vres;
120             }
121         }
122         pDisp->Release();
123     }
124     return hr;
125 };
126
127 HDC CreateDevDC(DVTARGETDEVICE *ptd)
128 {
129         HDC hdc=NULL;
130         if( NULL == ptd )
131     {
132                 hdc = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
133         }
134     else
135     {
136         LPDEVNAMES lpDevNames;
137         LPDEVMODE lpDevMode;
138         LPTSTR lpszDriverName;
139         LPTSTR lpszDeviceName;
140         LPTSTR lpszPortName;
141
142         lpDevNames = (LPDEVNAMES) ptd; // offset for size field
143
144         if (ptd->tdExtDevmodeOffset == 0)
145         {
146             lpDevMode = NULL;
147         }
148         else
149         {
150             lpDevMode  = (LPDEVMODE) ((LPTSTR)ptd + ptd->tdExtDevmodeOffset);
151         }
152
153         lpszDriverName = (LPTSTR) lpDevNames + ptd->tdDriverNameOffset;
154         lpszDeviceName = (LPTSTR) lpDevNames + ptd->tdDeviceNameOffset;
155         lpszPortName   = (LPTSTR) lpDevNames + ptd->tdPortNameOffset;
156
157         hdc = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, lpDevMode);
158     }
159         return hdc;
160 };
161
162 #define HIMETRIC_PER_INCH 2540
163
164 void DPFromHimetric(HDC hdc, LPPOINT pt, int count)
165 {
166     LONG lpX = GetDeviceCaps(hdc, LOGPIXELSX);
167     LONG lpY = GetDeviceCaps(hdc, LOGPIXELSY);
168     while( count-- )
169     {
170         pt->x = pt->x*lpX/HIMETRIC_PER_INCH;
171         pt->y = pt->y*lpY/HIMETRIC_PER_INCH;
172         ++pt;
173     }
174 };
175
176 void HimetricFromDP(HDC hdc, LPPOINT pt, int count)
177 {
178     LONG lpX = GetDeviceCaps(hdc, LOGPIXELSX);
179     LONG lpY = GetDeviceCaps(hdc, LOGPIXELSY);
180     while( count-- )
181     {
182         pt->x = pt->x*HIMETRIC_PER_INCH/lpX;
183         pt->y = pt->y*HIMETRIC_PER_INCH/lpY;
184         ++pt;
185     }
186 };
187