📄 typelib.c
字号:
/*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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.
* So when you release the dispinterface, you delete the vtable-interface structures
* as well... fortunately, clean up of structures is not implemented.
*
* - locale stuff is partially implemented but hasn't been tested.
*
* - typelib file is still read in its entirety, but it is released now.
* - some garbage is read from function names on some very rare occasions.
*
* --------------------------------------------------------------------------------------
* Known problems left from previous implementation (1999, Rein Klazes) :
*
* -. Data structures are straightforward, but slow for look-ups.
* -. (related) nothing is hashed
* -. there are a number of stubs in ITypeLib and ITypeInfo interfaces. Most
* of them I don't know yet how to implement them.
* -. Most error return values are just guessed not checked with windows
* behaviour.
* -. didn't bother with a c++ interface
* -. lousy fatal error handling
* -. some methods just return pointers to internal data structures, this is
* partly laziness, partly I want to check how windows does it.
*
*/
#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"
WINE_DEFAULT_DEBUG_CHANNEL(ole);
WINE_DECLARE_DEBUG_CHANNEL(typelib);
/* The OLE Automation ProxyStub Interface Class (aka Typelib Marshaler) */
const GUID CLSID_PSOAInterface = { 0x00020424, 0, 0, { 0xC0, 0, 0, 0, 0, 0, 0, 0x46 } };
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
/****************************************************************************
* 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
/* 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[] = {'\\','%','u','.','%','u',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%lx, %p)\n", debugstr_guid(guid), wMaj, wMin, lcid, path);
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%08lx\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) ||
((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;
}
break;
case REGKIND_NONE:
break;
}
TRACE(" returns %08lx\n",res);
return res;
}
/******************************************************************************
* LoadRegTypeLib [OLEAUT32.162]
*
* Loads a registered type library.
*
* PARAMS
* rguid [I] GUID of the registered type library.
* wVerMajor [I] major version.
* wVerMinor [I] minor version.
* lcid [I] locale ID.
* ppTLib [O] pointer that receives an ITypeLib object on success.
*
* RETURNS
* Success: S_OK.
* Failure: Any HRESULT code returned from QueryPathOfRegTypeLib or
* LoadTypeLib.
*/
HRESULT WINAPI LoadRegTypeLib(
REFGUID rguid,
WORD wVerMajor,
WORD wVerMinor,
LCID lcid,
ITypeLib **ppTLib)
{
BSTR bstr=NULL;
HRESULT res=QueryPathOfRegTypeLib( rguid, wVerMajor, wVerMinor, lcid, &bstr);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -