First, the preface
The last book introduced the concepts of GUIDs, CLSID, IID, and interfaces. The focus of this return is to introduce the data types in COM. I do not introduce the component program design steps AH? Cough...... Don't worry, don't worry! Confucius said: "To eat a mouthful of food", Lao Zi: "Impatient for hot tofu", Sun Ziyun: "Take a step to see a step" ... First grasp the necessary knowledge, the future will be able to write procedures to be handy also:-)
Before you get to the point, keep in mind one principle: COM components run in a distributed environment. For example, if you write a component program (DLL or EXE), the user may be loading a component (Inproc_server) in a process within the machine, or a process that invokes a component from another process (Local_server) Or it may be on this computer that calls the component on the computer on the Earth (Remote_server). So when it comes to understanding and designing, remember this phrase all the time. Fast! Take out the little books and write them down!
Second, the HRESULT function return value
Everyone has their own philosophical ideas when they do programming. Take the function return value, there are many forms.
Function |
return value |
return value information |
Double sin (double) |
Floating-point values |
Calculate the positive and the black value |
BOOL DeleteFile (LPCTSTR) |
Boolean value |
Whether the file deletion was successful. If you fail, you need GetLastError () to get the reason for failure |
void * malloc (size_t) |
Memory pointers |
The memory request, if failed, returns null pointer nulls |
LONG Regdeletekey (HKEY,LPCTSTR) |
Integer |
Deletes a registry key. 0 indicates success, not 0 failure, and this value reflects the cause of the failure |
UINT DragQueryFile(HDROP,UINT,LPTSTR,UINT) |
Integer |
Gets the drag-and-drop file information. Called with a different parameter, the different meanings are returned: A few minutes to indicate the number of files, a short file name length, a moment to indicate character length |
...... ...... |
... |
...... ...... |
Such a complex return value, so variable return value, so that everyone in the learning and use of the process, adding additional difficulties. Well, COM's design code has finally unified them. Component APIs and interface pointers, except for the IUnknown::AddRef () and IUnknown::Release () two functions, all other functions have an HRESULT as the return value. Imagine a component's interface function, such as add (), which completes the addition of 2 integers, in the C language, we can define the following:
long Add( long n1, long n2 )
{
return n1 + n2;
}
Do you remember the principles we just talked about? COM components are running in a distributed environment. In other words, this function may run on the "Other Side of the Earth" on the computer, since running so far away, it is possible that the server shutdown, network drop, running timeout, the other side is not in the service area ... and other exceptions. So the addition function, in addition to returning the result of the operation, should return a value------whether the function is properly executed.
HRESULT Add( long n1, long n2, long *pSum )
{
*pSum = n1 + n2;
return S_OK;
}
If the function executes normally, it returns S_OK, while the actual function run results are returned through the parameter pointer. If an exception is encountered, the COM system is judged and the corresponding error value is returned. The common return values are:
HRESULT |
Value |
Meaning |
S_ok |
0x00000000 |
Success |
S_false |
0x00000001 |
function completed successfully, but error occurred while returning |
E_invalidarg |
0x80070057 |
Parameter has error |
E_outofmemory |
0x8007000e |
Memory Request Error |
E_unexpected |
0x8000FFFF |
Unknown exception |
E_NotImpl |
0x80004001 |
Features not implemented |
E_fail |
0x80004005 |
Error not described in detail. General need to obtain Rich error error message (Note 1) |
E_pointer |
0x80004003 |
Invalid pointer |
E_handle |
0x80070006 |
Invalid handle |
E_abort |
0x80004004 |
Terminate operation |
E_accessdenied |
0x80070005 |
Access Denied |
E_nointerface |
0x80004002 |
Interface not supported |
Figure I, the structure of the HRESULT