Inside com Reading Notes-hresult, guid, registry, and other details

Source: Internet
Author: User
Document directory
  • Use of hresult values
  • Guid declaration and definition
  • Comparison of guid
  • Registry Organization
  • CLSID keyword Structure
  • Other details about the Registry
  • Progid
  • Self-registration
  • Component category
1. hresult

Hresult is a 32-bit value that can be divided into three fields,

 

Note: s_false is defined as 1 and s_ OK is defined as 0, which is the opposite of C/C ++'s principle.

The device code contained in the 15 BITs between 16 and 30 in the hresult value. When the device code is identified, the operating system part of the code returned in the hresult can be returned. Currently defined:

Use of hresult values

 

  • There are multiple successful codes and multiple failed codes.

 

The status code returned by a function in various cases usually contains multiple successful codes and multiple failed codes. This is why succeeded and failed macros are used. Generally, the hresult value is not directly compared with a successful code (such as s_ OK) | the failed code (such as s_faile) to determine whether a function is successful. Use the secceeded and failed macros to determine whether the function is successful.

 

  • Failed code may change

 

Others may define new hresult error codes, and the customer program may encounter corresponding errors. The component used by the customer may change, so the error code returned by the component may also change.

2. guid

Why use guid instead of a long integer? The essence of the problem is not how many interfaces can be identified, but how to ensure that each interface identifier is unique.

Guid declaration and definition

You can use the define_guid macro in objbase. h to define

# Ifndefinitguid

# Definedefine_guid (name, l, W1, W2, B1, B2, B3, B4, B5, B6, B7, B8 )\

Extern_c const guid far name

# Else

 

# Definedefine_guid (name, l, W1, W2, B1, B2, B3, B4, B5, B6, B7, B8 )\

Extern_c const guid name \

= {L, W1, W2, {B1, B2, B3, B4, B5, B6, B7, B8 }}

# Endif // initguid

The define_guid defined in objbase. H will generate the following similar output:

Extern_c const guid far iid_ix

However, if objbase. h contains initguid. H, the code generated by define_guid is as follows:

// {32bb8320-b41b-11cf-a6bb-0080c7b2d682}

Extern "C" const IID iid_ix =

{0x32bb8320, 0xb41b, 0x11cf,

{0xa6, 0xbb, 0x0, 0x80, 0xc7, 0xb2, 0xd6, 0x82 }};

Comparison of guid

An = Operator is defined in objbase. H. You can also use the isequalguid, isequaliid, and isequalclsid.

 

3. Windows Registry

Registry Organization

A registry is a layered structure composed of many elements. Each element is called a keyword. Each keyword can contain a series of subkeywords. A series of named values and/or an untitled value. A subkeyword can also contain its subkeywords and values, but its values cannot contain subkeywords or values. Each value has its corresponding type. Registry structure:

 

CLSID keyword Structure

Com only uses one branch of the Registry: heky_classes_root. Under this keyword, you can find the CLSID keyword. The following CLSID keyword indicates the CLSID of all components installed in the system. The Registry CLSID is a string in the following format: {32bb8320-b41b-11cf-a6bb-0080c7b2d682 }, A subkeyword inprocserver32when the keyword is currently concerned. The default value of this keyword is the name of the DLL file where the component is located.

Other details about the Registry

Start with hkey_classes_root and list the file extensions registered by various applications. After the extension, you can see many other names. Most of these names are called progid, indicating the identifier defined by the programmer.

 

  • Appid ----- The subkeyword is used to map an appid (Application ID) to a remote server name. Distributed COM will use this keyword
  • Component category-this branch of the registry can map the catid (Component category ID) to a specific component category.
  • Interface ---- maps an IID to information related to an interface. This information is mainly about convenient cross-process Interface Usage of apartment buildings.
  • License ----- license information of the COM component authorized when the license is maintained
  • Typelib ----- information about the parameters used by the interface member functions when Keywords of the Type Library are maintained.

 

 

Progid

Progid indicates a programmer's easy-to-remember name specified by the programmer for a CLSID.

 

  • Progid naming convention

 

Progid has the following format:

<Program>. <component>. <version>

For example: Visio. drawing.4

 

  • Progid registry format

 

 

 

  • Conversion from progid to CLSID

 

CLSID;

Clsidfromprogid (L "primer. xxx.1", & CLSID );

 

Self-registration

DLL outputs two functions:

# Definestdapi extern_c hresultstdapicalltype

 

Stdapi dllregisterserver ();

Stdapi dllunregisterserver ();

You can use regsve32.exe to register a component. It actually registers the component by calling the above functions.

 

  • Implementation of dllregisterserver

 

You can directly call the registry function. There are many functions in the API that want to add new keywords to the Registry and delete a keyword.

Regopenkeyex

Regcreatekeyex

Regsetvalueex

Regenumkeyex

Regdeletekey

Regclosekey

Component category

A component class is actually an interface set, which is assigned to a guid. This GUID is called a catid. If a component implements all interfaces of a component category, it can be registered as a member of the component category. You can select a component that belongs to a specific component category from the Registry to find the required component.

4. com Library

 

  • Com library Initialization

 

Before using other functions of the COM library, the process must call coinitialize to initialize the com library function. When a process does not require the com function, it must call couninitialize.

 

  • Memory Management

 

Cotaskmemalloc ()

Cotaskmemfree ();

 

  • Convert string to guid

 

The clsid string in the registry. Therefore, some functions are required to complete the conversion from CLSID to string.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.