?? typelib.c
字號(hào):
/*
* TYPELIB
*
* Copyright 1997 Marcus Meissner
* 1999 Rein Klazes
* 2000 Francois Jacques
* 2001 Huw D M Davies for CodeWeavers
* 2005 Robert Shearman, for CodeWeavers
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* --------------------------------------------------------------------------------------
* Known problems (2000, Francois Jacques)
*
* - Tested using OLEVIEW (Platform SDK tool) only.
*
* - dual interface dispinterfaces. vtable-interface ITypeInfo instances are
* creating by doing a straight copy of the dispinterface instance and just changing
* its typekind. Pointed structures aren't copied - only the address of the pointers.
*
* - locale stuff is partially implemented but hasn't been tested.
*
* - typelib file is still read in its entirety, but it is released now.
*
* --------------------------------------------------------------------------------------
* Known problems left from previous implementation (1999, Rein Klazes) :
*
* -. Data structures are straightforward, but slow for look-ups.
* -. (related) nothing is hashed
* -. Most error return values are just guessed not checked with windows
* behaviour.
* -. lousy fatal error handling
*
*/
#include "config.h"
#include "wine/port.h"
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <ctype.h>
#define COBJMACROS
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
#include "winerror.h"
#include "windef.h"
#include "winbase.h"
#include "winnls.h"
#include "winreg.h"
#include "winuser.h"
#include "wine/unicode.h"
#include "objbase.h"
#include "typelib.h"
#include "wine/debug.h"
#include "variant.h"
#include "wine/list.h"
WINE_DEFAULT_DEBUG_CHANNEL(ole);
WINE_DECLARE_DEBUG_CHANNEL(typelib);
static HRESULT typedescvt_to_variantvt(ITypeInfo *tinfo, const TYPEDESC *tdesc, VARTYPE *vt);
static HRESULT TLB_AllocAndInitVarDesc(const VARDESC *src, VARDESC **dest_ptr);
/****************************************************************************
* FromLExxx
*
* Takes p_iVal (which is in little endian) and returns it
* in the host machine's byte order.
*/
#ifdef WORDS_BIGENDIAN
static WORD FromLEWord(WORD p_iVal)
{
return (((p_iVal & 0x00FF) << 8) |
((p_iVal & 0xFF00) >> 8));
}
static DWORD FromLEDWord(DWORD p_iVal)
{
return (((p_iVal & 0x000000FF) << 24) |
((p_iVal & 0x0000FF00) << 8) |
((p_iVal & 0x00FF0000) >> 8) |
((p_iVal & 0xFF000000) >> 24));
}
#else
#define FromLEWord(X) (X)
#define FromLEDWord(X) (X)
#endif
#define DISPATCH_HREF_OFFSET 0x01000000
#define DISPATCH_HREF_MASK 0xff000000
/****************************************************************************
* FromLExxx
*
* Fix byte order in any structure if necessary
*/
#ifdef WORDS_BIGENDIAN
static void FromLEWords(void *p_Val, int p_iSize)
{
WORD *Val = p_Val;
p_iSize /= sizeof(WORD);
while (p_iSize) {
*Val = FromLEWord(*Val);
Val++;
p_iSize--;
}
}
static void FromLEDWords(void *p_Val, int p_iSize)
{
DWORD *Val = p_Val;
p_iSize /= sizeof(DWORD);
while (p_iSize) {
*Val = FromLEDWord(*Val);
Val++;
p_iSize--;
}
}
#else
#define FromLEWords(X,Y) /*nothing*/
#define FromLEDWords(X,Y) /*nothing*/
#endif
/*
* Find a typelib key which matches a requested maj.min version.
*/
static BOOL find_typelib_key( REFGUID guid, WORD *wMaj, WORD *wMin )
{
static const WCHAR typelibW[] = {'T','y','p','e','l','i','b','\\',0};
WCHAR buffer[60];
char key_name[16];
DWORD len, i;
INT best_min = -1;
HKEY hkey;
memcpy( buffer, typelibW, sizeof(typelibW) );
StringFromGUID2( guid, buffer + strlenW(buffer), 40 );
if (RegOpenKeyExW( HKEY_CLASSES_ROOT, buffer, 0, KEY_READ, &hkey ) != ERROR_SUCCESS)
return FALSE;
len = sizeof(key_name);
i = 0;
while (RegEnumKeyExA(hkey, i++, key_name, &len, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
INT v_maj, v_min;
if (sscanf(key_name, "%x.%x", &v_maj, &v_min) == 2)
{
TRACE("found %s: %x.%x\n", debugstr_w(buffer), v_maj, v_min);
if (*wMaj == v_maj)
{
if (*wMin == v_min)
{
best_min = v_min;
break; /* exact match */
}
if (v_min > best_min) best_min = v_min;
}
}
len = sizeof(key_name);
}
RegCloseKey( hkey );
if (best_min >= 0)
{
*wMin = best_min;
return TRUE;
}
return FALSE;
}
/* get the path of a typelib key, in the form "Typelib\\<guid>\\<maj>.<min>" */
/* buffer must be at least 60 characters long */
static WCHAR *get_typelib_key( REFGUID guid, WORD wMaj, WORD wMin, WCHAR *buffer )
{
static const WCHAR TypelibW[] = {'T','y','p','e','l','i','b','\\',0};
static const WCHAR VersionFormatW[] = {'\\','%','x','.','%','x',0};
memcpy( buffer, TypelibW, sizeof(TypelibW) );
StringFromGUID2( guid, buffer + strlenW(buffer), 40 );
sprintfW( buffer + strlenW(buffer), VersionFormatW, wMaj, wMin );
return buffer;
}
/* get the path of an interface key, in the form "Interface\\<guid>" */
/* buffer must be at least 50 characters long */
static WCHAR *get_interface_key( REFGUID guid, WCHAR *buffer )
{
static const WCHAR InterfaceW[] = {'I','n','t','e','r','f','a','c','e','\\',0};
memcpy( buffer, InterfaceW, sizeof(InterfaceW) );
StringFromGUID2( guid, buffer + strlenW(buffer), 40 );
return buffer;
}
/* get the lcid subkey for a typelib, in the form "<lcid>\\<syskind>" */
/* buffer must be at least 16 characters long */
static WCHAR *get_lcid_subkey( LCID lcid, SYSKIND syskind, WCHAR *buffer )
{
static const WCHAR LcidFormatW[] = {'%','l','x','\\',0};
static const WCHAR win16W[] = {'w','i','n','1','6',0};
static const WCHAR win32W[] = {'w','i','n','3','2',0};
sprintfW( buffer, LcidFormatW, lcid );
switch(syskind)
{
case SYS_WIN16: strcatW( buffer, win16W ); break;
case SYS_WIN32: strcatW( buffer, win32W ); break;
default:
TRACE("Typelib is for unsupported syskind %i\n", syskind);
return NULL;
}
return buffer;
}
static int TLB_ReadTypeLib(LPCWSTR pszFileName, LPWSTR pszPath, UINT cchPath, ITypeLib2 **ppTypeLib);
/****************************************************************************
* QueryPathOfRegTypeLib [OLEAUT32.164]
*
* Gets the path to a registered type library.
*
* PARAMS
* guid [I] referenced guid
* wMaj [I] major version
* wMin [I] minor version
* lcid [I] locale id
* path [O] path of typelib
*
* RETURNS
* Success: S_OK.
* Failure: If the type library is not registered then TYPE_E_LIBNOTREGISTERED
* or TYPE_E_REGISTRYACCESS if the type library registration key couldn't be
* opened.
*/
HRESULT WINAPI QueryPathOfRegTypeLib(
REFGUID guid,
WORD wMaj,
WORD wMin,
LCID lcid,
LPBSTR path )
{
HRESULT hr = TYPE_E_LIBNOTREGISTERED;
LCID myLCID = lcid;
HKEY hkey;
WCHAR buffer[60];
WCHAR Path[MAX_PATH];
LONG res;
TRACE_(typelib)("(%s, %x.%x, 0x%x, %p)\n", debugstr_guid(guid), wMaj, wMin, lcid, path);
if (!find_typelib_key( guid, &wMaj, &wMin )) return TYPE_E_LIBNOTREGISTERED;
get_typelib_key( guid, wMaj, wMin, buffer );
res = RegOpenKeyExW( HKEY_CLASSES_ROOT, buffer, 0, KEY_READ, &hkey );
if (res == ERROR_FILE_NOT_FOUND)
{
TRACE_(typelib)("%s not found\n", debugstr_w(buffer));
return TYPE_E_LIBNOTREGISTERED;
}
else if (res != ERROR_SUCCESS)
{
TRACE_(typelib)("failed to open %s for read access\n", debugstr_w(buffer));
return TYPE_E_REGISTRYACCESS;
}
while (hr != S_OK)
{
LONG dwPathLen = sizeof(Path);
get_lcid_subkey( myLCID, SYS_WIN32, buffer );
if (RegQueryValueW(hkey, buffer, Path, &dwPathLen))
{
if (!lcid)
break;
else if (myLCID == lcid)
{
/* try with sub-langid */
myLCID = SUBLANGID(lcid);
}
else if ((myLCID == SUBLANGID(lcid)) && myLCID)
{
/* try with system langid */
myLCID = 0;
}
else
{
break;
}
}
else
{
*path = SysAllocString( Path );
hr = S_OK;
}
}
RegCloseKey( hkey );
TRACE_(typelib)("-- 0x%08x\n", hr);
return hr;
}
/******************************************************************************
* CreateTypeLib [OLEAUT32.160] creates a typelib
*
* RETURNS
* Success: S_OK
* Failure: Status
*/
HRESULT WINAPI CreateTypeLib(
SYSKIND syskind, LPCOLESTR szFile, ICreateTypeLib** ppctlib
) {
FIXME("(%d,%s,%p), stub!\n",syskind,debugstr_w(szFile),ppctlib);
return E_FAIL;
}
/******************************************************************************
* LoadTypeLib [OLEAUT32.161]
*
* Loads a type library
*
* PARAMS
* szFile [I] Name of file to load from.
* pptLib [O] Pointer that receives ITypeLib object on success.
*
* RETURNS
* Success: S_OK
* Failure: Status
*
* SEE
* LoadTypeLibEx, LoadRegTypeLib, CreateTypeLib.
*/
HRESULT WINAPI LoadTypeLib(const OLECHAR *szFile, ITypeLib * *pptLib)
{
TRACE("(%s,%p)\n",debugstr_w(szFile), pptLib);
return LoadTypeLibEx(szFile, REGKIND_DEFAULT, pptLib);
}
/******************************************************************************
* LoadTypeLibEx [OLEAUT32.183]
*
* Loads and optionally registers a type library
*
* RETURNS
* Success: S_OK
* Failure: Status
*/
HRESULT WINAPI LoadTypeLibEx(
LPCOLESTR szFile, /* [in] Name of file to load from */
REGKIND regkind, /* [in] Specify kind of registration */
ITypeLib **pptLib) /* [out] Pointer to pointer to loaded type library */
{
WCHAR szPath[MAX_PATH+1];
HRESULT res;
TRACE("(%s,%d,%p)\n",debugstr_w(szFile), regkind, pptLib);
*pptLib = NULL;
res = TLB_ReadTypeLib(szFile, szPath, MAX_PATH + 1, (ITypeLib2**)pptLib);
if (SUCCEEDED(res))
switch(regkind)
{
case REGKIND_DEFAULT:
/* don't register typelibs supplied with full path. Experimentation confirms the following */
if (((szFile[0] == '\\') && (szFile[1] == '\\')) ||
(szFile[0] && (szFile[1] == ':'))) break;
/* else fall-through */
case REGKIND_REGISTER:
if (!SUCCEEDED(res = RegisterTypeLib(*pptLib, (LPOLESTR)szPath, NULL)))
{
IUnknown_Release(*pptLib);
*pptLib = 0;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -