标签:
?
MFC introduced another binary sharing mechanism with MFC extension DLLs. But these are even more restrictive - you can only use them from an MFC app.
COM solves all these problems by defining a?binary standard, meaning that COM specifies that the binary modules (the DLLs and EXEs) must be compiled to match a specific structure. The standard also specifies exactly how COM objects must be organized in memory. The binaries must also not depend on any feature of any programming language (such as name decoration in C++). Once that‘s done, the modules can be accessed easily from any programming language. A binary standard puts the burden of compatibility on the compiler that produces the binaries, which makes it much easier for the folks who come along later and need to use those binaries.
?
Creating a new object
Deleting objects
Now, in between those two stages of creating and destroying the object, you actually have to?use?it. When you create a COM object, you tell the COM library what interface you need. If the object is created successfully, the COM library returns a pointer to the requested interface. You can then call methods through that pointer, just as if it were a pointer to a regular C++ object.
I‘ve already shown some simple error handling, using the?SUCCEEDED?and?FAILED?macros. Now I‘ll give some more details on what to do with the?HRESULTs returned from COM methods.
An?HRESULT?is a 32-bit signed integer, with nonnegative values indicating success, and negative values indicating failure. An?HRESULT?has three fields: the severity bit (to indicate success or failure), the facility code, and the status code. The "facility" indicates what component or program the?HRESULT?is coming from. Microsoft assigns facility codes to the various components, for example COM has one, the Task Scheduler has one, and so on. The "code" is a 16-bit field that has no intrinsic meaning; the codes are just an arbitrary association between a number and a meaning, just like the values returned by?GetLastError().
If you look up error codes in the?winerror.h?file, you‘ll see a lot of?HRESULTs listed, with the naming convention [facility]_[severity]_[description]. Generic?HRESULTs that can be returned by any component (likeE_OUTOFMEMORY) have no facility in their name. Examples:
Fortunately, there are easier ways to determine the meaning of an?HRESULT?than looking through?winerror.h.HRESULTs for built-in facilities can be looked up with the Error Lookup tool. For example, say you forgot to callCoInitialize()?before?CoCreateInstance().?CoCreateInstance()?will return a value of 0x800401F0. You can enter that value into Error Lookup and you‘ll see the description: "CoInitialize has not been called."
You can also look up?HRESULT?descriptions in the debugger. If you have an?HRESULT?variable called?hres, you can view the description in the Watch window by entering "hres,hr" as the value to watch. The ",hr" tells VC to display the value as an?HRESULT?description.
标签:
原文地址:http://www.cnblogs.com/time-is-life/p/5624168.html