ɎȿȾȿɊȺɅɖɇɈȿ ȺȽȿɇɌɋɌȼɈ ɉɈ ɈȻɊȺɁɈȼȺɇɂɘ ȽɈɋɍȾȺɊɋɌȼȿɇɇɈȿ ɈȻɊȺɁɈȼȺɌȿɅɖɇɈȿ ɍɑɊȿɀȾȿɇɂȿ ȼɕɋɒȿȽɈ ɉɊɈɎȿɋɋɂɈɇȺɅɖɇɈȽɈ ɈȻɊȺɁɈȼȺɇɂə «ȼɈɊɈɇȿɀɋɄɂɃ ȽɈɋɍȾȺɊɋɌȼȿɇɇɕɃ ɍɇɂȼȿɊɋɂɌȿɌ» ɍɬɜɟɪɠɞɟɧɨ ɧɚɭɱɧɨ-ɦɟɬɨɞɢɱɟɫɤɢɦ ɫɨɜɟɬɨɦ ɮɚɤɭɥɶɬɟɬɚ ɩɪɢɤɥɚɞɧɨɣ ɦɚɬɟɦɚɬɢɤɢ, ɢɧɮɨɪɦɚɬɢɤɢ ɢ ɦɟɯɚɧɢɤɢ 14 ɦɚɹ 2007, ɩɪɨɬɨɤɨɥ ʋ 9
Ⱥɜɬɨɪɵ: Ɇ.Ⱥ. Ⱥɪɬɟɦɨɜ, Ⱥ.Ⱥ. ȼɚɯɬɢɧ, Ƚ.ɗ. ȼɨɳɢɧɫɤɚɹ, ȼ.Ƚ. Ɋɭɞɚɥɟɜ Ɋɟɰɟɧɡɟɧɬ ɡɚɜ. ɤɚɮ. ɉɢɂɌ ɮ-ɬɚ ɎɄɇ ȼȽɍ ɇ.Ⱥ. Ɍɸɤɚɱɟɜ
Ɉɫɧɨɜɵ ɋɈɆ-ɬɟɯɧɨɥɨɝɢɣ ɍɱɟɛɧɨ-ɦɟɬɨɞɢɱɟɫɤɨɟ ɩɨɫɨɛɢɟ ɞɥɹ ɜɭɡɨɜ
ɍɱɟɛɧɨɟ ɩɨɫɨɛɢɟ ɩɨɞɝɨɬɨɜɥɟɧɨ ɧɚ ɤɚɮɟɞɪɟ ɩɪɨɝɪɚɦɦɧɨɝɨ ɨɛɟɫɩɟɱɟɧɢɹ ɢ ɚɞɦɢɧɢɫɬɪɢɪɨɜɚɧɢɹ ɢɧɮɨɪɦɚɰɢɨɧɧɵɯ ɫɢɫɬɟɦ ɮɚɤɭɥɶɬɟɬɚ ɩɪɢɤɥɚɞɧɨɣ ɦɚɬɟɦɚɬɢɤɢ, ɢɧɮɨɪɦɚɬɢɤɢ ɢ ɦɟɯɚɧɢɤɢ ȼɨɪɨɧɟɠɫɤɨɝɨ ɝɨɫɭɞɚɪɫɬɜɟɧɧɨɝɨ ɭɧɢɜɟɪɫɢɬɟɬɚ.
ɂɡɞɚɬɟɥɶɫɤɨ-ɩɨɥɢɝɪɚɮɢɱɟɫɤɢɣ ɰɟɧɬɪ ȼɨɪɨɧɟɠɫɤɨɝɨ ɝɨɫɭɞɚɪɫɬɜɟɧɧɨɝɨ ɭɧɢɜɟɪɫɢɬɟɬɚ 2007
Ⱦɥɹ ɫɩɟɰɢɚɥɶɧɨɫɬɢ 010503 – Ɇɚɬɟɦɚɬɢɱɟɫɤɨɟ ɨɛɟɫɩɟɱɟɧɢɟ ɢ ɚɞɦɢɧɢɫɬɪɢɪɨɜɚɧɢɟ ɢɧɮɨɪɦɚɰɢɨɧɧɵɯ ɫɢɫɬɟɦ.
2
ɋɨɞɟɪɠɚɧɢɟ ȼɜɟɞɟɧɢɟ............................................................................................................. 5 I. Ɉɫɧɨɜɧɵɟ ɩɨɧɹɬɢɹ ɢ ɨɩɪɟɞɟɥɟɧɢɹ................................................................ 5 1.1. ɑɬɨ ɬɚɤɨɟ COM. Ɉɫɧɨɜɧɵɟ ɫɜɨɣɫɬɜɚ, ɨɬɥɢɱɚɸɳɢɟ COM ɨɬ ɈɈɉ ....... 5 1.2. Ɉɬɧɨɲɟɧɢɟ «Ʉɥɢɟɧɬ-ɫɟɪɜɟɪ» .................................................................... 7 1.3. ɍɧɢɤɚɥɶɧɨɫɬɶ ɤɨɦɩɨɧɟɧɬ. GUID, CLSID................................................. 7 1.4. Ⱦɨɫɬɭɩ ɤ ɤɨɦɩɨɧɟɧɬɚɦ. ɂɧɬɟɪɮɟɣɫɵ ....................................................... 9 1.5. ɂɟɪɚɪɯɢɹ ɢɧɬɟɪɮɟɣɫɨɜ. ɂɧɬɟɪɮɟɣɫ IUnknown ....................................... 9 1.6. ɉɨɪɬɚɬɢɜɧɨɫɬɶ ɢ ɤɨɦɦɭɧɢɤɚɛɟɥɶɧɨɫɬɶ COM. Ɋɟɝɢɫɬɪɚɰɢɹ COMɫɟɪɜɟɪɨɜ. ɋɢɫɬɟɦɧɵɣ ɪɟɟɫɬɪ Windows.......................................................... 12 II. Ɋɚɡɪɚɛɨɬɤɚ COM-ɫɟɪɜɟɪɨɜ ɜ Delphi.......................................................... 14 2.1. Ɋɚɡɪɚɛɨɬɤɚ ɢɧɬɟɪɮɟɣɫɚ ............................................................................ 14 2.2. Ɋɟɚɥɢɡɚɰɢɹ ɜɫɬɪɨɟɧɧɨɝɨ COM-ɫɟɪɜɟɪɚ.................................................. 15 2.3. Ɋɚɡɪɚɛɨɬɤɚ ɤɨɦɩɨɧɟɧɬɨɜ.......................................................................... 17 2.4. Ɋɚɡɪɟɲɟɧɢɟ ɧɟɨɞɧɨɡɧɚɱɧɨɫɬɢ ɦɟɬɨɞɨɜ .................................................. 19 2.5. Ⱦɟɥɟɝɢɪɨɜɚɧɢɟ ɢɧɬɟɪɮɟɣɫɚ ..................................................................... 20 2.6. Ƚɟɧɟɪɚɬɨɪ ɤɨɦɩɨɧɟɧɬɨɜ ........................................................................... 22 2.7. ɉɪɢɦɟɪ ɪɟɚɥɢɡɚɰɢɢ ɜɫɬɪɨɟɧɧɨɝɨ COM-ɫɟɪɜɟɪɚ ................................... 24 2.8. ȼɧɟɲɧɢɟ COM-ɫɟɪɜɟɪɵ........................................................................... 27 2.9. Ȼɟɡɨɩɚɫɧɵɟ ɦɚɫɫɢɜɵ (SafeArray) ............................................................ 28 2.10. Ɉɛɪɚɛɨɬɤɚ ɦɚɫɫɢɜɨɜ ɞɚɧɧɵɯ ................................................................. 29 2.10.1. ɋɨɡɞɚɧɢɟ ɫɟɪɜɟɪɚ................................................................................. 29 2.10.2. ɋɨɡɞɚɧɢɟ ɤɥɢɟɧɬɚ ................................................................................ 31 III. Ȼɢɛɥɢɨɬɟɤɚ ɬɢɩɨɜ ...................................................................................... 33 3.1. ɉɪɨɟɤɬɢɪɨɜɚɧɢɟ ɛɢɛɥɢɨɬɟɤɢ ɬɢɩɨɜ. Ɋɟɞɚɤɬɨɪ Type Library Editor..... 33 3.2. ɉɚɧɟɥɶ ɢɧɫɬɪɭɦɟɧɬɨɜ ɞɢɚɥɨɝɨɜɨɝɨ ɨɤɧɚ Type Library Editor .............. 34 3.3. ɂɫɩɨɥɶɡɨɜɚɧɢɟ ɛɢɛɥɢɨɬɟɤɢ ɬɢɩɨɜ ɩɪɢ ɪɚɡɪɚɛɨɬɤɟ ɤɨɦɩɨɧɟɧɬɨɜ ....... 35 3.4. ɉɪɢɦɟɪ ɪɚɡɪɚɛɨɬɤɢ ɜɫɬɪɨɟɧɧɨɝɨ COM-ɫɟɪɜɟɪɚ ɫ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ ɛɢɛɥɢɨɬɟɤɢ ɬɢɩɨɜ ............................................................................................ 36 IV. Ɋɚɡɪɚɛɨɬɤɚ ɩɪɢɥɨɠɟɧɢɣ, ɢɫɩɨɥɶɡɭɸɳɢɯ COM-ɫɟɪɜɟɪɵ ...................... 38 4.1. ɉɨɥɭɱɟɧɢɟ ɫɫɵɥɤɢ ɧɚ ɢɧɬɟɪɮɟɣɫ ɤɨɦɩɨɧɟɧɬɚ. Ɏɭɧɤɰɢɢ CreateComObject ɢ ProgIdToClassId...................................................................................... 38 4.2. ɗɤɫɩɨɪɬ ɨɩɢɫɚɧɢɹ ɢɧɬɟɪɮɟɣɫɨɜ ɢɡ ɛɢɛɥɢɨɬɟɤɢ ɬɢɩɨɜ COM-ɫɟɪɜɟɪɚ 39 4.3. Ɉɫɨɛɟɧɧɨɫɬɢ ɬɟɯɧɢɤɢ ɢɫɩɨɥɶɡɨɜɚɧɢɹ ɤɨɦɩɨɧɟɧɬɨɜ ɜ Delphi............. 40 4.4. ɉɪɢɦɟɪ ɪɟɚɥɢɡɚɰɢɢ COM-ɤɥɢɟɧɬɚ ......................................................... 41 V. Ⱥɜɬɨɦɚɬɢɡɚɰɢɹ............................................................................................ 43 5.1. Ɋɚɧɧɟɟ ɢ ɩɨɡɞɧɟɟ ɫɜɹɡɵɜɚɧɢɟ. Ɍɢɩ Variant ............................................ 43 5.2. ɂɧɬɟɪɮɟɣɫɵ ɞɢɫɩɟɬɱɟɪɢɡɚɰɢɢ. ............................................................... 45 5.3. ɉɪɢɦɟɪ ɪɟɚɥɢɡɚɰɢɢ ɜɫɬɪɨɟɧɧɨɝɨ COM-ɫɟɪɜɟɪɚ ɚɜɬɨɦɚɬɢɡɚɰɢɢ........ 47 5.4. ɉɪɢɦɟɪ ɪɟɚɥɢɡɚɰɢɢ ɜɧɟɲɧɟɝɨ COM-ɫɟɪɜɟɪɚ ɚɜɬɨɦɚɬɢɡɚɰɢɢ ............ 48 5.5. ɋɨɛɵɬɢɹ ɜ COM ɢ ɨɛɪɚɬɧɵɟ ɜɵɡɨɜɵ...................................................... 50 VI. Ⱥɜɬɨɦɚɬɢɡɚɰɢɹ ɩɪɢɥɨɠɟɧɢɣ Microsoft Office ........................................ 54 3
6.1. VBA ɢ ɫɪɟɞɫɬɜɚ ɪɚɡɪɚɛɨɬɤɢ ɤɨɧɬɪɨɥɥɟɪɨɜ ɚɜɬɨɦɚɬɢɡɚɰɢɢ ................. 56 6.2. Ɉɛɴɟɤɬɧɵɟ ɦɨɞɟɥɢ Microsoft Office ....................................................... 57 6.3. Ɉɛɳɢɟ ɩɪɢɧɰɢɩɵ ɫɨɡɞɚɧɢɹ ɤɨɧɬɪɨɥɥɟɪɨɜ ɚɜɬɨɦɚɬɢɡɚɰɢɢ.................. 58 6.4. Ⱥɜɬɨɦɚɬɢɡɚɰɢɹ Microsoft Word............................................................... 58 6.4.1. ɉɪɨɝɪɚɦɦɧɵɟ ɢɞɟɧɬɢɮɢɤɚɬɨɪɵ ɢ ɨɛɴɟɤɬɧɚɹ ɦɨɞɟɥɶ Microsoft Word.................................................................................... 58 6.4.2. ɋɨɡɞɚɧɢɟ ɢ ɨɬɤɪɵɬɢɟ ɞɨɤɭɦɟɧɬɨɜ Microsoft Word ............................ 60 6.4.3. ɋɨɯɪɚɧɟɧɢɟ, ɩɟɱɚɬɶ ɢ ɡɚɤɪɵɬɢɟ ɞɨɤɭɦɟɧɬɨɜ Microsoft Word .......... 60 6.4.4. ȼɫɬɚɜɤɚ ɬɟɤɫɬɚ ɢ ɨɛɴɟɤɬɨɜ ɜ ɞɨɤɭɦɟɧɬ ɢ ɮɨɪɦɚɬɢɪɨɜɚɧɢɟ ɬɟɤɫɬɚ ... 61 6.4.5. ɉɟɪɟɦɟɳɟɧɢɟ ɤɭɪɫɨɪɚ ɩɨ ɬɟɤɫɬɭ ......................................................... 64 6.4.6. ɋɨɡɞɚɧɢɟ ɬɚɛɥɢɰ .................................................................................... 65 6.4.7. Ɉɛɪɚɳɟɧɢɟ ɤ ɫɜɨɣɫɬɜɚɦ ɞɨɤɭɦɟɧɬɚ..................................................... 66 6.5. Ⱥɜɬɨɦɚɬɢɡɚɰɢɹ Microsoft Excel............................................................... 66 6.5.1. ɉɪɨɝɪɚɦɦɧɵɟ ɢɞɟɧɬɢɮɢɤɚɬɨɪɵ ɢ ɨɛɴɟɤɬɧɚɹ ɦɨɞɟɥɶ Microsoft Excel.................................................................................... 66 6.5.2. Ɂɚɩɭɫɤ Microsoft Excel, ɫɨɡɞɚɧɢɟ ɢ ɨɬɤɪɵɬɢɟ ɪɚɛɨɱɢɯ ɤɧɢɝ .................................................................................................... 68 6.5.3. ɋɨɯɪɚɧɟɧɢɟ, ɩɟɱɚɬɶ ɢ ɡɚɤɪɵɬɢɟ ɪɚɛɨɱɢɯ ɤɧɢɝ Microsoft Excel ........................................................................ 69 6.5.4. Ɉɛɪɚɳɟɧɢɟ ɤ ɥɢɫɬɚɦ ɢ ɹɱɟɣɤɚɦ ........................................................... 70 6.5.5. ɋɨɡɞɚɧɢɟ ɞɢɚɝɪɚɦɦ ............................................................................... 71 6.6. ɉɪɢɦɟɪ ɷɤɫɩɨɪɬɚ ɞɚɧɧɵɯ ɜ Word............................................................ 73 Ɂɚɞɚɱɢ «Ʉɥɢɟɧɬ-ɋɟɪɜɟɪ» ................................................................................ 82 ɋɩɢɫɨɤ ɥɢɬɟɪɚɬɭɪɵ ......................................................................................... 84 ɋɫɵɥɤɢ ɜ Internet ............................................................................................. 84
4
ȼɜɟɞɟɧɢɟ ɉɵɬɚɹɫɶ ɪɚɫɲɢɪɢɬɶ ɜɨɡɦɨɠɧɨɫɬɢ Windows, ɤɨɦɩɚɧɢɹ Microsoft ɫɬɨɥɤɧɭɥɚɫɶ ɫ ɧɟɨɛɯɨɞɢɦɨɫɬɶɸ ɫɨɡɞɚɧɢɹ ɬɟɯɧɨɥɨɝɢɢ, ɤɨɬɨɪɚɹ ɨɛɟɫɩɟɱɢɜɚɥɚ ɛɵ ɞɨɫɬɭɩ ɩɪɢɥɨɠɟɧɢɣ ɤ ɨɛɴɟɤɬɚɦ, ɪɚɫɩɨɥɨɠɟɧɧɵɦ ɜɧɟ ɩɪɢɥɨɠɟɧɢɹ. Ⱦɪɭɝɢɦɢ ɫɥɨɜɚɦɢ, ɩɪɢɥɨɠɟɧɢɹ ɞɨɥɠɧɵ ɪɚɛɨɬɚɬɶ ɫ ɜɧɟɲɧɢɦɢ ɨɛɴɟɤɬɚɦɢ ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɨɜ, ɤɨɬɨɪɵɟ ɢɫɩɨɥɶɡɭɸɬɫɹ ɞɥɹ ɪɚɛɨɬɵ ɫ ɜɧɭɬɪɟɧɧɢɦɢ ɨɛɴɟɤɬɚɦɢ. Ʉɪɨɦɟ ɬɨɝɨ, ɨɛɴɟɤɬɵ ɧɟ ɞɨɥɠɧɵ ɡɚɜɢɫɟɬɶ ɨɬ ɹɡɵɤɚ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ, ɤ ɧɢɦ ɞɨɥɠɟɧ ɛɵɬɶ ɨɬɤɪɵɬ ɞɨɫɬɭɩ ɫɨ ɫɬɨɪɨɧɵ ɩɪɢɥɨɠɟɧɢɣ ɧɟ ɬɨɥɶɤɨ ɥɨɤɚɥɶɧɨɝɨ, ɧɨ ɢ ɭɞɚɥɟɧɧɨɝɨ ɤɨɦɩɶɸɬɟɪɚ. ȼ ɤɚɱɟɫɬɜɟ ɪɟɲɟɧɢɹ ɩɪɨɛɥɟɦɵ, ɫɜɹɡɚɧɧɨɣ ɫ ɞɨɫɬɭɩɨɦ ɤ ɨɛɴɟɤɬɚɦ ɜ ɩɪɟɞɟɥɚɯ ɨɬɞɟɥɶɧɨɝɨ ɤɨɦɩɶɸɬɟɪɚ (ɩɪɢɥɨɠɟɧɢɟ—ɩɪɢɥɨɠɟɧɢɟ) ɢ ɥɨɤɚɥɶɧɨɣ ɫɟɬɢ (ɤɨɦɩɶɸɬɟɪ—ɤɨɦɩɶɸɬɟɪ), ɤɨɦɩɚɧɢɹ Microsoft ɪɚɡɪɚɛɨɬɚɥɚ ɦɨɞɟɥɢ ɫɨɫɬɚɜɧɵɯ ɨɛɴɟɤɬɨɜ, ɤɨɬɨɪɵɟ ɧɚɡɵɜɚɸɬɫɹ ɋɈɆ 1 (Component Object Model) ɢ DCOM (Distributed ɋɈɆ). ȼ ɞɚɧɧɨɦ ɦɟɬɨɞɢɱɟɫɤɨɦ ɩɨɫɨɛɢɢ ɢɡɥɨɠɟɧɵ ɨɫɧɨɜɵ ɷɬɨɣ ɬɟɯɧɨɥɨɝɢɢ ɢ ɩɪɨɞɟɦɨɧɫɬɪɢɪɨɜɚɧɵ ɩɪɢɦɟɪɵ ɫɨɡɞɚɧɢɹ ɢ ɢɫɩɨɥɶɡɨɜɚɧɢɹ ɩɪɢɥɨɠɟɧɢɣ, ɨɫɧɨɜɚɧɧɵɯ ɧɚ ɬɟɯɧɨɥɨɝɢɢ COM.
I. Ɉɫɧɨɜɧɵɟ ɩɨɧɹɬɢɹ ɢ ɨɩɪɟɞɟɥɟɧɢɹ 1.1. ɑɬɨ ɬɚɤɨɟ COM. Ɉɫɧɨɜɧɵɟ ɫɜɨɣɫɬɜɚ, ɨɬɥɢɱɚɸɳɢɟ COM ɨɬ ɈɈɉ Ɍɟɯɧɨɥɨɝɢɹ ɨɛɴɟɤɬɧɨ-ɨɪɢɟɧɬɢɪɨɜɚɧɧɨɝɨ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɨɫɧɨɜɚɧɚ ɧɚ ɬɨɦ, ɱɬɨ ɩɪɢɥɨɠɟɧɢɟ ɪɚɡɛɢɜɚɟɬɫɹ ɧɚ ɨɛɴɟɤɬɵ, ɤɨɬɨɪɵɟ ɜɡɚɢɦɨɞɟɣɫɬɜɭɸɬ ɞɪɭɝ ɫ ɞɪɭɝɨɦ ɜɧɭɬɪɢ ɩɪɨɝɪɚɦɦɵ (ɪɢɫ. 1.1).
ɗɬɨ ɞɚɟɬ ɜɨɡɦɨɠɧɨɫɬɶ ɛɵɫɬɪɨɣ ɪɚɡɪɚɛɨɬɤɢ ɢ ɦɨɞɢɮɢɤɚɰɢɢ ɩɪɨɝɪɚɦɦɧɵɯ ɩɪɨɞɭɤɬɨɜ ɫ ɢɡɛɟɠɚɧɢɟɦ ɨɲɢɛɨɤ, ɬɚɤ ɤɚɤ ɩɪɨɝɪɚɦɦɧɵɣ ɤɨɞ ɪɚɡɛɢɜɚɟɬɫɹ ɧɚ ɨɬɞɟɥɶɧɵɟ ɛɥɨɤɢ (ɨɛɴɟɤɬɵ), ɫɜɹɡɶ ɦɟɠɞɭ ɤɨɬɨɪɵɦɢ ɨɫɭɳɟɫɬɜɥɹɟɬɫɹ ɩɨ ɭɫɬɚɧɨɜɥɟɧɧɵɦ ɩɪɚɜɢɥɚɦ (ɫɜɨɣɫɬɜɚ ɢ ɦɟɬɨɞɵ). ȼ COM-ɬɟɯɧɨɥɨɝɢɢ ɨɛɴɟɤɬɵ ɩɨɦɟɳɚɸɬɫɹ ɜ ɨɬɞɟɥɶɧɵɟ ɢɫɩɨɥɧɹɟɦɵɟ ɛɥɨɤɢ (ɪɢɫ. 1.2), ɤɨɬɨɪɵɦɢ ɦɨɝɭɬ ɹɜɥɹɬɶɫɹ ɞɢɧɚɦɢɱɟɫɤɢ ɤɨɦɩɨɧɭɟɦɵɟ ɛɢɛɥɢɨɬɟɤɢ (DLL) ɢɥɢ ɩɪɢɥɨɠɟɧɢɟ (EXE).
Ɋɢɫ. 1.2. ɉɪɨɝɪɚɦɦɚ, ɫɤɨɧɫɬɪɭɢɪɨɜɚɧɧɚɹ ɩɨ ɚɪɯɢɬɟɤɬɭɪɟ COM Ɇɨɞɟɥɶ COM ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ ɢɧɫɬɪɭɤɰɢɸ ɩɨ ɫɨɡɞɚɧɢɸ ɫɨɜɦɟɫɬɢɦɵɯ ɤɨɦɩɨɧɟɧɬɨɜ ɩɪɨɝɪɚɦɦɧɨɝɨ ɨɛɟɫɩɟɱɟɧɢɹ ɢ ɧɟ ɹɜɥɹɟɬɫɹ ɹɡɵɤɨɦ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ, ɛɢɛɥɢɨɬɟɤɨɣ ɩɪɨɝɪɚɦɦɧɨɝɨ ɤɨɞɚ ɢɥɢ ɤɨɦɩɢɥɹɬɨɪɨɦ. Ɍɟɯɧɨɥɨɝɢɹ COM ɨɫɧɨɜɚɧɚ ɧɚ ɫɥɟɞɭɸɳɢɯ ɨɫɧɨɜɧɵɯ ɤɨɧɰɟɩɰɢɹɯ. 1. ɍɧɢɤɚɥɶɧɨɫɬɶ ɢ ɤɨɧɬɟɤɫɬɧɚɹ ɧɟɡɚɜɢɫɢɦɨɫɬɶ ɤɨɦɩɨɧɟɧɬ. ȼ ɫɢɫɬɟɦɟ ɧɟ ɞɨɥɠɧɨ ɛɵɬɶ ɤɨɦɩɨɧɟɧɬɨɜ ɫ ɨɞɢɧɚɤɨɜɵɦ ɫɩɨɫɨɛɨɦ ɨɛɪɚɳɟɧɢɹ ɢ ɪɚɡɧɵɦɢ ɫɦɵɫɥɨɜɵɦɢ ɧɚɡɧɚɱɟɧɢɹɦɢ. 2. ɂɧɤɚɩɫɭɥɹɰɢɹ. Ɋɟɚɥɢɡɚɰɢɹ ɤɨɦɩɨɧɟɧɬɨɜ COM ɞɨɥɠɧɚ ɛɵɬɶ ɫɤɪɵɬɚ. ɗɬɨ ɧɟɨɛɯɨɞɢɦɨ ɞɥɹ ɬɨɝɨ, ɱɬɨɛɵ ɛɵɥɚ ɧɟɡɚɜɢɫɢɦɨɫɬɶ ɨɬ ɹɡɵɤɨɜ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ. 3. ɉɨɪɬɚɬɢɜɧɨɫɬɶ ɢ ɤɨɦɦɭɧɢɤɚɛɟɥɶɧɨɫɬɶ. ɇɭɠɧɵɣ ɤɨɦɩɨɧɟɧɬ ɦɨɠɧɨ ɛɵɫɬɪɨ ɧɚɣɬɢ ɞɢɧɚɦɢɱɟɫɤɢ, ɝɞɟ ɛɵ ɨɧ ɧɢ ɧɚɯɨɞɢɥɫɹ.
Ɋɢɫ. 1.1. ɉɪɨɝɪɚɦɦɚ, ɫɤɨɧɫɬɪɭɢɪɨɜɚɧɧɚɹ ɜ ɈɈɉ
ɉɪɟɢɦɭɳɟɫɬɜɨ ɋɈɆ ɡɚɤɥɸɱɚɟɬɫɹ ɜ ɬɨɦ, ɱɬɨ ɷɬɨ ɩɨɡɜɨɥɹɟɬ ɤɚɠɞɨɦɭ ɪɚɡɪɚɛɨɬɱɢɤɭ ɫɤɨɧɰɟɧɬɪɢɪɨɜɚɬɶɫɹ ɧɚ ɪɚɡɪɚɛɨɬɤɟ ɫɜɨɢɯ ɤɨɦɩɨɧɟɧɬ, ɤɨɬɨɪɵɟ ɦɨɝɭɬ ɨɛɦɟɧɢɜɚɬɶɫɹ ɢɧɮɨɪɦɚɰɢɟɣ ɧɟɡɚɜɢɫɢɦɨ ɨɬ ɹɡɵɤɚ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɢɥɢ ɢɧɫɬɪɭɦɟɧɬɚɥɶɧɵɯ ɫɪɟɞɫɬɜ ɪɟɚɥɢɡɚɰɢɢ. Ȼɨɥɟɟ ɬɨɝɨ, ɬɟɯɧɨɥɨɝɢɹ COM ɞɚɟɬ ɝɢɛɤɢɟ ɜɨɡɦɨɠɧɨɫɬɢ ɞɢɧɚɦɢɱɟɫɤɨɣ ɧɚɫɬɪɨɣɤɢ ɢ ɦɨɞɢɮɢɤɚɰɢɢ ɩɪɨɝɪɚɦɦɧɨɝɨ ɩɪɨɞɭɤɬɚ ɫ ɭɱɟɬɨɦ ɠɟɥɚɧɢɣ ɩɨɥɶɡɨɜɚɬɟɥɹ ɢ ɨɫɨɛɟɧɧɨɫɬɟɣ ɨɩɟɪɚɰɢɨɧɧɨɣ ɫɢɫɬɟɦɵ.
1
ɇɚɱɢɧɚɹ ɫ Windows 2000 ɜɜɨɞɢɬɫɹ ɧɨɜɚɹ ɬɟɯɧɨɥɨɝɢɹ, ɤɨɬɨɪɚɹ ɧɚɡɵɜɚɟɬɫɹ COM+. ɗɬɨ ɪɚɫɲɢɪɟɧɧɵɟ ɜɨɡɦɨɠɧɨɫɬɢ ɬɟɯɧɨɥɨɝɢɢ COM, ɩɨɞɞɟɪɠɢɜɚɸɳɚɹ ɚɪɯɢɬɟɤɬɭɪɭ Windows DNA (Distributed interNet Application) [5]. 5
6
1.2. Ɉɬɧɨɲɟɧɢɟ «Ʉɥɢɟɧɬ-ɫɟɪɜɟɪ» ɉɭɫɬɶ ɩɪɢɥɨɠɟɧɢɸ ɬɪɟɛɭɟɬɫɹ ɫɨɜɟɪɲɢɬɶ ɤɚɤɨɟ-ɬɨ ɞɟɣɫɬɜɢɟ ɢɥɢ ɩɪɨɰɟɫɫ, ɤɨɬɨɪɵɣ ɟɦɭ ɧɟɢɡɜɟɫɬɟɧ. ɇɨ ɟɫɬɶ ɤɨɦɩɨɧɟɧɬ, ɧɚɯɨɞɹɳɢɣɫɹ ɜ ɞɪɭɝɨɦ ɞɜɨɢɱɧɨɦ ɤɨɞɟ, ɤɨɬɨɪɵɣ ɦɨɠɟɬ ɷɬɨ ɞɟɣɫɬɜɢɟ ɢɥɢ ɩɪɨɰɟɫɫ ɫɨɜɟɪɲɢɬɶ. ȿɫɥɢ ɩɪɢɥɨɠɟɧɢɟ «ɡɧɚɟɬ» ɨ ɫɭɳɟɫɬɜɨɜɚɧɢɢ ɢ ɜɨɡɦɨɠɧɨɫɬɹɯ ɷɬɨɝɨ ɤɨɦɩɨɧɟɧɬɚ, ɬɨ ɨɧɨ «ɡɚɫɬɚɜɥɹɟɬ» ɤɨɦɩɨɧɟɧɬ ɜɵɩɨɥɧɢɬɶ ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɟ ɞɟɣɫɬɜɢɟ. Ɍɚɤɨɟ ɜɡɚɢɦɨɞɟɣɫɬɜɢɟ ɩɪɢɥɨɠɟɧɢɹ ɢ ɤɨɦɩɨɧɟɧɬɚ ɧɚɡɵɜɚɸɬ ɨɬɧɨɲɟɧɢɟɦ «ɤɥɢɟɧɬ-ɫɟɪɜɟɪ», ɝɞɟ ɩɪɢɥɨɠɟɧɢɟ, ɢɝɪɚɸɳɟɟ ɪɨɥɶ ɢɧɢɰɢɚɬɨɪɚ, ɧɚɡɵɜɚɟɬɫɹ «ɤɥɢɟɧɬɨɦ», ɚ ɢɫɩɨɥɧɹɟɦɵɣ ɦɨɞɭɥɶ, ɯɪɚɧɹɳɢɣ ɤɨɦɩɨɧɟɧɬ, – «ɫɟɪɜɟɪɨɦ». ɋɭɳɟɫɬɜɭɟɬ ɞɜɚ ɬɢɩɚ COM-ɫɟɪɜɟɪɨɜ: ɜɫɬɪɨɟɧɧɵɟ, ɬɚɤɠɟ ɢɯ ɧɚɡɵɜɚɸɬ ɜɧɭɬɪɟɧɧɢɟ, ɢ ɜɧɟɲɧɢɟ. ȼɫɬɪɨɟɧɧɵɟ COM-ɫɟɪɜɟɪɵ ɷɬɨ ɞɢɧɚɦɢɱɟɫɤɢ ɤɨɦɩɨɧɭɟɦɵɟ ɛɢɛɥɢɨɬɟɤɢ (DLL), ɤɨɬɨɪɵɟ ɩɪɢ ɡɚɝɪɭɡɤɟ ɜ ɩɚɦɹɬɶ ɤɨɦɩɶɸɬɟɪɚ ɨɬɨɛɪɚɠɚɸɬɫɹ ɧɚ ɚɞɪɟɫɧɨɟ ɩɪɨɫɬɪɚɧɫɬɜɨ COM-ɤɥɢɟɧɬɚ (ɩɪɨɝɪɚɦɦɵ). ȼɧɟɲɧɢɟ COM-ɫɟɪɜɟɪɵ ɪɟɚɥɢɡɨɜɚɧɵ ɜ ɜɢɞɟ ɢɫɩɨɥɧɹɟɦɵɯ ɩɪɨɝɪɚɦɦ (exeɮɚɣɥɨɜ), ɢ, ɫɥɟɞɨɜɚɬɟɥɶɧɨ, ɨɧɢ ɜ ɩɪɨɰɟɫɫɟ ɜɵɩɨɥɧɟɧɢɹ ɡɚɧɢɦɚɸɬ ɜ ɩɚɦɹɬɢ ɤɨɦɩɶɸɬɟɪɚ ɚɞɪɟɫɧɨɟ ɩɪɨɫɬɪɚɧɫɬɜɨ, ɨɬɞɟɥɶɧɨɟ ɨɬ ɤɥɢɟɧɬɫɤɨɝɨ ɩɪɢɥɨɠɟɧɢɹ. ɗɬɨ ɩɪɢɜɨɞɢɬ ɤ ɧɟɤɨɬɨɪɵɦ ɧɸɚɧɫɚɦ ɩɪɢ ɫɨɡɞɚɧɢɢ ɬɚɤɨɝɨ ɪɨɞɚ COM-ɫɟɪɜɟɪɨɜ, ɨ ɱɟɦ ɛɭɞɟɬ ɫɤɚɡɚɧɨ ɜ ɩ. 2.8.
ɢɞɟɧɬɢɮɢɤɚɬɨɪ ɦɨɠɟɬ ɛɵɬɶ ɫɢɧɬɟɡɢɪɨɜɚɧ ɢɡ ɨɱɟɧɶ ɧɟɩɨɫɬɨɹɧɧɵɯ ɦɚɲɢɧɧɵɯ ɯɚɪɚɤɬɟɪɢɫɬɢɤ. ȼ ɫɨɜɪɟɦɟɧɧɵɯ ɫɪɟɞɚɯ ɪɚɡɪɚɛɨɬɤɢ ɩɪɢɥɨɠɟɧɢɣ, ɬɚɤɢɯ ɤɚɤ Delphi, Visual Studio, ɪɟɚɥɢɡɨɜɚɧɵ ɫɪɟɞɫɬɜɚ ɚɜɬɨɦɚɬɢɱɟɫɤɨɝɨ ɩɨɥɭɱɟɧɢɹ ɭɧɢɤɚɥɶɧɨɝɨ ɧɨɦɟɪɚ ɱɟɪɟɡ API-ɮɭɧɤɰɢɸ CoCreateGuid. ɉɪɢɦɟɱɚɧɢɟ 1.1. ɑɬɨɛɵ ɩɨɥɭɱɢɬɶ GUID ɜ Delphi, ɧɚɞɨ ɧɚɠɚɬɶ ɤɨɦɛɢɧɚɰɢɸ ɤɥɚɜɢɲ Ctrl+Shift+G. Ⱦɥɹ ɚɜɬɨɦɚɬɢɱɟɫɤɨɣ ɝɟɧɟɪɚɰɢɢ ɝɥɨɛɚɥɶɧɨɝɨ ɭɧɢɤɚɥɶɧɨɝɨ ɧɨɦɟɪɚ ɜ ɩɪɨɝɪɚɦɦɟ ɢɫɩɨɥɶɡɭɟɬɫɹ ɩɪɨɰɟɞɭɪɚ CoCreateGuid ȼ ɩɪɨɝɪɚɦɦɟ GUID ɨɩɪɟɞɟɥɹɟɬɫɹ ɤɚɤ ɫɬɪɭɤɬɭɪɚ ɬɢɩɚ TGUID=Record D1: LongWord; D2: Word; D3: Word; D4: array [0..7] of Byte; End; ɑɟɬɵɪɟ ɩɨɥɹ TGUID ɫɨɨɬɜɟɬɫɬɜɭɸɬ ɱɟɬɵɪɟɦ ɫɨɫɬɚɜɧɵɦ 16-ɛɚɣɬɧɨɝɨ ɱɢɫɥɚ. Ⱦɥɹ ɭɞɨɛɫɬɜɚ ɷɬɨ ɱɢɫɥɨ ɦɨɠɟɬ ɛɵɬɶ ɡɚɩɢɫɚɧɨ ɜ ɫɥɟɞɭɸɳɟɣ ɫɢɦɜɨɥɶɧɨɣ ɲɟɫɬɧɚɞɰɚɬɟɪɢɱɧɨɣ ɮɨɪɦɟ (ɤɚɠɞɚɹ ɲɟɫɬɧɚɞɰɚɬɟɪɢɱɧɚɹ ɰɢɮɪɚ ɡɚɦɟɧɟɧɚ ɫɢɦɜɨɥɨɦ X): ‘{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}’
1.3. ɍɧɢɤɚɥɶɧɨɫɬɶ ɤɨɦɩɨɧɟɧɬ. GUID, CLSID ɑɬɨɛɵ ɩɪɢɥɨɠɟɧɢɟ ɦɨɝɥɨ ɫɜɹɡɚɬɶɫɹ ɫ ɤɨɦɩɨɧɟɧɬɨɦ, ɧɟɨɛɯɨɞɢɦɨ ɡɧɚɬɶ ɢɦɹ ɨɛɴɟɤɬɚ. ɇɨ ɬɭɬ ɜɨɡɧɢɤɚɟɬ ɩɪɨɛɥɟɦɚ ɫɨɜɩɚɞɟɧɢɹ ɢɦɟɧ, ɬɚɤ ɤɚɤ ɩɪɨɞɭɤɬɵ ɪɚɡɧɵɯ ɩɪɨɢɡɜɨɞɢɬɟɥɟɣ ɦɨɝɭɬ ɢɦɟɬɶ ɨɞɢɧɚɤɨɜɵɟ ɢɦɟɧɚ, ɚ ɫɥɟɞɨɜɚɬɟɥɶɧɨ, ɧɟ ɦɨɝɭɬ ɛɵɬɶ ɭɫɬɚɧɨɜɥɟɧɵ ɧɚ ɨɞɧɭ ɦɚɲɢɧɭ. Ⱦɥɹ ɪɟɲɟɧɢɹ ɷɬɨɣ ɩɪɨɛɥɟɦɵ ɤɨɦɩɚɧɢɹ Microsoft ɪɚɡɪɚɛɨɬɚɥɚ ɢɞɟɸ ɧɭɦɟɪɚɰɢɢ ɤɨɦɩɨɧɟɧɬɨɜ ɭɧɢɤɚɥɶɧɵɦɢ ɧɨɦɟɪɚɦɢ. ɗɬɨɬ ɧɨɦɟɪ ɧɚɡɵɜɚɟɬɫɹ GUID (Globally Unique Identifier), ɱɬɨ ɨɡɧɚɱɚɟɬ ɝɥɨɛɚɥɶɧɨ ɭɧɢɤɚɥɶɧɵɣ ɢɞɟɧɬɢɮɢɤɚɬɨɪ. GUID ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ 16-ɛɚɣɬɧɨɟ ɱɢɫɥɨ, ɜɵɱɢɫɥɟɧɧɨɟ ɫɩɟɰɢɚɥɶɧɨɣ ɯɷɲ-ɮɭɧɤɰɢɟɣ, ɭɱɢɬɵɜɚɸɳɟɣ ɫɥɟɞɭɸɳɢɟ ɞɚɧɧɵɟ: x ɬɟɤɭɳɢɟ ɞɚɬɭ ɢ ɜɪɟɦɹ; x «ɬɢɤɢ ɬɚɣɦɟɪɚ»; x ɩɪɨɫɬɨɣ ɢɧɤɪɟɦɟɧɬɧɵɣ ɫɱɟɬɱɢɤ, ɱɬɨɛɵ ɦɨɠɧɨ ɛɵɥɨ ɛɵ ɩɪɚɜɢɥɶɧɨ ɨɛɪɚɛɚɬɵɜɚɬɶ ɫɥɟɞɭɸɳɢɟ ɨɱɟɧɶ ɱɚɫɬɨ ɡɚɩɪɨɫɵ ɧɚ ɝɟɧɟɪɚɰɢɸ GUID; x ɢɫɬɢɧɧɨ ɝɥɨɛɚɥɶɧɨ ɭɧɢɤɚɥɶɧɵɣ IEEE-ɢɞɟɧɬɢɮɢɤɚɬɨɪ ɦɚɲɢɧɵ, ɢɡɜɥɟɤɚɟɦɵɣ ɢɡ ɫɟɬɟɜɨɣ ɤɚɪɬɵ. ȿɫɥɢ ɜ ɦɚɲɢɧɟ ɧɟɬ ɫɟɬɟɜɨɣ ɤɚɪɬɵ,
7
ɉɪɢɦɟɪɵ ɡɚɩɢɫɢ GUID: ‘{168A8BE0-485B-11D5-B5D2-444553540000}’ ‘{168A8BE1-485B-11D5-B5D2-444553540000}’ ‘{168A8BE2-485B-11D5-B5D2-444553540000}’ Ⱦɥɹ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɫɢɦɜɨɥɶɧɨɝɨ ɩɪɟɞɫɬɚɜɥɟɧɢɹ GUID ɜ ɞɜɨɢɱɧɨɟ ɢ ɨɛɪɚɬɧɨ ɜ Delphi ɪɟɚɥɢɡɨɜɚɧɵ ɫɥɟɞɭɸɳɢɟ ɫɢɫɬɟɦɧɵɟ ɮɭɧɤɰɢɢ: function StringToGUID(const S: string): TGUID; function GUIDToString(const ClassID: TGUID): string; ɂɡ ɚɥɝɨɪɢɬɦɚ ɩɨɥɭɱɟɧɢɹ GUID ɢ ɪɚɡɦɟɪɧɨɫɬɢ ɱɢɫɥɚ ɜɢɞɧɨ, ɱɬɨ ɷɬɨɬ ɧɨɦɟɪ ɞɟɣɫɬɜɢɬɟɥɶɧɨ ɭɧɢɤɚɥɶɧɵɣ. Ʉɚɠɞɵɣ ɤɨɦɩɨɧɟɧɬ ɞɨɥɠɟɧ ɢɦɟɬɶ ɫɜɨɣ ɭɧɢɤɚɥɶɧɵɣ ɧɨɦɟɪ. ɗɬɨɬ ɧɨɦɟɪ ɧɚɡɵɜɚɸɬ CLSID (Class Identifier), ɱɬɨ ɨɡɧɚɱɚɟɬ ɢɞɟɧɬɢɮɢɤɚɬɨɪ ɤɥɚɫɫɚ. ɉɪɢɦɟɱɚɧɢɟ 1.2. Ʉɨɝɞɚ ɢɫɩɨɥɶɡɭɟɬɫɹ GUID, ɫɨɫɬɚɜɥɟɧɧɵɣ ɢɡ ɨɱɟɧɶ ɦɚɥɟɧɶɤɢɯ ɱɢɫɟɥ, ɷɬɨ, ɤɚɤ ɩɪɚɜɢɥɨ, ɹɜɥɹɟɬɫɹ ɩɪɢɡɧɚɤɨɦ ɩɪɢɧɚɞɥɟɠɧɨɫɬɢ ɤɨɦɩɨɧɟɧɬɚ ɤ ɹɞɪɭ, ɪɚɡɪɚɛɨɬɚɧɧɨɦɭ Microsoft. GUID, ɫɝɟɧɟɪɢɪɨɜɚɧɧɵɟ 8
ɞɪɭɝɢɦɢ ɩɪɨɢɡɜɨɞɢɬɟɥɹɦɢ, ɛɭɞɭɬ ɜɵɝɥɹɞɟɬɶ ɛɨɥɟɟ ɫɥɭɱɚɣɧɵɦɢ ɢ ɧɟ ɛɭɞɭɬ ɢɦɟɬɶ ɫɬɨɥɶɤɨ ɧɭɥɟɣ.
1.4. Ⱦɨɫɬɭɩ ɤ ɤɨɦɩɨɧɟɧɬɚɦ. ɂɧɬɟɪɮɟɣɫɵ Ɏɚɤɬɢɱɟɫɤɢ ɪɟɚɥɢɡɚɰɢɹ ɤɨɦɩɨɧɟɧɬɚ ɡɚɤɪɵɬɚ ɞɥɹ ɩɪɢɥɨɠɟɧɢɹ. ɉɨɥɶɡɨɜɚɬɟɥɹ ɧɟ ɞɨɥɠɧɵ ɢɧɬɟɪɟɫɨɜɚɬɶ ɫɩɨɫɨɛɵ ɪɟɚɥɢɡɚɰɢɢ ɤɨɦɩɨɧɟɧɬɚ, ɟɦɭ ɧɟɨɛɯɨɞɢɦɨ ɡɧɚɬɶ ɬɨɥɶɤɨ ɮɭɧɤɰɢɨɧɚɥɶɧɵɟ ɜɨɡɦɨɠɧɨɫɬɢ, ɤɨɬɨɪɵɟ ɦɨɠɟɬ ɩɪɟɞɨɫɬɚɜɢɬɶ ɤɨɦɩɨɧɟɧɬ. Ⱦɨɫɬɭɩ ɤ ɤɨɦɩɨɧɟɧɬɚɦ ɨɫɭɳɟɫɬɜɥɹɟɬɫɹ ɱɟɪɟɡ ɢɧɬɟɪɮɟɣɫ, ɤɨɬɨɪɵɣ ɢɝɪɚɟɬ ɪɨɥɶ ɤɨɧɬɪɚɤɬɚ (ɞɨɝɨɜɨɪɚ) ɦɟɠɞɭ ɨɛɴɟɤɬɨɦ ɢ ɩɨɥɶɡɨɜɚɬɟɥɟɦ. ɉɨ ɫɭɳɟɫɬɜɭ ɢɧɬɟɪɮɟɣɫ ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ ɫɩɢɫɨɤ ɭɤɚɡɚɬɟɥɟɣ ɧɚ ɩɪɨɰɟɞɭɪɵ ɢ ɮɭɧɤɰɢɢ, ɪɟɚɥɢɡɨɜɚɧɧɵɟ ɜ ɤɨɦɩɨɧɟɧɬɟ. Ɍɚɤɢɦ ɨɛɪɚɡɨɦ, ɢɧɬɟɪɮɟɣɫ ɩɨɯɨɠ ɧɚ ɚɛɫɬɪɚɤɬɧɵɣ ɤɥɚɫɫ, ɜ ɤɨɬɨɪɨɦ ɩɟɪɟɱɢɫɥɟɧɵ ɜɢɪɬɭɚɥɶɧɵɟ ɩɪɨɰɟɞɭɪɵ ɢ ɮɭɧɤɰɢɢ, ɪɟɚɥɢɡɚɰɢɹ ɤɨɬɨɪɵɯ ɥɨɠɢɬɫɹ ɧɚ ɞɨɱɟɪɧɢɟ ɤɥɚɫɫɵ. ɂɧɵɦɢ ɫɥɨɜɚɦɢ, ɢɧɬɟɪɮɟɣɫ ɨɩɪɟɞɟɥɹɟɬ ɩɨɜɟɞɟɧɢɟ ɨɛɴɟɤɬɚ, ɧɨ ɧɟ ɩɨɞɞɟɪɠɢɜɚɟɬ ɫɪɟɞɫɬɜɚ ɪɟɚɥɢɡɚɰɢɢ ɷɬɨɝɨ ɩɨɜɟɞɟɧɢɹ. ȿɳɟ ɨɞɧɢɦ ɜɚɠɧɵɦ ɫɜɨɣɫɬɜɨɦ COM-ɤɨɦɩɨɧɟɧɬɨɜ ɹɜɥɹɟɬɫɹ ɜɨɡɦɨɠɧɨɫɬɶ ɩɨɞɞɟɪɠɤɢ ɫɪɚɡɭ ɧɟɫɤɨɥɶɤɢɯ ɢɧɬɟɪɮɟɣɫɨɜ. ɗɬɨ ɞɚɟɬ ɜɨɡɦɨɠɧɨɫɬɶ ɪɚɡɛɢɬɶ ɛɨɥɶɲɨɣ ɧɚɛɨɪ ɩɪɨɰɟɞɭɪ ɧɚ ɧɟɫɤɨɥɶɤɨ ɧɟɛɨɥɶɲɢɯ ɥɨɝɢɱɟɫɤɢɯ ɝɪɭɩɩ. ɉɪɢɦɟɱɚɧɢɟ 1.3. Ɋɟɚɥɢɡɨɜɚɧɧɵɟ ɢ ɢɫɩɨɥɶɡɭɟɦɵɟ ɢɧɬɟɪɮɟɣɫɵ ɧɟ ɦɟɧɹɸɬɫɹ ɧɢɤɨɝɞɚ. ɉɪɢ ɦɨɞɢɮɢɤɚɰɢɢ ɤɨɦɩɨɧɟɧɬɚ ɫɭɳɟɫɬɜɭɸɳɢɣ ɢɧɬɟɪɮɟɣɫ ɧɟ ɦɟɧɹɟɬɫɹ, ɚ ɞɨɛɚɜɥɹɟɬɫɹ ɧɨɜɵɣ. ɗɬɨ ɨɛɟɫɩɟɱɢɜɚɟɬ ɤɨɧɬɟɤɫɬɧɭɸ ɧɟɡɚɜɢɫɢɦɨɫɬɶ COM. Ʉɚɤ ɢ ɤɨɦɩɨɧɟɧɬɵ, ɢɧɬɟɪɮɟɣɫɵ ɬɨɠɟ ɢɦɟɸɬ ɫɜɨɣ ɧɨɦɟɪ, ɤɨɬɨɪɵɣ ɧɚɡɵɜɚɸɬ IID (Interface Identifier), ɬɨ ɟɫɬɶ ɢɞɟɧɬɢɮɢɤɚɬɨɪ ɢɧɬɟɪɮɟɣɫɚ. ɗɬɨ ɩɨɡɜɨɥɹɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶ ɢɧɬɟɪɮɟɣɫɵ, ɧɟ ɨɩɚɫɚɹɫɶ ɫɨɜɩɚɞɟɧɢɹ ɢɦɟɧ.
1.5. ɂɟɪɚɪɯɢɹ ɢɧɬɟɪɮɟɣɫɨɜ. ɂɧɬɟɪɮɟɣɫ IUnknown ȼ ɨɫɧɨɜɟ ɜɫɟɯ COM-ɢɧɬɟɪɮɟɣɫɨɜ ɥɟɠɢɬ ɢɧɬɟɪɮɟɣɫ ɫ ɢɦɟɧɟɦ IUnknown. ɗɬɨɬ ɢɧɬɟɪɮɟɣɫ ɩɨɞɞɟɪɠɢɜɚɟɬ ɬɪɢ ɜɚɠɧɵɟ ɮɭɧɤɰɢɢ, ɤɨɬɨɪɵɟ ɧɚɫɥɟɞɭɸɬɫɹ ɤɚɠɞɨɦɭ ɢɧɬɟɪɮɟɣɫɭ: IUnknown=interface [‘{00000000-0000-0000-C000-000000000046}’] function QueryInterface(const IID: TGUID; out obj): HResult; stdcall; function _AddRef: Integer; stdcall; function _Release: Integer; stdcall; end;
9
ɉɪɢ ɞɢɧɚɦɢɱɟɫɤɨɣ ɡɚɝɪɭɡɤɟ ɤɨɦɩɨɧɟɧɬɚ ɫɢɫɬɟɦɚ ɧɟ ɡɧɚɟɬ, ɤɚɤɢɟ ɩɨɞɞɟɪɠɢɜɚɸɬɫɹ ɢɧɬɟɪɮɟɣɫɵ, ɧɨ ɡɧɚɟɬ, ɱɬɨ ɩɪɹɦɨ ɢɥɢ ɤɨɫɜɟɧɧɨ ɩɨɞɞɟɪɠɢɜɚɟɬɫɹ ɢɧɬɟɪɮɟɣɫ IUnknown, ɜ ɤɨɬɨɪɨɦ ɪɟɚɥɢɡɨɜɚɧɚ ɮɭɧɤɰɢɹ QueryInterface ɫɨ ɫɥɟɞɭɸɳɢɦɢ ɩɚɪɚɦɟɬɪɚɦɢ: IID – ɢɞɟɧɬɢɮɢɤɚɬɨɪ ɡɚɩɪɚɲɢɜɚɟɦɨɝɨ ɢɧɬɟɪɮɟɣɫɚ; Obj – ɩɪɟɞɧɚɡɧɚɱɟɧ ɞɥɹ ɯɪɚɧɟɧɢɹ ɜɨɡɜɪɚɳɚɟɦɨɝɨ ɢɧɬɟɪɮɟɣɫɧɨɝɨ ɭɤɚɡɚɬɟɥɹ. Ɏɭɧɤɰɢɹ QueryInterface ɞɨɥɠɧɚ ɛɵɬɶ ɪɟɚɥɢɡɨɜɚɧɚ ɜ ɤɨɦɩɨɧɟɧɬɟ ɬɚɤɢɦ ɨɛɪɚɡɨɦ, ɱɬɨɛɵ ɦɨɠɧɨ ɛɵɥɨ ɩɨɥɭɱɢɬɶ ɥɸɛɨɣ ɩɨɞɞɟɪɠɢɜɚɟɦɵɣ ɢɧɬɟɪɮɟɣɫ. ȿɫɥɢ ɡɚɩɪɚɲɢɜɚɟɦɵɣ ɢɧɬɟɪɮɟɣɫ ɜ ɤɨɦɩɨɧɟɧɬɟ ɪɟɚɥɢɡɨɜɚɧ, QueryInterface ɜɨɡɜɪɚɳɚɟɬ ɭɤɚɡɚɬɟɥɶ ɧɚ ɢɧɬɟɪɮɟɣɫ ɜ ɩɚɪɚɦɟɬɪɟ obj ɢ ɡɧɚɱɟɧɢɟ S_OK, ɢɧɚɱɟ – E_NOINTERFACE. ɉɨɫɥɟ ɪɚɛɨɬɵ ɫ ɤɨɦɩɨɧɟɧɬɨɦ ɧɟɨɛɯɨɞɢɦɨ ɟɝɨ ɜɵɝɪɭɡɢɬɶ ɢɡ ɩɚɦɹɬɢ. ɋɥɨɠɧɨɫɬɢ ɦɨɝɭɬ ɜɨɡɧɢɤɧɭɬɶ, ɤɨɝɞɚ ɧɚ ɤɨɦɩɨɧɟɧɬ, ɜɟɪɧɟɟ, ɧɚ ɟɝɨ ɢɧɬɟɪɮɟɣɫɵ, ɢɦɟɟɬɫɹ ɧɟɫɤɨɥɶɤɨ ɫɫɵɥɨɤ. ȼ ɛɨɥɶɲɢɯ ɩɪɨɟɤɬɚɯ ɭɫɥɟɞɢɬɶ ɡɚ ɷɬɢɦ ɩɪɚɤɬɢɱɟɫɤɢ ɧɟɜɨɡɦɨɠɧɨ. ɑɬɨɛɵ ɢɡɛɟɠɚɬɶ ɬɚɤɨɣ ɫɢɬɭɚɰɢɢ ɢɫɩɨɥɶɡɭɟɬɫɹ ɫɱɟɬɱɢɤ ɫɫɵɥɨɤ. Ʉɨɝɞɚ ɤɥɢɟɧɬ ɩɨɥɭɱɚɟɬ ɧɟɤɨɬɨɪɵɣ ɢɧɬɟɪɮɟɣɫ, ɡɧɚɱɟɧɢɟ ɷɬɨɝɨ ɫɱɟɬɱɢɤɚ ɭɜɟɥɢɱɢɜɚɟɬɫɹ ɧɚ ɟɞɢɧɢɰɭ. Ʉɨɝɞɚ ɤɥɢɟɧɬ ɡɚɤɚɧɱɢɜɚɟɬ ɪɚɛɨɬɭ ɫ ɢɧɬɟɪɮɟɣɫɨɦ, ɡɧɚɱɟɧɢɟ ɧɚ ɟɞɢɧɢɰɭ ɭɦɟɧɶɲɚɟɬɫɹ. Ʉɨɝɞɚ ɨɧɨ ɞɨɯɨɞɢɬ ɞɨ ɧɭɥɹ, ɤɨɦɩɨɧɟɧɬ ɭɞɚɥɹɟɬ ɫɟɛɹ ɢɡ ɩɚɦɹɬɢ. Ʉɥɢɟɧɬ ɬɚɤɠɟ ɭɜɟɥɢɱɢɜɚɟɬ ɫɱɟɬɱɢɤ ɫɫɵɥɨɤ, ɤɨɝɞɚ ɫɨɡɞɚɟɬ ɧɨɜɭɸ ɫɫɵɥɤɭ ɧɚ ɭɠɟ ɢɦɟɸɳɢɣɫɹ ɭ ɧɟɝɨ ɢɧɬɟɪɮɟɣɫ. Ⱦɥɹ ɩɪɨɜɟɞɟɧɢɹ ɷɬɢɯ ɨɩɟɪɚɰɢɣ ɩɪɟɞɧɚɡɧɚɱɟɧɵ ɮɭɧɤɰɢɢ _AddRef ɢ _Release. _AddRef – ɭɜɟɥɢɱɢɜɚɟɬ ɫɱɟɬɱɢɤ ɫɫɵɥɨɤ ɧɚ ɟɞɢɧɢɰɭ ɢ ɜɨɡɜɪɚɳɚɟɬ ɤɨɥɢɱɟɫɬɜɨ ɫɫɵɥɨɤ. _Release – ɭɦɟɧɶɲɚɟɬ ɫɱɟɬɱɢɤ ɫɫɵɥɨɤ ɧɚ ɟɞɢɧɢɰɭ ɢ ɜɨɡɜɪɚɳɚɟɬ ɤɨɥɢɱɟɫɬɜɨ ɫɫɵɥɨɤ. ȿɫɥɢ ɫɱɟɬɱɢɤ ɪɚɜɟɧ ɧɭɥɸ, ɭɧɢɱɬɨɠɚɟɬ ɨɛɴɟɤɬ. Ⱦɥɹ ɤɨɪɪɟɤɬɧɨɣ ɪɚɛɨɬɵ ɩɪɢɥɨɠɟɧɢɹ ɢ ɤɨɦɩɨɧɟɧɬɨɜ ɧɟɨɛɯɨɞɢɦɨ ɫɬɪɨɝɨ ɫɨɛɥɸɞɚɬɶ ɬɪɢ ɩɪɚɜɢɥɚ, ɩɪɢɧɹɬɵɯ ɩɪɢ ɪɚɛɨɬɟ ɫ COM: 1. Ɏɭɧɤɰɢɢ, ɜɨɡɜɪɚɳɚɸɳɢɟ ɢɧɬɟɪɮɟɣɫɵ, ɩɟɪɟɞ ɜɨɡɜɪɚɬɨɦ ɜɫɟɝɞɚ ɞɨɥɠɧɵ ɜɵɡɜɚɬɶ _AddRef ɞɥɹ ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɝɨ ɭɤɚɡɚɬɟɥɹ. ɗɬɨ ɬɚɤɠɟ ɨɬɧɨɫɢɬɫɹ ɢ ɤ QueryInterface. 2. ɉɨɫɥɟ ɡɚɜɟɪɲɟɧɢɹ ɪɚɛɨɬɵ ɫ ɢɧɬɟɪɮɟɣɫɨɦ ɫɥɟɞɭɟɬ ɜɵɡɜɚɬɶ ɞɥɹ ɧɟɝɨ _Release. 3. ȼɫɟɝɞɚ ɫɥɟɞɭɟɬ ɜɵɡɵɜɚɬɶ _AddRef, ɤɨɝɞɚ ɫɨɡɞɚɟɬɫɹ ɧɨɜɚɹ ɫɫɵɥɤɚ ɧɚ ɞɚɧɧɵɣ ɢɧɬɟɪɮɟɣɫ. ɉɪɢɜɟɞɟɦ ɩɪɢɦɟɪ ɪɟɚɥɢɡɚɰɢɢ QueryInterface, _AddRef, _Release ɜ ɧɟɤɨɟɦ Ʉɨɦɩɨɧɟɧɬɟ TSomeObject: type TSomeObject=class(TObject, ISomeInterface) private 10
FRefCount: integer; <ɪɟɚɥɢɡɚɰɢɹ ɡɚɤɪɵɬɨɣ ɱɚɫɬɢ> public //ɨɩɢɫɚɧɢɟ ɢɧɬɟɪɮɟɣɫɚ IUnknown function QueryInterface(const IID:TGUID; out obj):HResult; stdcall; function _AddRef: Integer; stdcall; function _Release: Integer; stdcall; <ɨɩɢɫɚɧɢɟ ɨɬɤɪɵɬɨɣ ɱɚɫɬɢ> end; <…………> function TSomeObject. QueryInterface(const obj):HResult; begin if GetInterface(IID, Obj) then begin Result:=S_OK; _AddRef; end else Result:=E_NOINTERFACE; end;
IID:TGUID;
out
1.6. ɉɨɪɬɚɬɢɜɧɨɫɬɶ ɢ ɤɨɦɦɭɧɢɤɚɛɟɥɶɧɨɫɬɶ COM. Ɋɟɝɢɫɬɪɚɰɢɹ COM-ɫɟɪɜɟɪɨɜ. ɋɢɫɬɟɦɧɵɣ ɪɟɟɫɬɪ Windows ȿɳɺ ɨɞɧɨ ɝɥɚɜɧɨɟ ɩɪɚɜɢɥɨ COM-ɬɟɯɧɨɥɨɝɢɢ – ɨɬɫɭɬɫɬɜɢɟ ɤɨɧɬɟɤɫɬɧɨɣ ɡɚɜɢɫɢɦɨɫɬɢ ɤɥɢɟɧɬɚ ɢ ɫɟɪɜɟɪɚ. ɉɨɞ ɤɨɧɬɟɤɫɬɧɨɣ ɧɟɡɚɜɢɫɢɦɨɫɬɶɸ ɩɨɞɪɚɡɭɦɟɜɚɟɬɫɹ, ɱɬɨ ɤɥɢɟɧɬ ɞɨɥɠɟɧ ɩɨɥɭɱɢɬɶ ɫɜɹɡɶ ɫ ɫɟɪɜɟɪɨɦ, ɝɞɟ ɛɵ ɨɧ ɧɢ ɧɚɯɨɞɢɥɫɹ. ɋɜɹɡɶ ɤɥɢɟɧɬɚ ɫ ɫɟɪɜɟɪɨɦ ɛɟɪɺɬ ɧɚ ɫɟɛɹ ɨɩɟɪɚɰɢɨɧɧɚɹ ɫɢɫɬɟɦɚ, ɤɨɬɨɪɚɹ ɫɨɞɟɪɠɢɬ ɫɢɫɬɟɦɧɭɸ ɛɚɡɭ ɞɚɧɧɵɯ, ɧɚɡɵɜɚɟɦɭɸ ɫɢɫɬɟɦɧɵɦ ɪɟɟɫɬɪɨɦ, ɝɞɟ ɩɪɨɩɢɫɵɜɚɟɬɫɹ ɧɚɡɜɚɧɢɟ COM-ɫɟɪɜɟɪɚ, ɭɧɢɤɚɥɶɧɵɣ ɧɨɦɟɪ ɤɨɦɩɨɧɟɧɬɚ CLSID ɢ ɞɪɭɝɚɹ ɢɧɮɨɪɦɚɰɢɹ. ɉɪɢɦɟɱɚɧɢɟ 1.4. Ɋɟɟɫɬɪ ɫɨɞɟɪɠɢɬ ɢɧɮɨɪɦɚɰɢɸ ɨɛ ɚɩɩɚɪɚɬɧɨɦ ɢ ɩɪɨɝɪɚɦɦɧɨɦ ɨɛɟɫɩɟɱɟɧɢɢ, ɨ ɤɨɧɮɢɝɭɪɚɰɢɢ ɤɨɦɩɶɸɬɟɪɚ ɢ ɨ ɩɨɥɶɡɨɜɚɬɟɥɹɯ. Ʌɸɛɚɹ ɩɪɨɝɪɚɦɦɚ ɞɥɹ Windows ɦɨɠɟɬ ɞɨɛɚɜɥɹɬɶ ɢ ɫɱɢɬɵɜɚɬɶ ɢɧɮɨɪɦɚɰɢɸ ɢɡ ɪɟɟɫɬɪɚ. Ɋɟɟɫɬɪ ɢɦɟɟɬ ɢɟɪɚɪɯɢɱɟɫɤɭɸ ɫɬɪɭɤɬɭɪɭ ɜ ɜɢɞɟ ɞɟɪɟɜɚ, ɝɞɟ ɭɡɥɚɦɢ ɹɜɥɹɸɬɫɹ ɪɚɡɞɟɥɵ, ɚ ɥɢɫɬɶɹɦɢ – ɞɚɧɧɵɟ. Ⱦɥɹ ɩɪɨɫɦɨɬɪɚ ɢ ɪɟɞɚɤɬɢɪɨɜɚɧɢɹ ɪɟɟɫɬɪɚ ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɫɬɚɧɞɚɪɬɧɭɸ ɩɪɨɝɪɚɦɦɭ regedit.exe – ɪɟɞɚɤɬɨɪ ɪɟɟɫɬɪɚ (ɪɢɫ. 1.3).
function TSomeObject._AddRef: Integer; begin Inc(FRefCount); Result:=FRefCount; end; function TSomeObject._Release: Integer; begin Dec(FRefCount); Result:=FRefCount; if FRefCount=0 then destroy; end; <…………> ȼ ɩɪɢɦɟɪɟ ɨɩɢɫɚɧ ɨɛɴɟɤɬ TSomeObject, ɩɨɞɞɟɪɠɢɜɚɸɳɢɣ ɢɧɬɟɪɮɟɣɫ ISomeInterface, ɢ ɩɨɤɚɡɚɧɚ ɪɟɚɥɢɡɚɰɢɹ ɮɭɧɤɰɢɣ QueryInterface, _AddRef ɢ _Release.
Ɋɢɫ. 1.3. Ɋɟɞɚɤɬɨɪ ɪɟɟɫɬɪɚ
11
12
ɉɪɢɦɟɱɚɧɢɟ 1.5. ɋɢɫɬɟɦɧɵɣ ɪɟɟɫɬɪ ɹɜɥɹɟɬɫɹ ɨɱɟɧɶ ɜɚɠɧɨɣ ɫɭɳɧɨɫɬɶɸ ɜ ɨɩɟɪɚɰɢɨɧɧɨɣ ɫɢɫɬɟɦɟ. ɍɞɚɥɟɧɢɟ ɢɥɢ ɢɡɦɟɧɟɧɢɟ ɜ ɧɺɦ ɤɚɤɢɯ-ɥɢɛɨ ɞɚɧɧɵɯ ɦɨɠɟɬ ɩɪɢɜɟɫɬɢ ɤ ɧɟɜɨɡɦɨɠɧɨɫɬɢ ɪɚɛɨɬɵ ɧɟɤɨɬɨɪɵɯ ɩɪɢɥɨɠɟɧɢɣ, ɚ ɬɨ ɢ ɜɫɟɣ ɨɩɟɪɚɰɢɨɧɧɨɣ ɫɢɫɬɟɦɵ. ȼɫɟ ɢɡɦɟɧɟɧɢɹ ɜ ɪɟɟɫɬɪɟ ɡɚɩɢɫɵɜɚɸɬɫɹ ɫɪɚɡɭ ɧɚ ɞɢɫɤ, ɨɬɤɚɬɨɜ ɜ ɫɢɫɬɟɦɟ ɧɟɬ. ɍɞɚɥɟɧɢɟ ɢɥɢ ɢɡɦɟɧɟɧɢɟ ɞɚɧɧɵɯ ɜ ɫɢɫɬɟɦɧɨɦ ɪɟɟɫɬɪɟ ɛɟɡ ɱɟɬɤɨɝɨ ɩɨɧɢɦɚɧɢɹ ɫɭɬɢ ɩɪɨɢɫɯɨɞɹɳɟɝɨ ɤɪɚɣɧɟ ɨɩɚɫɧɨ. ɉɨɩɵɬɤɢ ɮɟɧɨɦɟɧɨɥɨɝɢɱɟɫɤɨɝɨ ɢɫɫɥɟɞɨɜɚɧɢɹ “ɱɬɨ ɛɭɞɟɬ, ɟɫɥɢ ɭɞɚɥɢɬɶ ɜɨɬ ɷɬɨ…” ɦɨɝɭɬ ɩɥɨɯɨ ɤɨɧɱɢɬɶɫɹ. Ⱦɚɧɧɵɟ ɨɛ ɨɛɴɟɤɬɚɯ COM ɯɪɚɧɹɬɫɹ ɜ ɩɨɞɪɚɡɞɟɥɟ CLSID ɪɚɡɞɟɥɚ HKEY_CLASSES_ROOT. ȼ ɷɬɨɦ ɪɚɡɞɟɥɟ ɩɟɪɟɱɢɫɥɟɧɵ CLSID ɜɫɟɯ ɤɨɦɩɨɧɟɧɬɨɜ, ɭɫɬɚɧɨɜɥɟɧɧɵɯ ɜ ɫɢɫɬɟɦɟ. Ʉɚɠɞɵɣ CLSID ɫɨɞɟɪɠɢɬ ɩɚɪɚɦɟɬɪ ɩɨ ɭɦɨɥɱɚɧɢɸ, ɧɚɡɵɜɚɟɦɵɣ «ɞɪɭɠɟɫɬɜɟɧɧɵɦ» ɢɦɟɧɟɦ ɤɨɦɩɨɧɟɧɬɚ. ȼ ɪɚɡɞɟɥɟ ɨɩɢɫɚɧɢɹ ɤɨɦɩɨɧɟɧɬɚ ɟɫɬɶ ɩɨɞɪɚɡɞɟɥ LocalServer32 (ɞɥɹ ɜɧɟɲɧɢɯ COM-ɫɟɪɜɟɪɨɜ) ɢɥɢ InprocServer32 (ɞɥɹ ɜɧɭɬɪɟɧɧɢɯ COM-ɫɟɪɜɟɪɨɜ), ɫɨɞɟɪɠɚɳɢɯ ɢɦɹ ɩɪɢɥɨɠɟɧɢɹ ɢɥɢ DLL, ɜ ɤɨɬɨɪɵɯ ɧɚɯɨɞɢɬɫɹ ɤɨɦɩɨɧɟɧɬ. ɂɦɹ ɮɚɣɥɚ ɢ CLSID – ɷɬɨ ɧɚɢɛɨɥɟɟ ɜɚɠɧɵɟ ɞɚɧɧɵɟ ɞɥɹ ɧɨɪɦɚɥɶɧɨɝɨ ɮɭɧɤɰɢɨɧɢɪɨɜɚɧɢɹ, ɧɨ ɞɥɹ ɧɟɤɨɬɨɪɵɯ, ɛɨɥɟɟ ɫɥɨɠɧɵɯ ɤɨɦɩɨɧɟɧɬɨɜ COM, ɯɪɚɧɢɬɫɹ ɢ ɞɪɭɝɚɹ ɢɧɮɨɪɦɚɰɢɹ. ɇɚɩɪɢɦɟɪ, ɷɬɨ ɧɨɦɟɪ ɛɢɛɥɢɨɬɟɤɢ ɬɢɩɨɜ, ɩɨɞɞɟɪɠɢɜɚɟɦɨɣ ɞɚɧɧɵɦ COM-ɫɟɪɜɟɪɨɦ (TypeLib), ɧɨɦɟɪ ɜɟɪɫɢɢ COM-ɫɟɪɜɟɪɚ (Version), ɩɪɨɝɪɚɦɦɧɨɟ ɢɦɹ (ProgID) ɢ ɬ. ɩ. ɑɬɨɛɵ ɡɚɪɟɝɢɫɬɪɢɪɨɜɚɬɶ ɜɫɬɪɨɟɧɧɵɣ COM-ɫɟɪɜɟɪ ɜ ɫɢɫɬɟɦɧɨɦ ɪɟɟɫɬɪɟ, ɧɟɨɛɯɨɞɢɦɨ ɜɵɡɜɚɬɶ ɮɭɧɤɰɢɸ DllRegisterServer, ɧɚɯɨɞɹɳɭɸɫɹ ɜɧɭɬɪɢ DLL-ɦɨɞɭɥɹ COM-ɫɟɪɜɟɪɚ. ɗɬɨ ɦɨɠɧɨ ɫɞɟɥɚɬɶ ɜ ɩɪɢɥɨɠɟɧɢɢ, ɩɪɟɞɜɚɪɢɬɟɥɶɧɨ ɡɚɝɪɭɡɢɜ DLL, ɢɥɢ ɫ ɩɨɦɨɳɶɸ ɫɬɚɧɞɚɪɬɧɨɣ ɭɬɢɥɢɬɵ Windows RegSvr32.exe, ɜɵɩɨɥɧɢɜ ɤɨɦɚɧɞɭ: RegSvr32 -s <ɢɦɹ ɫɟɪɜɟɪɚ> ɉɪɢ ɭɞɚɥɟɧɢɢ ɜɫɬɪɨɟɧɧɨɝɨ ɫɟɪɜɟɪɚ ɢɡ ɫɢɫɬɟɦɵ ɧɟɨɛɯɨɞɢɦɨ ɡɚɩɭɫɬɢɬɶ ɮɭɧɤɰɢɸ DllUnregisterServer. ȼ Windows ɭɞɚɥɟɧɢɟ ɫɟɪɜɟɪɚ ɨɫɭɳɟɫɬɜɥɹɟɬɫɹ ɤɨɦɚɧɞɨɣ:
II. Ɋɚɡɪɚɛɨɬɤɚ COM-ɫɟɪɜɟɪɨɜ ɜ Delphi 2.1. Ɋɚɡɪɚɛɨɬɤɚ ɢɧɬɟɪɮɟɣɫɚ ɉɪɟɠɞɟ, ɱɟɦ ɪɟɚɥɢɡɨɜɵɜɚɬɶ COM-ɫɟɪɜɟɪ, ɧɭɠɧɨ ɪɚɡɪɚɛɨɬɚɬɶ ɢɧɬɟɪɮɟɣɫ. ɂɧɬɟɪɮɟɣɫ ɹɜɥɹɟɬɫɹ ɫɜɹɡɶɸ ɦɟɠɞɭ ɤɥɢɟɧɬɨɦ ɢ ɫɟɪɜɟɪɨɦ (ɩ. 1.4). ȼ Delphi ɢɧɬɟɪɮɟɣɫɵ ɨɩɢɫɵɜɚɸɬɫɹ ɜ ɩɪɨɝɪɚɦɦɧɨɦ ɦɨɞɭɥɟ ɩɨ ɫɥɟɞɭɸɳɟɣ ɫɬɪɭɤɬɭɪɟ: <ɢɦɹ ɢɧɬɟɪɮɟɣɫɚ>=interface [<ɫɬɪɨɤɚ ɫ IID>] {ɫɩɢɫɨɤ ɩɪɨɰɟɞɭɪ ɢ ɮɭɧɤɰɢɣ} end; ɂɧɬɟɪɮɟɣɫɵ, ɤɚɤ ɢ ɤɥɚɫɫɵ, ɨɛɥɚɞɚɸɬ ɫɜɨɣɫɬɜɨɦ ɧɚɫɥɟɞɨɜɚɧɢɹ: <ɢɦɹ ɢɧɬɟɪɮɟɣɫɚ>=interface(<ɢɦɹ ɪɨɞɢɬɟɥɶɫɤɨɝɨ ɢɧɬɟɪɮɟɣɫɚ>) [<ɫɬɪɨɤɚ ɫ IID>] {ɫɩɢɫɨɤ ɩɪɨɰɟɞɭɪ ɢ ɮɭɧɤɰɢɣ} end; ɉɪɢɦɟɱɚɧɢɟ 2.1. ȼ ɬɟɯɧɨɥɨɝɢɢ COM ɩɪɢɧɹɬɨ ɧɚɡɜɚɧɢɟ ɢɧɬɟɪɮɟɣɫɨɜ ɧɚɱɢɧɚɬɶ ɫ ɩɪɟɮɢɤɫɚ «I» (IUnknown, IDispatch, IMyInterface ɢ ɬ. ɩ.). ɉɪɢɦɟɱɚɧɢɟ 2.2. IID ɞɨɥɠɟɧ ɛɵɬɶ ɭɧɢɤɚɥɶɧɵɦ, ɩɨɷɬɨɦɭ, ɜɫɹɤɢɣ ɪɚɡ ɪɚɡɪɚɛɚɬɵɜɚɹ ɧɨɜɵɣ ɢɧɬɟɪɮɟɣɫ, ɢɫɩɨɥɶɡɭɣɬɟ ɝɟɧɟɪɚɬɨɪ ɭɧɢɤɚɥɶɧɵɯ ɢɞɟɧɬɢɮɢɤɚɬɨɪɨɜ ɫɢɫɬɟɦɵ (ɜ Delphi ɷɬɨ ɤɨɦɛɢɧɚɰɢɹ ɤɥɚɜɢɲ Ctrl+Shift+G). ɉɪɢɦɟɱɚɧɢɟ 2.3. ɉɪɢ ɨɩɢɫɚɧɢɢ ɦɟɬɨɞɨɜ ɢɧɬɟɪɮɟɣɫɨɜ ɧɟɨɛɯɨɞɢɦɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɞɢɪɟɤɬɢɜɭ stdcall, ɤɨɬɨɪɚɹ ɨɩɪɟɞɟɥɹɟɬ ɩɨɪɹɞɨɤ ɩɟɪɟɞɚɱɢ ɩɚɪɚɦɟɬɪɨɜ ɢ ɜɵɡɨɜɚ ɮɭɧɤɰɢɣ ɜ ɫɨɨɬɜɟɬɫɬɜɢɢ ɫɨ ɫɬɚɧɞɚɪɬɨɦ, ɩɪɢɧɹɬɵɦ ɜ ɨɩɟɪɚɰɢɨɧɧɨɣ ɫɢɫɬɟɦɟ Windows.
Ⱦɥɹ ɪɟɝɢɫɬɪɚɰɢɢ ɜɧɟɲɧɢɯ COM-ɫɟɪɜɟɪɨɜ ɬɪɟɛɭɟɬɫɹ ɡɚɩɭɫɬɢɬɶ ɩɪɢɥɨɠɟɧɢɟ, ɜɤɥɸɱɢɜ ɜ ɤɨɦɚɧɞɧɭɸ ɫɬɪɨɤɭ ɡɚɩɭɫɤɚ ɤɥɸɱ /regserver, ɚ ɞɥɹ ɭɞɚɥɟɧɢɹ ɧɭɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɤɥɸɱ /unregserver. ȼɫɬɪɨɟɧɧɵɣ COMɫɟɪɜɟɪ ɪɟɝɢɫɬɪɢɪɭɟɬɫɹ ɢ ɩɪɢ ɩɟɪɜɨɦ ɡɚɩɭɫɤɟ ɩɪɢɥɨɠɟɧɢɹ ɛɟɡ ɜɫɹɤɢɯ ɞɨɩɨɥɧɢɬɟɥɶɧɵɯ ɤɥɸɱɟɣ ɜ ɤɨɦɚɧɞɧɨɣ ɫɬɪɨɤɟ, ɧɨ ɩɨɫɥɟ ɷɬɨɝɨ ɫɟɪɜɟɪ ɛɭɞɟɬ ɩɪɨɞɨɥɠɚɬɶ ɪɚɛɨɬɚɬɶ.
ȼɫɟ ɢɧɬɟɪɮɟɣɫɵ ɩɪɹɦɨ ɢɥɢ ɤɨɫɜɟɧɧɨ ɧɚɫɥɟɞɭɸɬɫɹ ɨɬ Iunknown, ɤɨɬɨɪɵɣ ɫɨɞɟɪɠɢɬ ɜɚɠɧɵɟ ɮɭɧɤɰɢɢ ɞɥɹ ɪɟɚɥɢɡɚɰɢɢ ɬɟɯɧɨɥɨɝɢɢ COM (ɩ. 1.5). Ⱦɥɹ ɤɨɪɪɟɤɬɧɨɣ ɪɚɛɨɬɵ ɩɪɢɥɨɠɟɧɢɹ ɜ ɢɧɬɟɪɮɟɣɫɟ ɫɥɟɞɭɟɬ ɨɩɢɫɵɜɚɬɶ ɮɭɧɤɰɢɢ, ɜɨɡɜɪɚɳɚɸɳɢɟ ɞɚɧɧɵɟ ɱɟɪɟɡ ɩɚɪɚɦɟɬɪɵ, ɚ ɜ ɤɚɱɟɫɬɜɟ ɪɟɡɭɥɶɬɚɬɚ – ɢɧɮɨɪɦɚɰɢɸ ɨ ɤɨɪɪɟɤɬɧɨɫɬɢ ɜɵɩɨɥɧɟɧɢɹ ɨɩɟɪɚɰɢɣ. ɗɬɨ ɦɨɠɟɬ ɛɵɬɶ ɭɫɥɨɜɧɵɣ ɤɨɞ ɨɲɢɛɤɢ ɢɥɢ ɠɟ ɩɪɨɫɬɨ ɢɧɮɨɪɦɚɰɢɹ ɨɛ ɭɫɩɟɲɧɨɫɬɢ ɜɵɩɨɥɧɟɧɢɹ, ɧɚɩɪɢɦɟɪ, true – ɮɭɧɤɰɢɹ ɜɵɩɨɥɧɟɧɚ ɭɫɩɟɲɧɨ, false – ɜɨɡɧɢɤɥɢ ɨɲɢɛɤɢ. Ɍɚɤɚɹ ɪɟɚɥɢɡɚɰɢɹ ɞɚɟɬ ɜɨɡɦɨɠɧɨɫɬɶ ɤɨɧɬɪɨɥɢɪɨɜɚɬɶ ɢ ɨɛɪɚɛɚɬɵɜɚɬɶ ɨɲɢɛɤɢ ɜ ɤɥɢɟɧɬɟ.
13
14
RegSvr32 -u <ɢɦɹ ɫɟɪɜɟɪɚ>
Ʉɥɢɟɧɬ ɢ ɫɟɪɜɟɪ ɞɨɥɠɧɵ ɫɨɞɟɪɠɚɬɶ ɨɞɢɧɚɤɨɜɨɟ ɨɩɢɫɚɧɢɟ ɢɧɬɟɪɮɟɣɫɨɜ. ɉɨɷɬɨɦɭ ɢɧɬɟɪɮɟɣɫ ɥɭɱɲɟ ɨɩɢɫɚɬɶ ɜ ɨɬɞɟɥɶɧɨɦ ɦɨɞɭɥɟ, ɤɨɬɨɪɵɣ ɩɪɢɫɨɟɞɢɧɹɟɬɫɹ ɤ ɤɥɢɟɧɬɭ ɢ ɤ ɫɟɪɜɟɪɭ. ɉɪɢɜɟɞɟɦ ɩɪɢɦɟɪ ɪɟɚɥɢɡɚɰɢɢ ɢɧɬɟɪɮɟɣɫɚ. ȼ ɞɚɧɧɨɦ ɩɪɢɦɟɪɟ ɨɩɢɫɚɧ ɢɧɬɟɪɮɟɣɫ, ɫɨɞɟɪɠɚɳɢɣ ɬɪɢ ɮɭɧɤɰɢɢ ɩɟɪɟɜɨɞɚ ɰɟɥɨɝɨ ɱɢɫɥɚ ɜ ɞɜɨɢɱɧɵɣ, ɜɨɫɶɦɟɪɢɱɧɵɣ ɢ ɲɟɫɬɧɚɞɰɚɬɟɪɢɱɧɵɣ ɜɢɞ, ɤɨɬɨɪɵɣ ɛɭɞɟɬ ɜɵɜɨɞɢɬɶɫɹ ɜ ɜɢɞɟ ɫɬɪɨɤɢ. 1. Ɂɚɩɭɫɬɢɦ Delphi ɢ ɨɬɤɪɨɟɦ ɧɨɜɵɣ ɦɨɞɭɥɶ, ɜɵɛɪɚɜ ɜ ɦɟɧɸ File/New. ȼ ɩɨɹɜɢɜɲɟɦɫɹ ɞɢɚɥɨɝɨɜɨɦ ɨɤɧɟ New Items ɧɚ ɡɚɤɥɚɞɤɟ New ɜɵɛɟɪɟɦ ɷɥɟɦɟɧɬ Unit. 2. ɋɨɯɪɚɧɢɦ ɧɨɜɵɣ ɦɨɞɭɥɶ ɤɚɤ ConvertInterface.pas ɢ ɜ ɧɟɝɨ ɩɨɦɟɫɬɢɦ ɫɥɟɞɭɸɳɢɣ ɬɟɤɫɬ: unit ConvertInterface; interface type IConvert=interface ['{F1E9A4BA-4C4A-11D5-8D36-006008159451}'] function Bin (const n: Word; var Str: WideString): Boolean; stdcall; function Oct(const n: Word; var Str: WideString): Boolean; stdcall; function Hex(const n: Word; var Str: WideString): Boolean; stdcall; end; implementation end.
2.2. Ɋɟɚɥɢɡɚɰɢɹ ɜɫɬɪɨɟɧɧɨɝɨ COM-ɫɟɪɜɟɪɚ ȼɫɬɪɨɟɧɧɵɟ COM-ɫɟɪɜɟɪɵ ɪɟɚɥɢɡɨɜɚɧɵ ɜɧɭɬɪɢ ɦɨɞɭɥɟɣ DLL, ɩɨɷɬɨɦɭ ɨɧɢ ɜɵɩɨɥɧɹɸɬɫɹ ɜ ɩɪɨɰɟɫɫɟ ɩɪɢɥɨɠɟɧɢɹ, ɤɨɬɨɪɨɟ ɢɯ ɢɫɩɨɥɶɡɭɟɬ. ȼɫɥɟɞɫɬɜɢɟ ɞɚɧɧɨɝɨ ɫɜɨɣɫɬɜɚ ɨɧɢ ɢ ɩɨɥɭɱɢɥɢ ɫɜɨɟ ɧɚɡɜɚɧɢɟ. Ⱦɥɹ ɬɨɝɨ, ɱɬɨɛɵ ɫɨɡɞɚɬɶ ɜɫɬɪɨɟɧɧɵɣ COM-ɫɟɪɜɟɪ ɜ ɫɪɟɞɟ Delphi, ɧɟɨɛɯɨɞɢɦɨ ɜ ɝɥɚɜɧɨɦ ɦɟɧɸ ɜɵɛɪɚɬɶ File/New. ȼ ɩɨɹɜɢɜɲɟɦɫɹ ɞɢɚɥɨɝɨɜɨɦ ɨɤɧɟ New Items ɨɬɤɪɵɬɶ ɡɚɤɥɚɞɤɭ ActiveX (ɪɢɫ. 2.1) ɢ ɜɵɛɪɚɬɶ ɷɥɟɦɟɧɬ ActiveX Library.
15
Ɋɢɫ. 2.1. Ⱦɢɚɥɨɝɨɜɨɟ ɨɤɧɨ New Items ɧɚ ɡɚɤɥɚɞɤɟ ActiveX ȼ ɛɢɛɥɢɨɬɟɤɭ ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɩɪɨɩɢɫɵɜɚɸɬɫɹ ɱɟɬɵɪɟ ɷɤɫɩɨɪɬɢɪɭɟɦɵɟ ɮɭɧɤɰɢɢ, ɪɟɚɥɢɡɨɜɚɧɧɵɟ ɜ ɦɨɞɭɥɟ ComServ: function DllRegisterServer: HResult; stdcall; – ɢɫɩɨɥɶɡɭɟɬɫɹ ɞɥɹ ɪɟɝɢɫɬɪɚɰɢɢ COM-ɫɟɪɜɟɪɚ ɜ ɫɢɫɬɟɦɧɨɦ ɪɟɟɫɬɪɟ. ȿɫɥɢ ɪɟɝɢɫɬɪɚɰɢɹ ɩɪɨɲɥɚ ɭɫɩɟɲɧɨ, ɮɭɧɤɰɢɹ ɜɨɡɜɪɚɳɚɟɬ ɡɧɚɱɟɧɢɟ S_OK, ɢɧɚɱɟ S_FAIL. function DllUnregisterServer: HResult; stdcall; – ɨɛɪɚɬɧɚɹ ɮɭɧɤɰɢɹ ɤ DllRegisterServer: ɭɞɚɥɹɟɬ ɢɡ ɪɟɟɫɬɪɚ Windows ɜɫɟ ɷɥɟɦɟɧɬɵ, ɤɚɫɚɸɳɢɟɫɹ ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɝɨ ɤɨɦɩɨɧɟɧɬɚ. ȿɫɥɢ ɮɭɧɤɰɢɹ ɨɬɪɚɛɨɬɚɥɚ ɭɫɩɟɲɧɨ, ɬɨ ɜɨɡɜɪɚɳɚɟɬɫɹ ɡɧɚɱɟɧɢɟ S_OK, ɢɧɚɱɟ S_FAIL. function DllGetClassObject(const CLSID, IID: TGUID; var Obj): HResult; stdcall; – ɋɨɡɞɚɟɬ ɧɨɜɵɣ ɤɨɦɩɨɧɟɧɬ ɫ ɧɨɦɟɪɨɦ CLSID ɢ ɜɵɞɚɟɬ ɫɫɵɥɤɭ ɧɚ ɢɧɬɟɪɮɟɣɫ ɫ ɧɨɦɟɪɨɦ IID ɱɟɪɟɡ ɩɚɪɚɦɟɬɪ Obj. ȼɵɡɵɜɚɟɬɫɹ ɫɢɫɬɟɦɨɣ, ɪɟɚɥɢɡɭɸɳɟɣ ɚɪɯɢɬɟɤɬɭɪɭ COM. ȼ ɫɥɭɱɚɟ ɭɫɩɟɲɧɨɝɨ ɜɵɩɨɥɧɟɧɢɹ ɮɭɧɤɰɢɹ ɜɨɡɜɪɚɳɚɟɬ ɡɧɚɱɟɧɢɟ: S_OK ɢɧɚɱɟ: E_NOINTERFACE – ɟɫɥɢ ɢɧɬɟɪɮɟɣɫ ɧɟ ɧɚɣɞɟɧ ɢ CLASS_E_CLASSNOTAVAILABLE – ɤɨɦɩɨɧɟɧɬ ɫ ɬɚɤɢɦ ɧɨɦɟɪɨɦ ɧɟ ɡɚɪɟɝɢɫɬɪɢɪɨɜɚɧ ɜ ɫɢɫɬɟɦɟ. function DllCanUnloadNow: HResult; stdcall; – ɜɵɡɵɜɚɟɬɫɹ ɦɟɯɚɧɢɡɦɨɦ ɪɟɚɥɢɡɚɰɢɢ COM ɞɥɹ ɩɪɨɜɟɪɤɢ, ɜɵɩɨɥɧɹɸɬɫɹ ɥɢ ɭɫɥɨɜɢɹ ɭɞɚɥɟɧɢɹ 16
COM-ɫɟɪɜɟɪɚ ɢɡ ɩɚɦɹɬɢ. ȿɫɥɢ ɤɚɤɨɟ-ɥɢɛɨ ɩɪɢɥɨɠɟɧɢɟ ɢɦɟɟɬ ɫɫɵɥɤɭ ɧɚ ɥɸɛɨɣ ɤɨɦɩɨɧɟɧɬ ɫɟɪɜɟɪɚ, ɬɨ ɮɭɧɤɰɢɹ ɜɟɪɧɟɬ S_FALSE, ɜ ɩɪɨɬɢɜɧɨɦ ɫɥɭɱɚɟ – S_TRUE. ȼ ɩɨɫɥɟɞɧɟɦ ɫɥɭɱɚɟ ɦɟɯɚɧɢɡɦ ɪɟɚɥɢɡɚɰɢɢ COM ɭɞɚɥɢɬ COM-ɫɟɪɜɟɪ ɢɡ ɩɚɦɹɬɢ.
2.3. Ɋɚɡɪɚɛɨɬɤɚ ɤɨɦɩɨɧɟɧɬɨɜ Ʌɸɛɨɣ COM-ɫɟɪɜɟɪ (ɜɫɬɪɨɟɧɧɵɣ ɢɥɢ ɜɧɟɲɧɢɣ) ɧɟ ɢɦɟɟɬ ɤɚɤɨɝɨ-ɥɢɛɨ ɫɦɵɫɥɚ, ɟɫɥɢ ɜ ɧɟɦ ɧɟ ɯɪɚɧɢɬɫɹ ɯɨɬɹ ɛɵ ɨɞɢɧ ɤɨɦɩɨɧɟɧɬ. ɑɬɨɛɵ ɜ ɪɚɡɪɚɛɚɬɵɜɚɟɦɨɦ COM-ɫɟɪɜɟɪɟ ɫɨɡɞɚɬɶ ɧɨɜɵɣ ɤɨɦɩɨɧɟɧɬ, ɧɚɞɨ ɜɵɛɪɚɬɶ ɜ ɝɥɚɜɧɨɦ ɦɟɧɸ Delphi ɩɭɧɤɬ File/New. Ɂɚɬɟɦ ɜ ɩɨɹɜɢɜɲɟɦɫɹ ɞɢɚɥɨɝɨɜɨɦ ɨɤɧɟ New Items (ɪɢɫ. 2.1) ɧɚ ɡɚɤɥɚɞɤɟ ActiveX ɜɵɛɪɚɬɶ ɷɥɟɦɟɧɬ COM Object. ɉɨɫɥɟ ɱɟɝɨ ɨɬɤɪɨɟɬɫɹ ɨɤɧɨ ɦɚɫɬɟɪɚ COM Object Wizard (ɪɢɫ. 2.2).
Internal (ɜɧɭɬɪɟɧɧɢɣ) – ɢɫɩɨɥɶɡɭɟɬɫɹ ɬɨɥɶɤɨ ɞɥɹ ɤɨɦɩɨɧɟɧɬɨɜ, ɤɨɬɨɪɵɟ ɧɟ ɩɪɟɞɩɨɥɚɝɚɟɬɫɹ ɫɞɟɥɚɬɶ ɞɨɫɬɭɩɧɵɦɢ ɞɥɹ ɤɥɢɟɧɬɫɤɢɯ ɩɪɢɥɨɠɟɧɢɣ. Ɂɚɩɪɚɲɢɜɚɬɶ ɫɨɡɞɚɧɢɟ ɷɤɡɟɦɩɥɹɪɚ ɬɚɤɨɝɨ ɤɨɦɩɨɧɟɧɬɚ ɪɚɡɪɟɲɟɧɨ ɬɨɥɶɤɨ ɫɨɛɫɬɜɟɧɧɨɦɭ COM-ɫɟɪɜɟɪɭ; Single Instance (ɟɞɢɧɫɬɜɟɧɧɵɣ ɷɤɡɟɦɩɥɹɪ) – ɞɥɹ ɤɚɠɞɨɝɨ ɤɥɢɟɧɬɫɤɨɝɨ ɩɪɢɥɨɠɟɧɢɹ ɞɨɩɭɫɤɚɟɬɫɹ ɫɨɡɞɚɧɢɟ ɬɨɥɶɤɨ ɨɞɧɨɝɨ ɷɤɡɟɦɩɥɹɪɚ ɤɨɦɩɨɧɟɧɬɚ. ɉɪɢɥɨɠɟɧɢɟ, ɡɚɩɪɚɲɢɜɚɸɳɟɟ ɤɨɦɩɨɧɟɧɬ, ɛɭɞɟɬ ɪɚɛɨɬɚɬɶ ɫ ɫɨɛɫɬɜɟɧɧɨɣ ɨɬɞɟɥɶɧɨɣ ɤɨɩɢɟɣ COM-ɫɟɪɜɟɪɚ; Multiple Instance (ɦɧɨɠɟɫɬɜɨ ɷɤɡɟɦɩɥɹɪɨɜ) – COM-ɫɟɪɜɟɪ ɫɩɨɫɨɛɟɧ ɫɨɡɞɚɜɚɬɶ ɦɧɨɠɟɫɬɜɨ ɤɨɩɢɣ ɤɨɦɩɨɧɟɧɬɚ. ɉɪɢ ɡɚɩɪɨɫɟ ɧɨɜɨɝɨ ɷɤɡɟɦɩɥɹɪɚ ɤɨɦɩɨɧɟɧɬɚ ɧɨɜɵɣ ɫɟɪɜɟɪ ɧɟ ɡɚɩɭɫɤɚɟɬɫɹ (ɟɫɥɢ ɨɧ ɭɠɟ ɡɚɩɭɳɟɧ ɪɚɧɟɟ), ɚ ɫɨɡɞɚɟɬɫɹ ɧɨɜɵɣ ɷɤɡɟɦɩɥɹɪ COM-ɨɛɴɟɤɬɚ. Threading model – ɨɩɪɟɞɟɥɹɟɬ ɬɢɩ ɩɨɞɞɟɪɠɢɜɚɟɦɨɣ ɦɨɞɟɥɢ ɩɨɬɨɤɨɜ ɡɚɞɚɱ. Ⱦɚɧɧɵɣ ɩɚɪɚɦɟɬɪ ɚɤɬɭɚɥɟɧ ɬɨɥɶɤɨ ɤ ɜɫɬɪɨɟɧɧɵɦ COMɫɟɪɜɟɪɚɦ ɢ ɦɨɠɟɬ ɩɪɢɧɢɦɚɬɶ ɫɥɟɞɭɸɳɢɟ ɡɧɚɱɟɧɢɹ: Single (ɨɞɧɨɩɨɬɨɤɨɜɚɹ) – ɜɫɟ ɡɚɹɜɤɢ ɧɚ ɞɨɫɬɭɩ ɤ ɤɨɦɩɨɧɟɧɬɭ ɜɵɫɬɪɚɢɜɚɸɬɫɹ ɨɩɟɪɚɰɢɨɧɧɨɣ ɫɢɫɬɟɦɨɣ Windows ɜ ɨɱɟɪɟɞɶ, ɬɚɤ ɱɬɨ ɧɟɬ ɧɟɨɛɯɨɞɢɦɨɫɬɢ ɛɟɫɩɨɤɨɢɬɶɫɹ ɨ ɬɨɦ, ɱɬɨ ɧɟɫɤɨɥɶɤɨ ɩɨɬɨɤɨɜ ɦɨɝɭɬ ɨɞɧɨɜɪɟɦɟɧɧɨ ɢɦɟɬɶ ɞɨɫɬɭɩ ɤ ɫɟɪɜɟɪɭ; Apartment (ɫɟɤɰɢɨɧɢɪɨɜɚɧɧɚɹ) – ɤɨɦɩɨɧɟɧɬɵ ɨɛɪɚɛɚɬɵɜɚɸɬ ɡɚɩɪɨɫɵ ɬɨɥɶɤɨ ɢɡ ɬɨɝɨ ɠɟ ɩɨɬɨɤɚ, ɤɨɬɨɪɵɣ ɢɯ ɩɨɪɨɞɢɥ. Ɉɞɢɧ ɫɟɪɜɟɪ ɦɨɠɟɬ ɷɤɫɩɨɪɬɢɪɨɜɚɬɶ ɦɧɨɠɟɫɬɜɨ COM-ɨɛɴɟɤɬɨɜ, ɩɪɢɱɟɦ ɤɚɠɞɵɣ ɢɡ ɧɢɯ ɦɨɠɟɬ ɛɵɬɶ ɫɨɡɞɚɧ ɜ ɞɪɭɝɨɦ ɩɨɬɨɤɟ, ɩɨɷɬɨɦɭ ɬɪɟɛɭɟɬɫɹ ɫɢɧɯɪɨɧɢɡɚɰɢɹ ɞɨɫɬɭɩɚ ɤ ɥɸɛɵɦ ɝɥɨɛɚɥɶɧɵɦ ɞɚɧɧɵɦ, ɤɨɬɨɪɵɟ ɨɩɪɟɞɟɥɟɧɵ ɜ ɫɟɪɜɟɪɟ ɩɨɫɪɟɞɫɬɜɨɦ ɦɶɸɬɟɤɫɨɜ, ɫɨɛɵɬɢɣ, ɤɪɢɬɢɱɟɫɤɢɯ ɫɟɤɰɢɣ ɢ ɬ. ɩ. Free (ɫɜɨɛɨɞɧɚɹ) – ɦɧɨɠɟɫɬɜɨ ɩɨɬɨɤɨɜ ɦɨɠɟɬ ɛɵɬɶ ɚɤɬɢɜɧɨ ɜ ɥɸɛɨɦ ɞɚɧɧɨɦ ɤɨɦɩɨɧɟɧɬɟ ɜ ɨɞɧɨ ɢ ɬɨ ɠɟ ɜɪɟɦɹ. ȼɫɥɟɞɫɬɜɢɟ ɷɬɨɝɨ ɧɭɠɧɨ ɩɨɡɚɛɨɬɢɬɶɫɹ ɨ ɫɢɧɯɪɨɧɢɡɚɰɢɢ ɞɨɫɬɭɩɚ ɧɟ ɬɨɥɶɤɨ ɤ ɝɥɨɛɚɥɶɧɵɦ ɞɚɧɧɵɦ, ɧɨ ɢ ɤ ɥɨɤɚɥɶɧɵɦ; Both (ɢ ɫɟɤɰɢɨɧɧɚɹ ɢ ɫɜɨɛɨɞɧɚɹ) – ɤɨɦɩɨɧɟɧɬ ɞɨɥɠɟɧ ɫɢɧɯɪɨɧɢɡɢɪɨɜɚɬɶ ɫɜɨɣ ɫɨɛɫɬɜɟɧɧɵɣ ɞɨɫɬɭɩ ɤ ɷɤɡɟɦɩɥɹɪɚɦ ɤɨɦɩɨɧɟɧɬɨɜ, ɧɚɫɬɪɨɟɧɧɵɯ ɧɚ ɫɟɤɰɢɨɧɢɪɨɜɚɧɧɭɸ ɦɨɞɟɥɶ, ɢ ɜɵɩɨɥɧɹɬɶ ɦɚɪɲɪɭɬɢɡɚɰɢɸ ɩɚɪɚɦɟɬɪɨɜ ɦɟɠɞɭ ɩɨɬɨɤɚɦɢ;
ȼ ɩɨɥɟ Class Name ɜɜɨɞɢɬɫɹ ɢɦɹ ɤɥɚɫɫɚ. ɉɪɟɮɢɤɫ «T» ɤ ɧɚɡɜɚɧɢɸ ɨɛɴɟɤɬɚ ɞɨɛɚɜɢɬɫɹ ɚɜɬɨɦɚɬɢɱɟɫɤɢ. ɇɚɡɜɚɧɢɟ ɨɛɴɟɤɬɚ ɧɟ ɞɨɥɠɧɨ ɛɵɬɶ ɫɥɭɠɟɛɧɵɦ ɫɥɨɜɨɦ ɢ ɫɨɜɩɚɞɚɬɶ ɫ ɢɦɟɧɟɦ COM-ɫɟɪɜɟɪɚ ɢ ɩɪɢɫɨɟɞɢɧɟɧɧɨɝɨ ɦɨɞɭɥɹ. ɉɨɥɟ Instancing ɨɩɪɟɞɟɥɹɟɬ ɦɟɬɨɞ ɫɨɡɞɚɧɢɹ ɷɤɡɟɦɩɥɹɪɚ ɤɨɦɩɨɧɟɧɬɚ ɢ ɚɤɬɭɚɥɶɧɨ ɬɨɥɶɤɨ ɞɥɹ ɜɧɟɲɧɢɯ COM-ɫɟɪɜɟɪɨɜ:
ȼ ɩɨɥɟ Implemented interfaces ɱɟɪɟɡ ɡɚɩɹɬɭɸ ɜɜɨɞɹɬɫɹ ɧɚɡɜɚɧɢɹ ɢɧɬɟɪɮɟɣɫɨɜ, ɤɨɬɨɪɵɟ ɛɭɞɟɬ ɩɨɞɞɟɪɠɢɜɚɬɶ ɤɨɦɩɨɧɟɧɬ. ɉɨɥɟ Description ɩɪɟɞɧɚɡɧɚɱɟɧɨ ɞɥɹ ɜɜɨɞɚ ɡɚɦɟɱɚɧɢɣ ɢɥɢ ɫɬɪɨɤɢɨɩɢɫɚɧɢɹ ɨɛɴɟɤɬɚ. Ʉɨɝɞɚ ɜɵɛɢɪɚɟɬɫɹ ɨɩɰɢɹ Include Type Library, ɤ ɨɩɢɫɚɧɢɸ Ʉɨɦɩɨɧɟɧɬɚ ɩɨɞɤɥɸɱɚɟɬɫɹ ɛɢɛɥɢɨɬɟɤɚ ɬɢɩɨɜ. Ȼɢɛɥɢɨɬɟɤɢ ɬɢɩɨɜ ɪɚɡɨɛɪɚɧɵ ɜ ɬɪɟɬɶɟɣ ɝɥɚɜɟ. Ʉɨɦɩɨɧɟɧɬɵ ɜ Delphi ɨɩɢɫɵɜɚɸɬɫɹ ɬɚɤ ɠɟ, ɤɚɤ ɢ ɤɥɚɫɫɵ ɨɛɴɟɤɬɨɜ:
17
18
Ɋɢɫ. 2.2. Ɇɚɫɬɟɪ COM Object Wizard
<ɢɦɹ ɤɨɦɩɨɧɟɧɬɚ>=class(<ɢɦɹ ɨɛɴɟɤɬɚ-ɪɨɞɢɬɟɥɹ> {,<ɢɧɬɟɪɮɟɣɫ}) private <……………………………> protected <ɦɟɬɨɞɵ ɢɧɬɟɪɮɟɣɫɨɜ>
IInterface2=interface procedure DoIt; stdcall; end; TMyObject=class(TComObject, IInterface1, IInteface2) procedure IInterface1.DoIt=DoIt1; procedure IInterface2.DoIt=DoIt2; procedure DoIt1; stdcall; procedure DoIt2; stdcall;
<……………………………> public
end; <……………………………>
end; Ⱦɥɹ ɛɵɫɬɪɨɝɨ ɩɨɫɬɪɨɟɧɢɹ COM-ɩɪɢɥɨɠɟɧɢɣ ɪɚɡɪɚɛɨɬɱɢɤɢ Delphi ɩɨɦɟɫɬɢɥɢ ɜ ɦɨɞɭɥɶ ComObj ɤɥɚɫɫɵ, ɜ ɤɨɬɨɪɵɯ ɪɟɚɥɢɡɨɜɚɧɵ ɜɫɟ ɫɬɚɧɞɚɪɬɧɵɟ ɬɪɟɛɨɜɚɧɢɹ ɞɥɹ COM. Ɍɚɤ, ɧɚɩɪɢɦɟɪ, ɟɫɬɶ ɤɥɚɫɫ TComObject, ɜ ɤɨɬɨɪɨɦ ɪɟɚɥɢɡɨɜɚɧɵ ɜɫɟ ɦɟɬɨɞɵ ɢɧɬɟɪɮɟɣɫɚ IUnknown. ɉɪɢɦɟɱɚɧɢɟ 2.4. ɉɪɢ ɪɟɚɥɢɡɚɰɢɢ COM-ɩɪɢɥɨɠɟɧɢɣ ɜɫɟɝɞɚ ɩɨɥɶɡɭɣɬɟɫɶ ɲɚɛɥɨɧɚɦɢ, ɩɪɟɞɥɨɠɟɧɧɵɦɢ ɜ ɡɚɤɥɚɞɤɟ ActiveX ɞɢɚɥɨɝɨɜɨɝɨ ɨɤɧɚ New Items (ɪɢɫ. 2.1). Ⱦɥɹ ɤɚɠɞɨɝɨ ɬɢɩɚ ɡɚɞɚɱ, ɨɫɧɨɜɚɧɧɵɯ ɧɚ COM, ɜ Delphi ɪɟɚɥɢɡɨɜɚɧɵ ɫɜɨɢ ɤɥɚɫɫɵ, ɩɪɢɦɟɧɟɧɢɟ ɤɨɬɨɪɵɯ ɫɭɳɟɫɬɜɟɧɧɨ ɨɛɥɟɝɱɚɟɬ ɪɚɛɨɬɭ ɢ ɝɚɪɚɧɬɢɪɭɟɬ ɤɨɪɪɟɤɬɧɨɫɬɶ ɪɚɛɨɬɵ ɜɚɲɟɝɨ ɩɪɨɟɤɬɚ. ɂɧɬɟɪɮɟɣɫɵ, ɩɨɞɞɟɪɠɢɜɚɟɦɵɟ ɤɨɦɩɨɧɟɧɬɨɦ, ɨɩɢɫɵɜɚɸɬɫɹ ɱɟɪɟɡ ɡɚɩɹɬɭɸ ɜ ɪɚɡɞɟɥɟ ɨɩɢɫɚɧɢɹ ɤɥɚɫɫɚ-ɩɪɟɞɤɚ, ɚ ɦɟɬɨɞɵ ɢɧɬɟɪɮɟɣɫɚ ɨɛɵɱɧɨ ɨɩɢɫɵɜɚɸɬɫɹ ɜ ɪɚɡɞɟɥɟ ɨɩɢɫɚɧɢɹ protected. ɉɪɢɦɟɱɚɧɢɟ 2.5. ȿɫɥɢ ɧɭɠɧɨ ɢɧɢɰɢɚɥɢɡɢɪɨɜɚɬɶ ɤɚɤɢɟ-ɥɢɛɨ ɞɚɧɧɵɟ ɜɨ ɜɪɟɦɹ ɫɨɡɞɚɧɢɹ ɤɨɦɩɨɧɟɧɬɚ, ɬɨ ɞɥɹ ɷɬɢɯ ɰɟɥɟɣ ɢɫɩɨɥɶɡɭɣɬɟ ɦɟɬɨɞ Initialize, ɤɨɬɨɪɵɣ ɜɵɡɵɜɚɟɬɫɹ ɩɨɫɥɟ ɪɚɛɨɬɵ ɤɨɧɫɬɪɭɤɬɨɪɚ.
2.4. Ɋɚɡɪɟɲɟɧɢɟ ɧɟɨɞɧɨɡɧɚɱɧɨɫɬɢ ɦɟɬɨɞɨɜ Ʉɚɤ ɭɠɟ ɛɵɥɨ ɧɟɨɞɧɨɤɪɚɬɧɨ ɫɤɚɡɚɧɨ, ɤɨɦɩɨɧɟɧɬ ɦɨɠɟɬ ɩɨɞɞɟɪɠɢɜɚɬɶ ɧɟɫɤɨɥɶɤɨ ɢɧɬɟɪɮɟɣɫɨɜ. ȿɫɥɢ ɦɟɬɨɞɵ ɪɚɡɧɵɯ ɢɧɬɟɪɮɟɣɫɨɜ ɫ ɨɞɢɧɚɤɨɜɵɦɢ ɢɦɟɧɚɦɢ ɜɵɩɨɥɧɹɸɬ ɨɞɢɧɚɤɨɜɵɟ ɮɭɧɤɰɢɢ, ɩɪɨɛɥɟɦ ɧɟ ɜɨɡɧɢɤɚɟɬ, ɚ ɤɨɝɞɚ ɬɪɟɛɭɟɬɫɹ, ɱɬɨɛɵ ɨɧɢ ɜɵɩɨɥɧɹɥɢ ɪɚɡɧɵɟ ɮɭɧɤɰɢɢ, ɢɫɩɨɥɶɡɭɟɬɫɹ ɫɩɟɰɢɚɥɶɧɚɹ ɮɨɪɦɚ ɜɵɪɚɠɟɧɢɹ ɞɥɹ ɪɚɡɪɟɲɟɧɢɹ ɦɟɬɨɞɨɜ, ɩɪɢɦɟɪ ɪɟɚɥɢɡɚɰɢɢ ɤɨɬɨɪɵɯ ɩɪɢɜɟɞɟɧ ɜ ɫɥɟɞɭɸɳɟɦ ɩɪɢɦɟɪɟ: IInterface1=interface procedure DoIt; stdcall; end;
Ʉɥɚɫɫ TMyObject ɨɬɨɛɪɚɠɚɟɬ ɦɟɬɨɞ IInterface1.DoIt ɧɚ DoIt1, ɚ ɦɟɬɨɞ IInterface2.DoIt – ɧɚ DoIt2.
2.5. Ⱦɟɥɟɝɢɪɨɜɚɧɢɟ ɢɧɬɟɪɮɟɣɫɚ ɉɭɫɬɶ ɢɦɟɟɬɫɹ ɯɨɪɨɲɨ ɩɪɨɞɭɦɚɧɧɵɣ ɤɥɚɫɫ TObject1, ɜ ɤɨɬɨɪɨɦ ɪɟɚɥɢɡɨɜɚɧ ɢɧɬɟɪɮɟɣɫ IInterfase1. Ɍɪɟɛɭɟɬɫɹ ɫɨɡɞɚɬɶ ɤɥɚɫɫ TCombinedObject, ɜ ɤɨɬɨɪɨɦ ɛɭɞɭɬ ɪɟɚɥɢɡɨɜɚɧɵ ɞɜɚ ɢɧɬɟɪɮɟɣɫɚ IInterfase1 ɢ IInterfase2. Ⱦɥɹ ɷɬɢɯ ɰɟɥɟɣ ɜ ɫɪɟɞɟ Delphi ɪɚɡɪɚɛɨɬɚɧ ɦɟɬɨɞ ɞɟɥɟɝɢɪɨɜɚɧɢɹ ɢɧɬɟɪɮɟɣɫɚ, ɨɫɧɨɜɚɧɧɵɣ ɧɚ ɬɨɦ, ɱɬɨ ɤɥɚɫɫ-ɩɪɟɟɦɧɢɤ ɫɨɞɟɪɠɢɬ ɭɤɚɡɚɬɟɥɶ ɧɚ ɞɪɭɝɨɣ ɤɥɚɫɫ, ɜ ɤɨɬɨɪɨɦ ɭɠɟ ɪɟɚɥɢɡɨɜɚɧɨ ɩɨɜɟɞɟɧɢɟ ɨɞɧɨɝɨ ɢɥɢ ɛɨɥɟɟ ɢɧɬɟɪɮɟɣɫɨɜ. Ʉɚɤ ɦɨɠɧɨ ɨɫɭɳɟɫɬɜɢɬɶ ɞɟɥɟɝɢɪɨɜɚɧɢɟ, ɩɨɤɚɡɚɧɨ ɜ ɫɥɟɞɭɸɳɟɦ ɩɪɢɦɟɪɟ: unit Unit1; uses ComObj; type IInterface1=interface procedure DoIt1; end; IInterface2=interface procedure DoIt2; end; TObject1=class(TComObject, IInterface1) protected procedure DoIt1; end; TCombinedObject=class(TComObject, IInterface1, IInterface2) private
19
20
Fobj1: IInterface1; protected property Intf: IInterface1 read FObj1 implements IInterface1;
Ɉɛɴɟɤɬ TObject1 ɞɚɧɧɨɝɨ ɩɪɢɦɟɪɚ ɩɪɢɧɹɬɨ ɧɚɡɵɜɚɬɶ ɚɝɪɟɝɢɪɨɜɚɧɧɵɦ (ɨɛɴɟɞɢɧɹɟɦɵɦ), ɚ ɨɛɴɟɤɬ, ɤɨɬɨɪɵɣ ɯɪɚɧɢɬ ɫɫɵɥɤɭ ɧɚ ɚɝɪɟɝɢɪɭɟɦɵɣ ɨɛɴɟɤɬ (TCombinedObject), ɧɚɡɵɜɚɸɬ ɤɨɧɬɟɣɧɟɪɨɦ. ɉɪɢɦɟɱɚɧɢɟ 2.7. Ɉɛɪɚɬɢɬɟ ɜɧɢɦɚɧɢɟ, ɱɬɨ ɢɡ ɢɧɬɟɪɮɟɣɫɚ ɚɝɪɟɝɢɪɨɜɚɧɧɨɝɨ ɨɛɴɟɤɬɚ ɧɟɥɶɡɹ ɩɨɥɭɱɢɬɶ ɫɫɵɥɤɭ ɧɚ ɤɨɧɬɟɣɧɟɪ. ɂɧɵɦɢ ɫɥɨɜɚɦɢ, ɜɵɪɚɠɟɧɢɟ I2 as Interface1 ɜ ɧɚɲɟɦ ɩɪɢɦɟɪɟ ɫɝɟɧɟɪɢɪɭɟɬ ɨɲɢɛɤɭ.
procedure DoIt2; constructor Create; destructor Destroy;override; end;
2.6. Ƚɟɧɟɪɚɬɨɪ ɤɨɦɩɨɧɟɧɬɨɜ
implemetation
Ⱦɥɹ ɩɨɪɨɠɞɟɧɢɹ ɧɨɜɨɝɨ ɷɤɡɟɦɩɥɹɪɚ ɤɨɦɩɨɧɟɧɬɚ, ɩɪɢ ɡɚɩɪɨɫɟ ɩɪɢɥɨɠɟɧɢɟɦ-ɤɥɢɟɧɬɨɦ, ɢɫɩɨɥɶɡɭɟɬɫɹ ɫɬɚɧɞɚɪɬɧɵɣ ɦɟɯɚɧɢɡɦ COM, ɧɚɡɜɚɧɧɵɣ ɝɟɧɟɪɚɬɨɪɨɦ ɤɨɦɩɨɧɟɧɬɨɜ, ɢɥɢ ɮɚɛɪɢɤɨɣ ɤɥɚɫɫɚ. Ʌɸɛɨɣ ɤɨɦɩɨɧɟɧɬ ɢɦɟɟɬ ɚɫɫɨɰɢɢɪɨɜɚɧɧɵɣ ɫ ɧɢɦ ɝɟɧɟɪɚɬɨɪ ɤɨɦɩɨɧɟɧɬɨɜ, ɤɨɬɨɪɵɣ ɩɨɞɞɟɪɠɢɜɚɟɬ ɫɬɚɧɞɚɪɬɧɵɣ ɢɧɬɟɪɮɟɣɫ IClassFactory:
{TObject1} procedure TObject1.DoIt1; begin <…………………> end; constructor TCombinedObject.Create; begin inherited; FObj1:=TObject1.Create; end; destructor TCombinedObject.Destroy; begin FObj1:=nil; inherited; end; procedure TCombinedObject.DoIt2; begin <…………………> end; ɉɭɫɬɶ ɜ ɩɪɨɝɪɚɦɦɟ, ɢɫɩɨɥɶɡɭɸɳɟɣ ɤɨɦɩɨɧɟɧɬ TCombinedObject, ɟɫɬɶ ɩɟɪɟɦɟɧɧɚɹ I2 – ɫɫɵɥɤɚ ɧɚ ɢɧɬɟɪɮɟɣɫ IInterface2. ɑɬɨɛɵ ɩɨɥɭɱɢɬɶ ɫɫɵɥɤɭ ɧɚ IInterface1, ɢɫɩɨɥɶɡɭɟɬɫɹ ɨɩɟɪɚɬɨɪ as: I1:=I2 as IInterface1; ɉɪɢɦɟɱɚɧɢɟ 2.6. Ʉɚɤ ɛɵɥɨ ɫɤɚɡɚɧɨ ɜ ɩ. 1.5, ɞɥɹ ɩɨɥɭɱɟɧɢɹ ɫɫɵɥɤɢ ɧɚ ɞɪɭɝɨɣ ɩɨɞɞɟɪɠɢɜɚɟɦɵɣ ɢɧɬɟɪɮɟɣɫ ɧɭɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɮɭɧɤɰɢɸ QueryInterface, ɧɨ ɜ Delphi ɞɚɧɧɚɹ ɨɩɟɪɚɰɢɹ ɪɟɚɥɢɡɨɜɚɧɚ ɫ ɩɨɦɨɳɶɸ ɨɩɟɪɚɬɨɪɚ as. 21
IClassFactory = interface(IUnknown) ['{00000001-0000-0000-C000-000000000046}'] function CreateInstance(const unkOuter: IUnknown; const iid: TIID; out obj): HResult; stdcall; function LockServer(fLock: BOOL): HResult; stdcall; end; Ɏɭɧɤɰɢɹ ɋreateInstance ɨɬɜɟɬɫɬɜɟɧɧɚ ɡɚ ɫɨɡɞɚɧɢɟ ɷɤɡɟɦɩɥɹɪɚ ɤɨɦɩɨɧɟɧɬɚ, ɧɚ ɤɨɬɨɪɵɣ ɫɫɵɥɚɟɬɫɹ ɝɟɧɟɪɚɬɨɪ ɤɨɦɩɨɧɟɧɬɨɜ. Ʉɚɤ ɩɪɚɜɢɥɨ, ɧɟɬ ɧɟɨɛɯɨɞɢɦɨɫɬɢ ɜɵɡɵɜɚɬɶ ɞɚɧɧɭɸ ɮɭɧɤɰɢɸ ɫɚɦɨɫɬɨɹɬɟɥɶɧɨ, ɩɨɷɬɨɦɭ ɩɨɞɪɨɛɧɨɟ ɨɩɢɫɚɧɢɟ ɩɚɪɚɦɟɬɪɨɜ ɜ ɞɚɧɧɨɦ ɦɟɬɨɞɢɱɟɫɤɨɦ ɩɨɫɨɛɢɢ ɪɚɫɫɦɚɬɪɢɜɚɬɶɫɹ ɧɟ ɛɭɞɟɬ. LockServer ɢɫɩɨɥɶɡɭɟɬɫɹ ɫɢɫɬɟɦɨɣ ɞɥɹ ɩɨɞɫɱɟɬɚ ɫɫɵɥɨɤ ɨɛɪɚɳɟɧɢɣ ɤ COM-ɫɟɪɜɟɪɭ. Ʉɨɝɞɚ ɩɪɢɥɨɠɟɧɢɟ ɡɚɩɪɚɲɢɜɚɟɬ ɤɨɦɩɨɧɟɧɬ, ɜ ɞɚɧɧɭɸ ɮɭɧɤɰɢɸ ɩɟɪɟɞɚɟɬɫɹ ɩɚɪɚɦɟɬɪ true ɢ ɫɱɟɬɱɢɤ ɭɜɟɥɢɱɢɜɚɟɬɫɹ ɧɚ ɟɞɢɧɢɰɭ. Ʉɨɝɞɚ COM-ɫɟɪɜɟɪ ɧɟ ɧɭɠɟɧ ɩɪɢɥɨɠɟɧɢɸ, ɜ LockServer ɩɟɪɟɞɚɟɬɫɹ ɩɚɪɚɦɟɬɪ false ɢ ɫɱɟɬɱɢɤ ɧɚ ɟɞɢɧɢɰɭ ɭɦɟɧɶɲɚɟɬɫɹ. ȼ ɫɥɭɱɚɟ ɨɛɧɭɥɟɧɢɹ ɫɱɟɬɱɢɤɚ COM-ɫɟɪɜɟɪ ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɜɵɝɪɭɠɚɟɬɫɹ. ɉɪɢɦɟɱɚɧɢɟ 2.8. Microsoft ɨɛɴɹɜɢɥɚ ɟɳɟ ɨɞɢɧ ɢɧɬɟɪɮɟɣɫ IClassFactory2, ɤɨɬɨɪɵɣ ɞɨɛɚɜɥɹɟɬ ɤ IClassFactory ɩɨɞɞɟɪɠɤɭ ɥɢɰɟɧɡɢɪɨɜɚɧɢɹ ɢɥɢ ɪɚɡɪɟɲɟɧɢɹ ɧɚ ɫɨɡɞɚɧɢɟ. Ʉɥɢɟɧɬ ɨɛɹɡɚɧ ɩɟɪɟɞɚɬɶ ɝɟɧɟɪɚɬɨɪɭ ɤɨɦɩɨɧɟɧɬɨɜ ɩɪɢ ɩɨɦɨɳɢ IClassFactory2 ɤɨɪɪɟɤɬɧɵɣ ɤɥɸɱ ɢɥɢ ɥɢɰɟɧɡɢɸ, ɩɪɟɠɞɟ ɱɟɦ ɷɬɨɬ ɝɟɧɟɪɚɬɨɪ ɛɭɞɟɬ ɫɨɡɞɚɜɚɬɶ ɤɨɦɩɨɧɟɧɬɵ. [4] Ʉɨɝɞɚ ɤɥɢɟɧɬ ɩɟɪɟɞɚɟɬ ɫɢɫɬɟɦɟ COM ɡɚɩɪɨɫ ɧɚ ɧɨɜɵɣ ɷɤɡɟɦɩɥɹɪ, ɫɧɚɱɚɥɚ ɢɳɟɬɫɹ ɢ ɡɚɝɪɭɠɚɟɬɫɹ (ɟɫɥɢ ɨɧ ɧɟ ɡɚɝɪɭɠɟɧ) COM-ɫɟɪɜɟɪ, ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɣ ɡɚɩɪɚɲɢɜɚɟɦɨɦɭ ɤɨɦɩɨɧɟɧɬɭ. Ɂɚɬɟɦ ɫɢɫɬɟɦɚ COM ɩɨɞɤɥɸɱɚɟɬɫɹ ɤ ɮɚɛɪɢɤɟ ɤɨɦɩɨɧɟɧɬɨɜ ɱɟɪɟɡ ɢɧɬɟɪɮɟɣɫ COM, ɜɵɡɵɜɚɟɬ ɫɨɨɬɜɟɬɫɬɜɭɸɳɭɸ ɮɭɧɤɰɢɸ CreateInstance ɢ ɩɟɪɟɞɚɟɬ ɤɥɢɟɧɬɭ ɫɨɨɬɜɟɬɫɬɜɭɸɳɭɸ ɫɫɵɥɤɭ. 22
ɇɚ ɩɟɪɜɵɣ ɜɡɝɥɹɞ, ɦɨɠɟɬ ɜɨɡɧɢɤɧɭɬɶ ɜɩɟɱɚɬɥɟɧɢɟ, ɱɬɨ ɫɭɳɟɫɬɜɭɟɬ ɢɡɥɢɲɧɹɹ ɞɭɚɥɶɧɨɫɬɶ ɧɚ ɧɚɥɢɱɢɟ ɤɥɚɫɫɚ ɤɨɦɩɨɧɟɧɬɚ ɢ ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɣ ɟɦɭ ɮɚɛɪɢɤɢ ɤɥɚɫɫɚ, ɬɚɤ ɤɚɤ ɨɞɧɚ ɮɚɛɪɢɤɚ ɤɥɚɫɫɨɜ ɦɨɠɟɬ ɜɡɹɬɶ ɧɚ ɫɟɛɹ ɜɫɟ ɡɚɛɨɬɵ ɨ ɫɨɡɞɚɧɢɢ ɦɧɨɠɟɫɬɜɚ ɤɨɦɩɨɧɟɧɬɨɜ. ɇɨ ɧɚ ɫɚɦɨɦ ɞɟɥɟ ɢɦɟɟɬɫɹ ɬɨɥɶɤɨ ɨɞɢɧ ɷɤɡɟɦɩɥɹɪ ɝɟɧɟɪɚɬɨɪɚ ɤɨɦɩɨɧɟɧɬɨɜ (ComServer), ɤɨɬɨɪɵɣ ɧɚɯɨɞɢɬɫɹ ɜ ɩɨɥɟ ɡɪɟɧɢɹ COM ɢ ɫɨɞɟɪɠɢɬ ɫɩɢɫɨɤ ɜɫɟɯ ɜɧɭɬɪɟɧɧɢɯ ɨɛɴɟɤɬɨɜ ɮɚɛɪɢɤ ɤɥɚɫɫɨɜ. Ƚɟɧɟɪɚɬɨɪ ɤɨɦɩɨɧɟɧɬɨɜ ɢɡɨɥɢɪɭɟɬ ɦɟɯɚɧɢɡɦ ɪɟɚɥɢɡɚɰɢɢ COM ɨɬ ɫɚɦɨɝɨ ɩɪɨɰɟɫɫɚ ɮɨɪɦɢɪɨɜɚɧɢɹ ɨɛɴɟɤɬɚ. ȿɫɥɢ ɛɵ ɬɚɤɨɝɨ ɝɟɧɟɪɚɬɨɪɚ ɧɟ ɛɵɥɨ, ɬɨ ɩɪɢɲɥɨɫɶ ɛɵ ɜɵɩɨɥɧɹɬɶ ɧɟɩɨɫɪɟɞɫɬɜɟɧɧɵɣ ɜɵɡɨɜ ɤɨɧɫɬɪɭɤɬɨɪɚ ɤɥɚɫɫɚ, ɱɬɨɛɵ ɫɨɡɞɚɬɶ ɨɛɴɟɤɬ. Ɉɫɧɨɜɧɚɹ ɢɞɟɹ ɬɟɯɧɨɥɨɝɢɢ ɫɨɫɬɨɢɬ ɜ ɬɨɦ, ɱɬɨɛɵ ɧɟ ɧɚɤɥɚɞɵɜɚɬɶ ɨɝɪɚɧɢɱɟɧɢɣ ɧɚ ɬɨ, ɤɚɤ ɪɟɚɥɢɡɨɜɚɧ ɨɛɴɟɤɬ, ɚ ɜɵɩɨɥɧɹɟɦɨɟ ɤɨɧɫɬɪɭɤɬɨɪɨɦ ɫɨɡɞɚɧɢɟ ɟɫɬɶ ɧɟ ɱɬɨ ɢɧɨɟ, ɤɚɤ ɨɞɢɧ ɢɡ ɤɨɦɩɨɧɟɧɬɨɜ ɩɪɨɰɟɫɫɚ ɪɟɚɥɢɡɚɰɢɢ. ɉɪɢɦɟɱɚɧɢɟ 2.9. ɋɥɟɞɭɟɬ ɡɚɦɟɬɢɬɶ, ɱɬɨ ɬɟɯɧɨɥɨɝɢɹ COM ɜɨɜɫɟ ɧɟ ɬɪɟɛɭɟɬ, ɱɬɨɛɵ ɤɨɦɩɨɧɟɧɬɵ ɛɵɥɢ ɪɟɚɥɢɡɨɜɚɧɵ ɫ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ ɈɈɉ, ɩɪɨɫɬɨ ɬɚɤɨɣ ɩɨɞɯɨɞ ɹɜɥɹɟɬɫɹ ɧɚɢɛɨɥɟɟ ɭɞɨɛɧɵɦ. ɉɨɦɢɦɨ ɮɭɧɤɰɢɣ ɩɨɪɨɠɞɟɧɢɹ ɤɨɦɩɨɧɟɧɬɨɜ, ɮɚɛɪɢɤɚ ɤɥɚɫɫɨɜ ɯɪɚɧɢɬ ɞɚɧɧɵɟ ɨ ɦɟɬɨɞɟ ɫɨɡɞɚɧɢɹ ɷɤɡɟɦɩɥɹɪɚ ɤɨɦɩɨɧɟɧɬɚ, ɬɢɩ ɦɨɞɟɥɢ ɩɨɬɨɤɨɜ ɡɚɞɚɱ ɢ ɞɪɭɝɭɸ ɧɟɨɛɯɨɞɢɦɭɸ ɢɧɮɨɪɦɚɰɢɸ, ɤɨɬɨɪɚɹ ɩɟɪɟɞɚɟɬɫɹ ɜ ɤɨɧɫɬɪɭɤɬɨɪ. ɇɢɠɟ ɩɪɢɜɟɞɟɧ ɤɨɧɫɬɪɭɤɬɨɪ ɤɥɚɫɫɚ TClassFactory, ɢɫɩɨɥɶɡɭɟɦɵɣ ɞɥɹ ɪɟɚɥɢɡɚɰɢɢ ɩɪɨɫɬɵɯ ɤɨɦɩɨɧɟɧɬɨɜ: TClassFactory.Create(ComServer {ɫɬɚɧɞɚɪɬɧɨɟ ɢɦɹ ɮɚɛɪɢɤɢ ɤɥɚɫɫɨɜ ɜ Delphi}, <ɢɦɹ ɤɥɚɫɫɚ>,
, <ɦɟɬɨɞ ɫɨɡɞɚɧɢɹ ɷɤɡɟɦɩɥɹɪɚ ɤɨɦɩɨɧɟɧɬɚ>, <ɬɢɩ ɦɨɞɟɥɢ ɩɨɬɨɤɨɜ ɡɚɞɚɱ>); Ɇɟɬɨɞ ɫɨɡɞɚɧɢɹ ɷɤɡɟɦɩɥɹɪɚ ɤɨɦɩɨɧɟɧɬɚ ɢ ɬɢɩ ɦɨɞɟɥɢ ɩɨɬɨɤɨɜ ɡɚɞɚɱ ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɭɫɬɚɧɚɜɥɢɜɚɸɬɫɹ ɧɚ ɨɫɧɨɜɟ ɞɚɧɧɵɯ, ɜɜɟɞɟɧɧɵɯ ɜ ɦɚɫɬɟɪɟ COM Object Wizard (ɪɢɫ. 2.2). Ⱦɥɹ ɩɪɨɫɬɨɬɵ ɨɛɪɚɳɟɧɢɹ ɤ ɮɚɛɪɢɤɟ ɤɥɚɫɫɚ ɢɫɩɨɥɶɡɭɸɬɫɹ Co-ɤɥɚɫɫɵ, ɤɨɬɨɪɵɟ ɩɨɞɞɟɪɠɢɜɚɸɬ ɬɪɢ ɮɭɧɤɰɢɢ: <ɂɦɹ Co-ɤɥɚɫɫɚ> = class class function Create: <ɢɧɬɟɪɮɟɣɫ ɤɨɦɩɨɧɟɧɬɚ>; class function CreateRemote(const MachineName: string): <ɢɧɬɟɪɮɟɣɫ ɤɨɦɩɨɧɟɧɬɚ>; end; Create – ɩɪɢɦɟɧɹɟɬɫɹ ɞɥɹ ɩɨɥɭɱɟɧɢɹ ɧɨɜɨɝɨ ɷɤɡɟɦɩɥɹɪɚ ɤɨɦɩɨɧɟɧɬɚ. CreateRemote – ɫɨɡɞɚɟɬ ɷɤɡɟɦɩɥɹɪ ɤɨɦɩɨɧɟɧɬɚ ɧɚ ɭɞɚɥɟɧɧɨɣ ɦɚɲɢɧɟ (ɩɪɢɦɟɧɹɟɬɫɹ ɜ DCOM).
23
Ɉɛɵɱɧɨ, ɟɫɥɢ ɩɥɚɧɢɪɭɟɬɫɹ, ɱɬɨ ɤɨɦɩɨɧɟɧɬ ɛɭɞɟɬ ɡɚɩɪɚɲɢɜɚɬɶɫɹ ɤɥɢɟɧɬɨɦ, ɬɨ ɞɥɹ ɧɟɝɨ ɫɨɡɞɚɟɬɫɹ Co-ɤɥɚɫɫ, ɱɬɨɛɵ ɧɟ ɡɚɭɱɢɜɚɬɶ ɞɨɫɬɚɬɨɱɧɨ ɞɥɢɧɧɵɣ ɭɧɢɤɚɥɶɧɵɣ ɧɨɦɟɪ CLSID ɤɨɦɩɨɧɟɧɬɚ. ɉɪɢɦɟɱɚɧɢɟ 2.10. Ɉɛɵɱɧɨ ɧɚɡɜɚɧɢɟ Co-ɤɥɚɫɫɨɜ ɧɚɱɢɧɚɟɬɫɹ ɫ ɩɪɟɮɢɤɫɚ «Co» ɢ ɩɨɯɨɠɟ ɧɚ ɧɚɡɜɚɧɢɟ ɤɨɦɩɨɧɟɧɬɚ, ɤɨɬɨɪɵɣ ɨɧɢ ɩɨɪɨɠɞɚɸɬ.
2.7. ɉɪɢɦɟɪ ɪɟɚɥɢɡɚɰɢɢ ɜɫɬɪɨɟɧɧɨɝɨ COM-ɫɟɪɜɟɪɚ ȼ ɤɚɱɟɫɬɜɟ ɩɪɢɦɟɪɚ ɪɟɚɥɢɡɚɰɢɢ ɜɫɬɪɨɟɧɧɨɝɨ COM-ɫɟɪɜɟɪɚ ɫɨɡɞɚɞɢɦ ɤɨɦɩɨɧɟɧɬ ɩɟɪɟɜɨɞɚ ɰɟɥɨɝɨ ɱɢɫɥɚ ɜ ɞɜɨɢɱɧɵɣ, ɜɨɫɶɦɟɪɢɱɧɵɣ ɢ ɲɟɫɬɧɚɞɰɚɬɟɪɢɱɧɵɣ ɜɢɞ, ɤɨɬɨɪɵɣ ɛɭɞɟɬ ɜɵɜɨɞɢɬɶɫɹ ɜ ɜɢɞɟ ɫɬɪɨɤɢ. Ⱦɚɧɧɵɣ ɤɨɦɩɨɧɟɧɬ ɛɭɞɟɬ ɩɨɞɞɟɪɠɢɜɚɬɶ ɢɧɬɟɪɮɟɣɫ, ɩɪɢɜɟɞɟɧɧɵɣ ɜ ɩɪɢɦɟɪɟ ɩ. 2.1. 1. ȼɵɩɨɥɧɢɜ ɞɟɣɫɬɜɢɹ, ɨɩɢɫɚɧɧɵɟ ɜ ɩ. 2.2, ɫɨɯɪɚɧɢɦ ɩɪɨɟɤɬ ɩɨɞ ɢɦɟɧɟɦ Convert.dpr. 2. Ɂɚɬɟɦ ɜɵɛɟɪɟɦ ɷɥɟɦɟɧɬ COM Object ɜ ɞɢɚɥɨɝɨɜɨɦ ɨɤɧɟ New Items (ɩ. 2.3). ȼ ɦɚɫɬɟɪɟ COM Objeɫt Wizard ɨɬɤɥɸɱɢɦ ɨɩɰɢɢ Include TypeLibrary ɢ Mark Interfaced Oleautomation. Ɂɚɩɨɥɧɢɦ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɟ ɩɨɥɹ: ɜ Class Name ɜɜɟɞɟɦ ObjConvert, ɚ ɜ Implemented interfaces – IConvert. ɇɚɠɚɜ ɧɚ ɤɧɨɩɤɭ OK, ɩɨɥɭɱɢɦ ɦɨɞɭɥɶ ɫɥɟɞɭɸɳɟɝɨ ɫɨɞɟɪɠɚɧɢɹ: unit Unit1; interface uses Windows, ActiveX, ComObj; type TObjConvert = class(TComObject, IConvert) protected {Declare IConvert methods here} end; const Class_ObjConvert: TGUID = '{B65CC116-4C5F-11D5-8708854E82B6237}'; implementation uses ComServ; initialization
24
TComObjectFactory.Create(ComServer, TObjConvert, Class_ObjConvert, 'ObjConvert', 'Convert', ciSingleInstance, tmSingle); end. 3. ȼ ɪɚɡɞɟɥɟ ɨɩɢɫɚɧɢɹ ɢɫɩɨɥɶɡɭɟɦɵɯ ɦɨɞɭɥɟɣ ɩɨɫɥɟ Windows, ActiveX ɢ ComObj ɫɥɟɞɭɟɬ ɞɨɛɚɜɢɬɶ ɟɳɟ ɢ ɦɨɞɭɥɶ ConvertInterface, ɜ ɤɨɬɨɪɨɦ ɪɟɚɥɢɡɨɜɚɧ ɢɧɬɟɪɮɟɣɫ IConvert. 4. ȼ ɪɚɡɞɟɥɟ ɨɩɢɫɚɧɢɹ ɬɢɩɨɜ ɞɚɧɨ ɧɚɱɚɥɶɧɨɟ ɨɩɢɫɚɧɢɟ ɧɚɲɟɝɨ ɤɨɦɩɨɧɟɧɬɚ. ɂɫɯɨɞɹ ɢɡ ɷɬɨɝɨ ɨɩɢɫɚɧɢɹ, ɧɚɲ ɤɨɦɩɨɧɟɧɬ ɧɚɫɥɟɞɭɟɬɫɹ ɨɬ ɤɥɚɫɫɚ TɋomObject, ɢ ɩɨɞɞɟɪɠɢɜɚɟɬ ɢɧɬɟɪɮɟɣɫ IConvert. ȼ ɪɚɡɞɟɥɟ protected ɧɚɩɢɫɚɧ ɤɨɦɦɟɧɬɚɪɢɣ «Declare IConvert methods here» (ɨɩɢɲɢɬɟ ɡɞɟɫɶ ɦɟɬɨɞɵ IConvert). ȼ ɷɬɨɦ ɪɚɡɞɟɥɟ ɨɩɢɫɵɜɚɸɬɫɹ ɜɫɟ ɮɭɧɤɰɢɢ, ɤɨɬɨɪɵɟ ɩɨɞɞɟɪɠɢɜɚɸɬɫɹ ɢɧɬɟɪɮɟɣɫɨɦ: TObjConvert = class(TComObject, IConvert) protected {Declare IConvert methods here} function Bin (const n: Word; var Str: WideString): Boolean; stdcall; function Oct(const n: Word; var Str: WideString): Boolean; stdcall; function Hex(const n: Word; var Str: WideString): Boolean; stdcall; end; 5. Ɉɫɬɚɟɬɫɹ ɪɟɚɥɢɡɨɜɚɬɶ ɮɭɧɤɰɢɢ ɨɛɴɟɤɬɚ TObjConvert ɜ ɪɚɡɞɟɥɟ implementation: implementation uses ComServ; { TObjConvert } function TObjConvert.Bin(const n: Word; var Str: WideString): Boolean;
repeat S:=chr(m mod 2+48)+S; m:=m div 2; until m=0; Str:= WideString(S); except result:=false; end; end; function TObjConvert.Oct(const n: Word; var Str: WideString): Boolean; var S: String; m: Word; begin result:=true; try S: =''; m:=n; repeat S:=chr(m mod 8+48)+S; m:=m div 8; until m=0; Str:=WideString (S); except result:=false; end; end; function TObjConvert.Hex(const n: Word; var Str: WideString): Boolean; var S: String; m,k: Word;
var S: String; m: Word;
begin result:=true; try S:=''; m:=n; repeat
begin result:=true; try S:=''; m:=n; 25
26
k:=m mod 16; if k<10 then S:=chr(k+48)+S else S:=chr(57+k)+S; m:=m div 16; until m=0; Str:=WideString(S); except result:=false; end; end;
ɫɬɪɭɤɬɭɪ. Ⱦɥɹ ɷɬɨɝɨ ɩɨɧɚɞɨɛɢɬɫɹ ɢɧɬɟɪɮɟɣɫ IMarshal. ȼ ɞɚɧɧɨɦ ɦɟɬɨɞɢɱɟɫɤɨɦ ɩɨɫɨɛɢɢ ɪɟɚɥɢɡɚɰɢɹ ɦɚɪɲɚɥɢɧɝɚ ɪɚɫɫɦɚɬɪɢɜɚɬɶɫɹ ɧɟ ɛɭɞɟɬ. ȼɧɟɲɧɢɟ COM-ɫɟɪɜɟɪɵ, ɤɚɤ ɢ ɨɛɵɱɧɵɟ ɩɪɢɥɨɠɟɧɢɹ, ɦɨɠɧɨ ɡɚɤɪɵɬɶ ɫɚɦɨɫɬɨɹɬɟɥɶɧɨ, ɧɨ ɟɫɥɢ ɧɚ ɤɨɦɩɨɧɟɧɬɵ ɫɟɪɜɟɪɚ ɫɫɵɥɚɸɬɫɹ ɤɥɢɟɧɬɵ, ɬɨ ɫɢɫɬɟɦɚ ɜɵɞɚɟɬ ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɟ ɫɨɨɛɳɟɧɢɟ (ɪɢɫ. 2.3).
6. ɉɪɢɥɨɠɟɧɢɟ ɦɨɠɧɨ ɨɬɤɨɦɩɢɥɢɪɨɜɚɬɶ, ɧɚɠɚɜ ɫɨɱɟɬɚɧɢɟ ɤɥɚɜɢɲ Ctrl+F9. ɋɨɡɞɚɧɧɵɣ ɩɪɢ ɷɬɨɦ COM-ɫɟɪɜɟɪ, ɧɚɯɨɞɹɳɢɣɫɹ ɜ ɛɢɛɥɢɨɬɟɤɟ Convert.dll, ɝɨɬɨɜ ɤ ɢɫɩɨɥɶɡɨɜɚɧɢɸ. ȼ ɞɚɧɧɨɦ ɩɪɢɦɟɪɟ ɢɫɩɨɥɶɡɭɸɬɫɹ ɫɬɪɨɤɢ ɬɢɩɚ WideString. ɗɬɨ ɫɬɚɧɞɚɪɬɧɵɣ ɫɬɪɨɤɨɜɵɣ ɬɢɩ, ɢɫɩɨɥɶɡɭɟɦɵɣ ɜ ɬɟɯɧɨɥɨɝɢɢ COM. ȼ ɩɪɨɫɬɵɯ ɫɬɪɨɤɚɯ (String, ShortString, PChar ɢ ɬ. ɩ.) ɫɢɦɜɨɥɵ ɢɦɟɸɬ ɪɚɡɦɟɪɧɨɫɬɶ 8 ɛɢɬ (1 ɛɚɣɬ), ɚ ɜ WideString – 16 ɛɢɬ (2 ɛɚɣɬɚ). ɗɬɨ ɞɚɟɬ ɜɨɡɦɨɠɧɨɫɬɶ ɩɟɪɟɞɚɱɢ ɫɬɪɨɤ ɜ ɤɨɞɢɪɨɜɤɟ Unicode.
Ɋɢɫ. 2.3. ɉɪɟɞɭɩɪɟɠɞɟɧɢɟ, ɜɵɡɵɜɚɟɦɨɟ ɩɪɢ ɡɚɤɪɵɬɢɢ ɜɧɟɲɧɟɝɨ COM-ɤɥɢɟɧɬɚ, ɟɫɥɢ ɧɚ ɤɨɦɩɨɧɟɧɬɵ ɫɟɪɜɟɪɚ ɫɫɵɥɚɸɬɫɹ ɤɥɢɟɧɬɵ ɉɪɢɦɟɪ ɜɧɟɲɧɟɝɨ COM-ɫɟɪɜɟɪɚ ɛɭɞɟɬ ɩɪɢɜɟɞɟɧ ɜ ɩɹɬɨɣ ɝɥɚɜɟ, ɬɚɤ ɤɚɤ ɞɥɹ ɪɟɚɥɢɡɚɰɢɢ ɬɚɤɢɯ ɫɟɪɜɟɪɨɜ ɬɪɟɛɭɟɬɫɹ ɛɢɛɥɢɨɬɟɤɚ ɬɢɩɨɜ, ɤɨɬɨɪɚɹ ɛɭɞɟɬ ɪɚɫɫɦɨɬɪɟɧɚ ɜ ɝɥ. III.
2.8. ȼɧɟɲɧɢɟ COM-ɫɟɪɜɟɪɵ 2.9. Ȼɟɡɨɩɚɫɧɵɟ ɦɚɫɫɢɜɵ (SafeArray) ȼɧɟɲɧɢɟ COM-ɫɟɪɜɟɪɵ ɪɟɚɥɢɡɨɜɚɧɵ ɜ ɜɢɞɟ ɩɪɨɝɪɚɦɦ (exe-ɮɚɣɥɨɜ) ɢ ɜ ɩɪɨɰɟɫɫɟ ɜɵɩɨɥɧɟɧɢɹ ɡɚɧɢɦɚɸɬ ɚɞɪɟɫɧɨɟ ɩɪɨɫɬɪɚɧɫɬɜɨ, ɨɬɥɢɱɧɨɟ ɨɬ ɤɥɢɟɧɬɫɤɨɝɨ. ɗɬɨ ɩɪɢɜɨɞɢɬ ɤ ɧɟɤɨɬɨɪɵɦ ɧɸɚɧɫɚɦ ɩɪɢ ɫɨɡɞɚɧɢɢ ɬɚɤɨɝɨ ɪɨɞɚ COM-ɫɟɪɜɟɪɨɜ. ȼ ɨɬɥɢɱɢɟ ɨɬ ɜɫɬɪɨɟɧɧɵɯ, ɜɧɟɲɧɢɟ COM-ɫɟɪɜɟɪɵ ɧɟ ɷɤɫɩɨɪɬɢɪɭɸɬ ɮɭɧɤɰɢɢ DllRegisterServer, DllUnregisterServer, DllGetClassObject ɢ DllCanUnloadNow. Ɍɚɤ ɤɚɤ ɤɥɢɟɧɬ ɢ ɜɧɟɲɧɢɣ COM-ɫɟɪɜɟɪ ɧɟ ɦɨɝɭɬ ɩɨɥɭɱɢɬɶ ɩɪɹɦɨɣ ɞɨɫɬɭɩ ɤ ɚɞɪɟɫɧɨɦɭ ɩɪɨɫɬɪɚɧɫɬɜɭ ɞɪɭɝ ɞɪɭɝɚ, ɫɢɫɬɟɦɚ COM ɩɟɪɟɞɚɟɬ ɞɚɧɧɵɟ ɦɟɠɞɭ ɧɢɦɢ ɩɨɫɪɟɞɫɬɜɨɦ ɩɪɨɰɟɫɫɚ, ɧɚɡɵɜɚɟɦɨɝɨ ɦɚɪɲɚɥɢɧɝɨɦ 1 . ɋɢɫɬɟɦɚ COM, ɪɟɚɥɢɡɨɜɚɧɧɚɹ ɜ ɨɩɟɪɚɰɢɨɧɧɨɣ ɫɢɫɬɟɦɟ Windows, ɦɨɠɟɬ ɩɟɪɟɞɚɜɚɬɶ ɩɨɫɪɟɞɫɬɜɨɦ ɦɚɪɲɚɥɢɧɝɚ ɩɟɪɟɦɟɧɧɵɟ ɫɥɟɞɭɸɳɢɯ ɬɢɩɨɜ: Smallint, Integer, Single, Double, Currency, TdateTime, WideString, IDispatch, SCODE, WordBool, OleVariant, IUnknown, ShortInt, Byte, Word, UINT, int64, Largeint, SYSINT, SYSUINT, HResult, Pointer, SafeArray, PChar, PWideChar. ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɜɫɟ ɩɟɪɟɱɢɫɥɟɧɧɵɟ ɬɢɩɵ ɨɬɧɨɫɹɬɫɹ ɤ ɤɚɬɟɝɨɪɢɢ ɩɪɨɫɬɵɯ ɬɢɩɨɜ, ɬɚɤ ɤɚɤ ɦɚɪɲɚɥɢɧɝ ɩɟɪɟɦɟɧɧɵɯ ɫɥɨɠɧɵɯ ɬɢɩɨɜ (ɦɚɫɫɢɜɵ, ɡɚɩɢɫɢ, ɫɬɪɭɤɬɭɪɵ ɢ ɬ. ɩ.) ɫɢɫɬɟɦɚ COM ɧɟ ɩɨɞɞɟɪɠɢɜɚɟɬ. ȿɫɥɢ ɜɫɟɬɚɤɢ ɬɪɟɛɭɟɬɫɹ ɩɟɪɟɞɚɬɶ ɞɚɧɧɵɟ ɬɢɩɚ ɡɚɩɢɫɶ ɢɥɢ ɦɚɫɫɢɜ, ɧɭɠɧɨ ɫɚɦɨɫɬɨɹɬɟɥɶɧɨ ɡɚɩɪɨɝɪɚɦɦɢɪɨɜɚɬɶ ɩɨɞɞɟɪɠɤɭ ɦɟɯɚɧɢɡɦɚ ɦɚɪɲɚɥɢɧɝɚ ɞɥɹ ɬɚɤɢɯ 1
Ȼɟɡɨɩɚɫɧɵɟ ɦɚɫɫɢɜɵ (ɬɢɩ SafeArray) ɪɟɚɥɢɡɨɜɚɧɵ ɧɚ ɨɫɧɨɜɟ ɜɚɪɢɚɧɬɧɵɯ ɦɚɫɫɢɜɨɜ. ɉɪɨɰɟɞɭɪɵ ɢ ɮɭɧɤɰɢɢ ɞɥɹ ɪɚɛɨɬɵ ɫ ɜɚɪɢɚɧɬɧɵɦɢ ɦɚɫɫɢɜɚɦɢ ɩɪɢɜɟɞɟɧɵ ɜ ɫɥɟɞɭɸɳɟɣ ɬɚɛɥɢɰɟ. ɉɨɞɩɪɨɝɪɚɦɦɚ
Ɉɩɢɫɚɧɢɟ ɋɨɡɞɚɟɬ ɜɚɪɢɚɧɬɧɵɣ ɦɚɫɫɢɜ. function VarArrayCreate(const Bounds: array of Integer; VarType: TVarType): Vari- ɉɚɪɚɦɟɬɪ Bounds ɫɨɞɟɪɠɢɬ ɪɚɡɦɟɪɧɨɫɬɶ ɦɚɫɫɢɜɚ. ɉɚɪɚant; ɦɟɬɪ VarType ɨɩɪɟɞɟɥɹɟɬ ɬɢɩ ɯɪɚɧɢɦɵɯ ɞɚɧɧɵɯ ɋɨɡɞɚɟɬ ɨɞɧɨɦɟɪɧɵɣ function VarArrayOf(const Values: array ɜɚɪɢɚɧɬɧɵɣ ɦɚɫɫɢɜ ɢɡ of Variant): Variant; ɷɥɟɦɟɧɬɨɜ Values. ɂɧɞɟɤɫɚɰɢɹ ɧɚɱɢɧɚɟɬɫɹ ɫ ɧɭɥɹ ȼɨɡɜɪɚɳɚɟɬ ɫɫɵɥɤɭ ɧɚ ɜɚɪɢfunction VarArrayRef(const A: Variant): ɚɧɬɧɵɣ ɦɚɫɫɢɜ Variant; function VarArrayDimCount(const A: Variant): Integer;
Ɉɬ ɫɥɨɜɚ Marshaling – ɜɵɫɬɪɚɢɜɚɧɢɟ ɜ ɨɩɪɟɞɟɥɟɧɧɨɦ ɩɨɪɹɞɤɟ. 27
28
ȼɨɡɜɪɚɳɚɟɬ ɤɨɥɢɱɟɫɬɜɨ ɢɡɦɟɪɟɧɢɣ ɜ ɜɚɪɢɚɧɬɧɨɦ ɦɚɫɫɢɜɟ ȼ ɫɥɭɱɚɟ, ɟɫɥɢ ɷɬɨ ɧɟ ɦɚɫɫɢɜ, ɜɨɡɜɪɚɳɚɟɬ 0
ȼɨɡɜɪɚɳɚɟɬ ɧɢɠɧɸɸ ɝɪɚɧɢɰɭ ɢɡɦɟɪɟɧɢɹ Dim ɞɥɹ ɦɚɫɫɢɜɚ ȼɨɡɜɪɚɳɚɟɬ ɜɟɪɯɧɸɸ ɝɪɚfunction VarArrayHighBound(const A: ɧɢɰɭ ɢɡɦɟɪɟɧɢɹ Dim ɞɥɹ Variant; Dim: Integer): Integer; ɦɚɫɫɢɜɚ function VarArrayLock(const A: Variant): Ȼɥɨɤɢɪɭɟɬ ɜɚɪɢɚɧɬɧɵɣ ɦɚɫɫɢɜ ɢ ɜɨɡɜɪɚɳɚɟɬ ɫɫɵɥɤɭ ɧɚ Pointer; ɧɟɝɨ ɋɧɢɦɚɟɬ ɛɥɨɤɢɪɨɜɤɭ ɫ ɜɚprocedure VarArrayUnlock(var A: Variɪɢɚɧɬɧɨɝɨ ɦɚɫɫɢɜɚ, ɡɚɞɚɧant); ɧɭɸ ɩɪɟɞɵɞɭɳɟɣ ɮɭɧɤɰɢɟɣ function VarIsNull(const V: Variant): Boo- ȼɨɡɜɪɚɳɚɟɬ ɢɫɬɢɧɭ, ɟɫɥɢ ɜɚɪɢɚɧɬ ɫɨɞɟɪɠɢɬ ɩɭɫɬɨɟ lean; ɡɧɚɱɟɧɢɟ ɂɡɦɟɧɹɟɬ ɪɚɡɦɟɪ ɜɚɪɢɚɧɬprocedure VarArrayRedim(A: Variant; ɧɨɝɨ ɦɚɫɫɢɜɚ HighBound: Integer); function VarArrayLowBound(const A: Variant; Dim: Integer): Integer;
ȼ ɫɥɟɞɭɸɳɟɦ ɩɭɧɤɬɟ ɩɪɢɜɟɞɟɧ ɩɪɢɦɟɪ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɜɚɪɢɚɧɬɧɵɯ ɦɚɫɫɢɜɨɜ.
2.10. Ɉɛɪɚɛɨɬɤɚ ɦɚɫɫɢɜɨɜ ɞɚɧɧɵɯ Ɋɚɫɫɦɨɬɪɢɦ ɮɭɧɤɰɢɨɧɚɥɶɧɵɟ ɜɨɡɦɨɠɧɨɫɬɢ ɛɟɡɨɩɚɫɧɵɯ ɦɚɫɫɢɜɨɜ ɧɚ ɩɪɢɦɟɪɟ. 2.10.1. ɋɨɡɞɚɧɢɟ ɫɟɪɜɟɪɚ Ʉɚɤ ɢ ɜ ɩɪɟɞɵɞɭɳɟɦ ɩɪɢɦɟɪɟ, ɫɨɡɞɚɞɢɦ ɜɧɟɲɧɢɣ ɫɟɪɜɟɪ ɚɜɬɨɦɚɬɢɡɚɰɢɢ. ȼ ɪɟɞɚɤɬɨɪɟ ɛɢɛɥɢɨɬɟɤɢ ɬɢɩɨɜ ɨɩɪɟɞɟɥɢɦ ɞɜɚ ɢɧɬɟɪɮɟɣɫɚ – IvectSrv ɢ IMatrixSrv. ȼ ɩɟɪɜɨɦ ɢɧɬɟɪɮɟɣɫɟ ɫɨɡɞɚɞɢɦ ɦɟɬɨɞ ChangeVect, ɜɵɩɨɥɧɹɸɳɢɣ ɩɟɪɟɫɬɚɧɨɜɤɭ ɷɥɟɦɟɧɬɨɜ ɦɚɫɫɢɜɚ. Ɍɢɩ ɩɟɪɜɨɝɨ ɩɚɪɚɦɟɬɪɚ (ɢɫɯɨɞɧɵɣ ɦɚɫɫɢɜ) ɨɩɪɟɞɟɥɢɦ ɤɚɤ Variant ɫ ɦɨɞɢɮɢɤɚɬɨɪɨɦ in, ɬɢɩ ɜɬɨɪɨɝɨ (ɪɟɡɭɥɶɬɚɬ ɜɵɱɢɫɥɟɧɢɣ) – Variant ɫ ɦɨɞɢɮɢɤɚɬɨɪɨɦ out. ȼɨ ɜɬɨɪɨɦ ɢɧɬɟɪɮɟɣɫɟ IMatrixSrv ɞɥɹ ɬɪɚɧɫɩɨɧɢɪɨɜɚɧɢɹ ɦɚɬɪɢɰɵ ɫɨɡɞɚɞɢɦ ɦɟɬɨɞ Trans ɫ ɚɧɚɥɨɝɢɱɧɵɦɢ ɩɚɪɚɦɟɬɪɚɦɢ. ɋɝɟɧɟɪɢɪɭɟɦ ɦɨɞɭɥɶ ɫ ɨɩɪɟɞɟɥɟɧɢɟɦ ɤɨɦɩɨɧɟɧɬɧɨɝɨ ɤɥɚɫɫɚ ɢ ɡɚɩɢɲɟɦ ɜ ɧɟɝɨ ɪɟɚɥɢɡɚɰɢɸ ɭɩɨɦɹɧɭɬɵɯ ɦɟɬɨɞɨɜ: type TVectSrv = class(TAutoObject, IVectSrv, IMatrixSrv) protected procedure ChangeVect(v:OleVariant; out z:OleVariant); safecall; 29
procedure Trans(a: OleVariant; out b: OleVariant); safecall; end; implementation uses ComServ, variants; procedure TVectSrv.ChangeVect(v: OleVariant; out z: OleVariant); var dim, i,j: integer; begin dim := VarArrayDimCount(v); // ɱɢɫɥɨ ɢɡɦɟɪɟɧɢɣ if dim = 1 then begin z := v; // Ɉɩɪɟɞɟɥɹɟɦ ɪɚɡɦɟɪɧɨɫɬɢ ɩɨ X ɩɟɪɟɞɚɧɧɨɝɨ ɦɚɫɫɢɜɚ i := VarArrayLowBound(v,1); j := VarArrayHighBound(v,1); // Ɇɟɧɹɟɦ ɦɟɫɬɚɦɢ ɩɟɪɜɵɣ ɢ ɩɨɫɥɟɞɧɢɣ ɷɥɟɦɟɧɬ z[i]:=v[j]; z[j]:=v[i]; end; end; procedure TVectSrv.Trans(a: OleVariant; out b: OleVariant); var dim, n, m, i0,j0,i, j: integer; begin b := a; dim := VarArrayDimCount(a); // ɱɢɫɥɨ ɢɡɦɟɪɟɧɢɣ if dim = 2 then begin // Ɉɩɪɟɞɟɥɹɟɦ ɪɚɡɦɟɪɧɨɫɬɢ ɩɨ X ɩɟɪɟɞɚɧɧɨɝɨ ɦɚɫɫɢɜɚ i0 := VarArrayLowBound(a,1); n := VarArrayHighBound(a,1); // .. ɢ ɪɚɡɦɟɪɧɨɫɬɢ ɩɨ Y j0 := VarArrayLowBound(a,2); m := VarArrayHighBound(a,2); // Ɍɪɚɧɫɩɨɧɢɪɭɟɦ for i := i0 to n do for j := j0 to m do b[i,j] := a[j,i]; end; end; 30
2.10.2. ɋɨɡɞɚɧɢɟ ɤɥɢɟɧɬɚ type TMainForm = class(TForm) lb: TListBox; bCreate: TButton; bRun: TButton; sg: TStringGrid; procedure bCreateClick(Sender: TObject); procedure bRunClick(Sender: TObject); private { Private declarations } public v: IVectSrv; vm: IMatrixSrv; { Public declarations } end; var MainForm: TMainForm; implementation var X, Y, A , B: olevariant; // ɇɭɠɧɵ ɞɥɹ ɩɟɪɟɞɚɱɢ ɜ COM ɫɟɪɜɟɪ DynArr : array of array of integer; // ɞɢɧɚɦɢɱɟɫɤɢɣ ɞɜɭɦɟɪɧɵɣ ɦɚɫɫɢɜ {$R *.dfm} procedure TMainForm.bCreateClick(Sender: TObject); var I, J: Integer; begin // Ⱦɥɹ ɢɥɥɸɫɬɪɚɰɢɢ ɡɞɟɫɶ ɩɪɢɦɟɧɟɧɵ ɞɜɚ ɫɩɨɫɨɛɚ. // Ɉɞɧɨɦɟɪɧɵɣ ɦɚɫɫɢɜ ɫɨɡɞɚɟɬɫɹ ɤɚɤ ɜɚɪɢɚɧɬɧɵɣ. // Ɇɚɬɪɢɰɚ - ɤɚɤ ɞɢɧɚɦɢɱɟɫɤɢɣ ɞɜɭɦɟɪɧɵɣ ɦɚɫɫɢɜ. // Ɂɚɞɚɟɦ ɞɥɢɧɭ 2 ɧɚ 2 ɞɜɭɦɟɪɧɨɝɨ ɞɢɧɚɦɢɱɟɫɤɨɝɨ ɦɚɫɫɢɜɚ SetLength (DynArr,2,2); // .. ɢ ɡɚɩɨɥɧɹɟɦ (ɥɭɱɲɟ ɷɬɨ ɫɞɟɥɚɬɶ ɩɨ-ɞɪɭɝɨɦɭ - ɢɡ ɮɚɣɥɚ, ɫɩɢɫɤɚ ɢɥɢ ɫɟɬɤɢ) DynArr[0,0]:=1; DynArr[1,1]:=3; 31
DynArr[0,1]:=4; DynArr[1,0]:=2; // ȼɵɜɨɞɢɦ ɜ ɫɟɬɤɭ sg.ColCount := 2; sg.RowCount := 2; with sg do for I := 0 to ColCount - 1 do for J:= 0 to RowCount - 1 do Cells[J,I] := IntToStr(DynArr[I,J]); // ɋɨɡɞɚɟɦ ɨɞɧɨɦɟɪɧɵɣ ɜɚɪɢɚɧɬɧɵɣ ɦɚɫɫɢɜ X := VarArrayOf ([1.2, 2.5, 3, 4.8]); // ȼɵɜɨɞɢɦ ɜ ListBox lb.Clear; for i:=0 to VarArrayHighBound(X,1) do lb.Items.Add(FloatToStr(X[i])); // Ɂɚɩɭɫɤɚɟɦ ɫɟɪɜɟɪ V := CreateComObject(Class_VectSrv) as IVectSrv; if V=Nil then ShowMessage ('ɋɟɪɜɟɪ ɧɟ ɧɚɣɞɟɧ!'); end; procedure TMainForm.bRunClick(Sender: TObject); var I, J: Integer; begin if V <> Nil then begin try // ȼɵɡɵɜɚɟɦ ɦɟɬɨɞ ɢɧɬɟɪɮɟɣɫɚ (ɦɟɧɹɟɬ ɦɟɫɬɚɦɢ ɷɥɟɦɟɧɬɵ) V.ChangeVect(x,y); // ȼɵɜɨɞɢɦ ɪɟɡɭɥɶɬɚɬ lb.Clear; for i:=0 to VarArrayHighBound(Y,1) do lb.Items.Add(FloatToStr(Y[i])); Vm := V as IMatrixSrv; // Ɉɛɪɚɳɚɟɦɫɹ ɤ ɞɪɭɝɨɦɭ ɢɧɬɟɪɮɟɣɫɭ A := DynArr; // Ɂɚɩɢɫɵɜɚɟɦ ɜ ɜɚɪɢɚɧɬ ɞɢɧɚɦɢɱɟɫɤɢɣ ɦɚɫɫɢɜ Vm.Trans(A,B); // ȼɵɡɵɜɚɟɦ ɦɟɬɨɞ (ɬɪɚɧɫɩɨɧɢɪɨɜɚɧɢɟ) // ȼɵɜɨɞɢɦ ɜ ɫɟɬɤɭ with SG do for I := 0 to ColCount - 1 do for J:= 0 to RowCount - 1 do 32
Cells[J,I] := IntToStr(B[I,J]); except on EOleException do ShowMessage ('Com ɫɟɪɜɟɪ ɧɟɞɨɫɬɭɩɟɧ!'); else ShowMessage ('ɑɬɨ-ɬɨ ɧɟ ɬɚɤ ...'); end; end; end; end.
III. Ȼɢɛɥɢɨɬɟɤɚ ɬɢɩɨɜ COM-ɫɟɪɜɟɪɵ ɢ ɤɥɢɟɧɬɵ, ɤɨɬɨɪɵɟ ɜɡɚɢɦɨɞɟɣɫɬɜɭɸɬ ɞɪɭɝ ɫ ɞɪɭɝɨɦ, ɞɨɥɠɧɵ ɩɨɞɞɟɪɠɢɜɚɬɶ ɨɞɢɧɚɤɨɜɨɟ ɨɩɢɫɚɧɢɟ ɢɧɬɟɪɮɟɣɫɨɜ ɢ ɩɪɚɜɢɥɚ ɩɟɪɟɞɚɱɢ ɩɚɪɚɦɟɬɪɨɜ. Ʉɨɝɞɚ ɤɥɢɟɧɬɵ ɢ ɫɟɪɜɟɪɵ ɩɪɨɟɤɬɢɪɭɸɬɫɹ ɧɚ ɨɞɧɨɦ ɹɡɵɤɟ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ, ɷɬɨɣ ɩɪɨɛɥɟɦɵ ɧɟ ɜɨɡɧɢɤɚɟɬ, ɬɚɤ ɤɚɤ ɦɨɠɧɨ ɩɪɢɫɨɟɞɢɧɢɬɶ ɨɞɢɧ ɢ ɬɨɬ ɠɟ ɦɨɞɭɥɶ ɨɛɴɹɜɥɟɧɢɹ ɢɧɬɟɪɮɟɣɫɨɜ. ɑɬɨɛɵ ɪɚɛɨɬɚɬɶ ɧɟɡɚɜɢɫɢɦɨ ɨɬ ɹɡɵɤɚ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ, ɛɵɥɨ ɪɚɡɪɚɛɨɬɚɧɨ ɫɬɚɧɞɚɪɬɧɨɟ ɨɩɢɫɚɧɢɟ ɢɧɬɟɪɮɟɣɫɚ, ɤɨɬɨɪɨɟ ɧɚɡɵɜɚɸɬ ɛɢɛɥɢɨɬɟɤɨɣ ɬɢɩɨɜ. Ȼɢɛɥɢɨɬɟɤɚ ɬɢɩɨɜ ɧɟɡɚɜɢɫɢɦɚ ɨɬ ɹɡɵɤɚ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ, ɢ ɜ ɩɪɨɟɤɬ ɨɧɚ ɦɨɠɟɬ ɛɵɬɶ ɜɧɟɫɟɧɚ ɜ ɜɢɞɟ ɪɟɫɭɪɫɚ ɫ ɪɚɫɲɢɪɟɧɢɟɦ tlb.
3.1. ɉɪɨɟɤɬɢɪɨɜɚɧɢɟ ɛɢɛɥɢɨɬɟɤɢ ɬɢɩɨɜ. Ɋɟɞɚɤɬɨɪ Type Library Editor
Ɋɢɫ. 3.1. Ɋɟɞɚɤɬɨɪ Type Library Editor
3.2. ɉɚɧɟɥɶ ɢɧɫɬɪɭɦɟɧɬɨɜ ɞɢɚɥɨɝɨɜɨɝɨ ɨɤɧɚ Type Library Editor
Ⱦɥɹ ɬɨɝɨ, ɱɬɨɛɵ ɛɢɛɥɢɨɬɟɤɭ ɬɢɩɨɜ ɩɨɞɤɥɸɱɢɬɶ ɤ ɪɚɡɪɚɛɚɬɵɜɚɟɦɨɦɭ COM-ɫɟɪɜɟɪɭ ɚɜɬɨɦɚɬɢɱɟɫɤɢ, ɧɭɠɧɨ ɜ ɨɤɧɟ ɦɚɫɬɟɪɚ COM Object Wizard (ɪɢɫ. 2.2) ɜɵɫɬɚɜɢɬɶ ɮɥɚɠɨɤ Include Type Library. Ⱦɥɹ ɨɩɢɫɚɧɢɹ ɞɚɧɧɵɯ ɜ ɛɢɛɥɢɨɬɟɤɟ ɬɢɩɨɜ ɢɫɩɨɥɶɡɭɟɬɫɹ ɫɩɟɰɢɚɥɶɧɵɣ ɹɡɵɤ IDL (Interface Definition Language – ɹɡɵɤ ɨɩɪɟɞɟɥɟɧɢɹ ɢɧɬɟɪɮɟɣɫɨɜ). Ⱦɥɹ ɚɜɬɨɦɚɬɢɡɚɰɢɢ ɪɚɛɨɬɵ ɫ ɛɢɛɥɢɨɬɟɤɨɣ ɬɢɩɨɜ ɜ Delphi ɪɚɡɪɚɛɨɬɚɧ ɫɩɟɰɢɚɥɶɧɵɣ ɪɟɞɚɤɬɨɪ Type Library Editor (ɪɢɫ. 3.1), ɤɨɬɨɪɵɣ ɦɨɠɧɨ ɡɚɩɭɫɬɢɬɶ, ɜɵɛɪɚɜ ɜ ɝɥɚɜɧɨɦ ɦɟɧɸ Vew/Type Library Editor. Ɉɤɧɨ Type Library Editor ɪɚɡɞɟɥɟɧɨ ɧɚ ɬɪɢ ɱɚɫɬɢ. ȼɜɟɪɯɭ ɪɚɫɩɨɥɨɠɟɧɚ ɩɚɧɟɥɶ ɢɧɫɬɪɭɦɟɧɬɨɜ, ɫ ɩɨɦɨɳɶɸ ɤɨɬɨɪɨɣ ɦɨɠɧɨ ɜɤɥɸɱɚɬɶ ɜ COMɫɟɪɜɟɪ ɧɨɜɵɟ ɢɧɬɟɪɮɟɣɫɵ, ɦɟɬɨɞɵ ɢ ɫɜɨɣɫɬɜɚ. ɋɥɟɜɚ ɪɚɫɩɨɥɨɠɟɧɨ ɩɨɥɟ ɫɩɢɫɤɚ ɨɛɴɟɤɬɨɜ, ɜ ɤɨɬɨɪɨɦ ɩɪɟɞɫɬɚɜɥɟɧɵ ɞɚɧɧɵɟ ɜ ɞɪɟɜɨɜɢɞɧɨɣ ɫɬɪɭɤɬɭɪɟ. ɋɩɪɚɜɚ – ɩɚɧɟɥɶ ɫ ɡɚɤɥɚɞɤɚɦɢ, ɜ ɤɨɬɨɪɵɯ ɪɟɞɚɤɬɢɪɭɟɬɫɹ ɷɥɟɦɟɧɬ, ɜɵɛɪɚɧɧɵɣ ɜ ɩɨɥɟ ɫɩɢɫɤɚ ɨɛɴɟɤɬɨɜ.
Ⱦɥɹ ɞɨɛɚɜɥɟɧɢɹ ɧɨɜɵɯ ɢɧɬɟɪɮɟɣɫɨɜ, ɫɜɨɣɫɬɜ ɢ ɦɟɬɨɞɨɜ ɢɫɩɨɥɶɡɭɟɬɫɹ ɩɚɧɟɥɶ ɢɧɫɬɪɭɦɟɧɬɨɜ (ɪɢɫ. 3.2), ɤɨɬɨɪɚɹ ɫɨɞɟɪɠɢɬ ɫɥɟɞɭɸɳɢɟ ɮɭɧɤɰɢɨɧɚɥɶɧɵɟ ɤɧɨɩɤɢ (ɫɥɟɜɚ ɧɚɩɪɚɜɨ): New Interface – ɩɨɫɥɟ ɳɟɥɱɤɚ ɧɚ ɷɬɨɣ ɤɧɨɩɤɟ ɛɭɞɟɬ ɞɨɛɚɜɥɟɧ ɧɨɜɵɣ ɢɧɬɟɪɮɟɣɫ; New Dispinterface – ɞɨɛɚɜɥɹɟɬ ɧɨɜɵɣ ɢɧɬɟɪɮɟɣɫ ɞɢɫɩɟɬɱɟɪɢɡɚɰɢɢ. ɂɧɬɟɪɮɟɣɫ ɞɢɫɩɟɬɱɟɪɢɡɚɰɢɢ (dispinterface) ɜɨ ɦɧɨɝɨɦ ɫɯɨɠ ɫ ɨɛɵɱɧɵɦ ɢɧɬɟɪɮɟɣɫɨɦ, ɧɨ ɢɫɩɨɥɶɡɭɟɬ ɨɬɥɢɱɧɵɣ ɨɬ ɧɟɝɨ ɦɟɯɚɧɢɡɦ ɞɢɫɩɟɬɱɟɪɢɡɚɰɢɢ ɞɥɹ ɜɵɡɨɜɚ ɦɟɬɨɞɨɜ ɫɟɪɜɟɪɚ (ɫɦ. ɝɥ. V); New CoClass – ɞɨɛɚɜɥɹɟɬ ɫɨɩɪɹɠɟɧɧɵɣ ɤɥɚɫɫ. ɋɨɩɪɹɠɟɧɧɵɣ ɤɥɚɫɫ (CoClass) – ɷɬɨ ɤɥɚɫɫ ɤɨɦɩɨɧɟɧɬɚ, ɜ ɤɨɬɨɪɨɦ ɪɟɚɥɢɡɨɜɚɧ ɢɧɬɟɪɮɟɣɫ (ɢɥɢ ɢɧɬɟɪɮɟɣɫɵ);
33
34
Ɋɢɫ. 3.2. ɉɚɧɟɥɶ ɢɧɫɬɪɭɦɟɧɬɨɜ ɪɟɞɚɤɬɨɪɚ Type Library Editor
New Enumeration – ɞɨɛɚɜɥɹɟɬ ɩɟɪɟɱɢɫɥɟɧɢɟ. ɉɟɪɟɱɢɫɥɟɧɢɟ (enumeration) ɜ ɛɢɛɥɢɨɬɟɤɟ ɬɢɩɨɜ ɚɧɚɥɨɝɢɱɧɨ ɩɟɪɟɱɢɫɥɢɦɨɦɭ ɬɢɩɭ ɜ Delphi: ɨɛɴɹɜɥɹɟɬ ɧɚɛɨɪ ɰɟɥɵɯ ɢɞɟɧɬɢɮɢɤɚɬɨɪɨɜ; New Alias – ɫɨɡɞɚɟɬ ɩɫɟɜɞɨɧɢɦ. ɉɫɟɜɞɨɧɢɦ (alias) ɢɫɩɨɥɶɡɭɟɬɫɹ ɜ ɛɢɛɥɢɨɬɟɤɟ ɬɢɩɨɜ ɞɥɹ ɫɩɟɰɢɮɢɤɚɰɢɢ ɬɢɩɚ, ɤɨɬɨɪɵɣ ɠɟɥɚɬɟɥɶɧɨ ɜɤɥɸɱɢɬɶ ɜ ɫɨɫɬɚɜ ɡɚɩɢɫɢ ɢɥɢ ɨɛɴɟɞɢɧɟɧɢɹ (union); New Record – ɡɚɞɚɟɬ ɫɬɪɭɤɬɭɪɭ ɡɚɩɢɫɢ; New Union – ɡɚɞɚɟɬ ɬɢɩ ɨɛɴɟɞɢɧɟɧɢɹ. Ɉɛɴɟɞɢɧɟɧɢɟ (union) – ɷɬɨ ɷɤɜɢɜɚɥɟɧɬ ɦɚɫɫɢɜɚ ɜɚɪɢɚɧɬɧɨɝɨ ɬɢɩɚ (array of variant); New Module – ɨɩɪɟɞɟɥɹɟɬ ɧɨɜɵɣ ɦɨɞɭɥɶ. Ɇɨɞɭɥɶ ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ ɧɚɛɨɪ ɦɟɬɨɞɨɜ ɢ ɤɨɧɫɬɚɧɬ; New Method – ɞɨɛɚɜɥɹɟɬ ɧɨɜɵɣ ɦɟɬɨɞ ɜ ɢɧɬɟɪɮɟɣɫ. ȿɫɥɢ ɜ ɫɩɢɫɤɟ ɨɛɴɟɤɬɨɜ ɜɵɞɟɥɢɬɶ ɢɧɬɟɪɮɟɣɫ, ɬɨ ɷɬɚ ɤɧɨɩɤɚ ɫɬɚɧɨɜɢɬɫɹ ɚɤɬɢɜɧɨɣ; New Property – ɞɨɛɚɜɥɹɟɬ ɧɨɜɨɟ ɫɜɨɣɫɬɜɨ ɜ ɢɧɬɟɪɮɟɣɫ. Ɍɚɤ ɠɟ, ɤɚɤ ɢ ɜ ɨɛɵɱɧɵɣ ɤɥɚɫɫ Delphi, ɜ ɢɧɬɟɪɮɟɣɫ ɦɨɠɧɨ ɞɨɛɚɜɢɬɶ ɫɜɨɣɫɬɜɨ, ɤɨɬɨɪɨɟ ɛɭɞɟɬ ɬɨɥɶɤɨ ɞɥɹ ɡɚɩɢɫɢ (read-only), ɬɨɥɶɤɨ ɞɥɹ ɱɬɟɧɢɹ (write-only) ɢɥɢ ɞɥɹ ɱɬɟɧɢɹ ɢ ɡɚɩɢɫɢ (read-write) ɨɞɧɨɜɪɟɦɟɧɧɨ; Refresh Implementation – ɩɪɢ ɧɚɠɚɬɢɢ ɧɚ ɷɬɭ ɤɧɨɩɤɭ ɨɛɧɨɜɥɹɟɬɫɹ ɩɪɨɝɪɚɦɦɧɵɣ ɤɨɞ ɩɪɢɥɨɠɟɧɢɹ ɫɨɨɬɜɟɬɫɬɜɟɧɧɨ ɞɚɧɧɵɦ ɜ ɪɟɞɚɤɬɨɪɟ Type Library Editor; Register Type Library – ɩɨɫɥɟ ɳɟɥɱɤɚ ɧɚ ɷɬɨɣ ɤɧɨɩɤɟ Delphi ɡɚɩɭɫɬɢɬ ɩɪɨɰɟɫɫ ɤɨɦɩɢɥɹɰɢɢ ɫɨɡɞɚɜɚɟɦɨɝɨ COM-ɫɟɪɜɟɪɚ ɢ ɡɚɪɟɝɢɫɬɪɢɪɭɟɬ ɟɝɨ ɜ ɫɢɫɬɟɦɧɨɦ ɪɟɟɫɬɪɟ Windows; Export to IDL – ɫ ɩɨɦɨɳɶɸ ɷɬɨɣ ɤɧɨɩɤɢ ɦɨɠɧɨ ɫɮɨɪɦɢɪɨɜɚɬɶ ɩɪɨɝɪɚɦɦɧɵɣ ɤɨɞ ɜ ɮɨɪɦɚɬɟ MIDL ɢɥɢ CORBA.
3.3. ɂɫɩɨɥɶɡɨɜɚɧɢɟ ɛɢɛɥɢɨɬɟɤɢ ɬɢɩɨɜ ɩɪɢ ɪɚɡɪɚɛɨɬɤɟ ɤɨɦɩɨɧɟɧɬɨɜ Ʉɚɤ ɭɠɟ ɛɵɥɨ ɫɤɚɡɚɧɨ, ɞɥɹ ɩɨɞɤɥɸɱɟɧɢɹ ɛɢɛɥɢɨɬɟɤɢ ɬɢɩɨɜ ɤ ɫɨɡɞɚɜɚɟɦɨɦɭ COM-ɫɟɪɜɟɪɭ ɧɭɠɧɨ ɜ ɦɚɫɬɟɪɟ COM Object Wizard (ɪɢɫ. 2.2) ɜɵɛɪɚɬɶ ɮɥɚɠɨɤ Include Type Library. Ɍɚɤɠɟ ɦɨɠɧɨ ɩɨɞɤɥɸɱɢɬɶ ɛɢɛɥɢɨɬɟɤɭ ɬɢɩɨɜ ɜɪɭɱɧɭɸ, ɞɨɛɚɜɢɜ ɟɟ ɜ ɩɪɨɟɤɬ ɜ ɜɢɞɟ ɪɟɫɭɪɫɚ: {$R MyTypeLib.tlb} ɉɪɢɦɟɱɚɧɢɟ 3.1. ɂɧɨɝɞɚ ɧɟɨɛɯɨɞɢɦɨ, ɱɬɨɛɵ ɧɟɫɤɨɥɶɤɨ COMɫɟɪɜɟɪɨɜ ɢɦɟɥɢ ɨɞɢɧɚɤɨɜɵɣ ɢɧɬɟɪɮɟɣɫ ɢ ɛɵɥɢ ɭɫɬɚɧɨɜɥɟɧɵ ɧɚ ɨɞɢɧ ɤɨɦɩɶɸɬɟɪ ɨɞɧɨɜɪɟɦɟɧɧɨ. Ⱦɥɹ ɷɬɢɯ ɰɟɥɟɣ ɛɢɛɥɢɨɬɟɤɚ ɬɢɩɨɜ ɪɟɝɢɫɬɪɢɪɭɟɬɫɹ ɜ ɫɢɫɬɟɦɟ ɨɬɞɟɥɶɧɨ ɨɬ COM-ɫɟɪɜɟɪɨɜ. Ʉɨɝɞɚ ɢɫɩɨɥɶɡɭɟɬɫɹ ɛɢɛɥɢɨɬɟɤɚ ɬɢɩɨɜ, ɫɨɡɞɚɜɚɟɦɵɣ ɤɨɦɩɨɧɟɧɬ ɧɚɫɥɟɞɭɟɬɫɹ ɧɟ ɨɬ TComObject, ɚ ɨɬ TTypedComObject. ɂ ɜɦɟɫɬɨ ɝɟɧɟɪɚɬɨɪɚ ɤɨɦɩɨɧɟɧɬɨɜ TComObjectFactory ɢɫɩɨɥɶɡɭɟɬɫɹ TTypedComObjectFactory. 35
TTypedComObject ɢ TTypedComObjectFactory ɩɪɟɞɧɚɡɧɚɱɟɧɵ ɞɥɹ ɪɚɛɨɬɵ ɫ ɛɢɛɥɢɨɬɟɤɨɣ ɬɢɩɨɜ ɢ ɚɧɚɥɨɝɢɱɧɵ ɤɥɚɫɫɚɦ TComObject ɢ TɋomObjectFactory. Ⱦɥɹ ɪɟɚɥɢɡɚɰɢɢ COM-ɫɟɪɜɟɪɚ ɜ ɫɪɟɞɟ Delphi ɪɟɞɚɤɬɨɪ Type Library Editor ɝɟɧɟɪɢɪɭɟɬ ɩɪɨɝɪɚɦɦɧɵɣ ɦɨɞɭɥɶ ɧɚ ɹɡɵɤɟ Object Pascal, ɧɟɩɨɫɪɟɞɫɬɜɟɧɧɨ ɫɜɹɡɚɧɧɵɣ ɫ ɛɢɛɥɢɨɬɟɤɨɣ ɬɢɩɨɜ. ȿɫɥɢ ɜ ɪɟɞɚɤɬɨɪɟ Type Library Editor ɧɚɠɚɬɶ ɧɚ ɤɧɨɩɤɭ Refresh Implementation, ɬɨ ɞɚɧɧɵɟ ɷɬɨɝɨ ɦɨɞɭɥɹ ɨɛɧɨɜɹɬɫɹ ɫɨɨɬɜɟɬɫɬɜɟɧɧɨ ɞɚɧɧɵɦ ɜ ɛɢɛɥɢɨɬɟɤɟ (tlb).
3.4. ɉɪɢɦɟɪ ɪɚɡɪɚɛɨɬɤɢ ɜɫɬɪɨɟɧɧɨɝɨ COM-ɫɟɪɜɟɪɚ ɫ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ ɛɢɛɥɢɨɬɟɤɢ ɬɢɩɨɜ ȼɨ ɜɬɨɪɨɣ ɝɥɚɜɟ ɛɵɥ ɪɚɡɨɛɪɚɧ ɩɪɢɦɟɪ ɪɚɡɪɚɛɨɬɤɢ COM-ɫɟɪɜɟɪɚ. Ɋɟɚɥɢɡɭɟɦ ɷɬɨɬ ɩɪɢɦɟɪ ɫ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ ɛɢɛɥɢɨɬɟɤɢ ɬɢɩɨɜ. 1. ɋɨɡɞɚɞɢɦ ɜɫɬɪɨɟɧɧɵɣ COM-ɫɟɪɜɟɪ, ɜɵɛɪɚɜ ɜ ɞɢɚɥɨɝɨɜɨɦ ɨɤɧɟ New Items (ɪɢɫ. 2.1) ɷɥɟɦɟɧɬ ActiveX Library, ɢ ɫɨɯɪɚɧɢɦ ɟɝɨ ɩɨɞ ɢɦɟɧɟɦ Convert.dpr. 2. ɋɨɡɞɚɞɢɦ ɧɨɜɵɣ Ʉɨɦɩɨɧɟɧɬ, ɜɵɛɪɚɜ ɷɥɟɦɟɧɬ COM Object. ȼ Ɇɚɫɬɟɪɟ COM Object Wizard (ɪɢɫ. 2.2) ɩɨɫɬɚɜɢɦ ɮɥɚɠɨɤ Include Type Library ɢ ɭɛɟɪɟɦ Mark interface Oleautomation (ɬ. ɤ. ɷɬɨ ɛɭɞɟɬ ɪɚɫɫɦɨɬɪɟɧɨ ɜ ɝɥ. V). ȼ ɩɨɥɟ Class Name ɧɚɩɢɲɟɦ ObjConvert. 3. Ʉɨɝɞɚ ɛɭɞɟɬ ɧɚɠɚɬɚ ɤɧɨɩɤɚ ɈɄ, ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɨɬɤɪɨɟɬɫɹ ɨɤɧɨ ɪɟɞɚɤɬɨɪɚ Type Library Editor. ȼ ɩɨɥɟ ɫɩɢɫɤɚ ɨɛɴɟɤɬɨɜ ɛɭɞɭɬ ɞɜɚ ɷɥɟɦɟɧɬɚ: ObjConvert ɢ IObjConvert. ȿɫɥɢ ɢɧɬɟɪɮɟɣɫɚ IObjConvert ɧɟɬ, ɬɨ ɫɨɡɞɚɣɬɟ ɟɝɨ ɫ ɩɨɦɨɳɶɸ ɩɚɧɟɥɢ ɢɧɫɬɪɭɦɟɧɬɨɜ ɪɟɞɚɤɬɨɪɚ. 4. ɍ ɢɧɬɟɪɮɟɣɫɚ IObjConvert ɫɨɡɞɚɞɢɦ ɧɨɜɵɣ ɦɟɬɨɞ ɢ ɧɚɡɨɜɟɦ ɟɝɨ Bin. ɇɚ ɡɚɤɥɚɞɤɟ Parameters ɜ ɫɬɪɨɤɟ Return Type ɭɫɬɚɧɨɜɢɦ ɜɨɡɜɪɚɳɚɟɦɵɣ ɬɢɩ WordBool. ȼ ɬɚɛɥɢɰɟ ɞɨɛɚɜɢɦ ɧɨɜɵɣ ɩɚɪɚɦɟɬɪ, ɧɚɠɚɜ ɧɚ ɤɧɨɩɤɭ Add, ɢ ɜ ɩɨɥɟ Name ɭɤɚɠɟɦ ɢɦɹ N, ɚ ɜ ɩɨɥɟ Type – Word. Ⱥɧɚɥɨɝɢɱɧɨ ɞɨɛɚɜɢɦ ɩɚɪɚɦɟɬɪ Str, ɧɚɠɚɜ ɧɚ ɤɧɨɩɤɭ Add ɢ ɜɵɛɪɚɜ ɜ ɩɨɥɟ Modifier ɡɧɚɱɟɧɢɟ out ɢ Retval, ɜ ɩɨɥɟ Name – Str, ɚ ɜ ɩɨɥɟ Type – WideString. 5. Ⱥɧɚɥɨɝɢɱɧɨ ɩɭɧɤɬɭ 5 ɡɚɞɚɸɬɫɹ ɦɟɬɨɞɵ Oct ɢ Hex. 6. ɇɚɠɚɜ ɧɚ ɤɧɨɩɤɭ Refresh Implementation, ɨɛɧɨɜɢɦ ɪɟɚɥɢɡɚɰɢɸ ɤɨɞɚ. 7. Ʉɨɝɞɚ ɜɟɪɧɟɦɫɹ ɜ ɪɟɞɚɤɬɨɪ Delphi, ɦɨɞɭɥɶ ObjConvertUnit ɛɭɞɟɬ ɫɨɞɟɪɠɚɬɶ ɫɥɟɞɭɸɳɢɣ ɬɟɤɫɬ: unit ObjConvertUnit; interface uses Windows, ActiveX, ComObj, Convert_TLB;
36
type TObjConvert = class(TTypedComObject, IConvert) protected function Bin(N: Word; var Str: WideString): WordBool; stdcall; function Oct(N: Word; var Str: WideString): WordBool; stdcall; function Hex(N: Word; var Str: WideString): WordBool; stdcall; {Declare IObjConvert methods here} end; implementation
IV. Ɋɚɡɪɚɛɨɬɤɚ ɩɪɢɥɨɠɟɧɢɣ, ɢɫɩɨɥɶɡɭɸɳɢɯ COM-ɫɟɪɜɟɪɵ 4.1. ɉɨɥɭɱɟɧɢɟ ɫɫɵɥɤɢ ɧɚ ɢɧɬɟɪɮɟɣɫ ɤɨɦɩɨɧɟɧɬɚ. Ɏɭɧɤɰɢɢ CreateComObject ɢ ProgIdToClassId ɉɪɢ ɪɚɛɨɬɟ ɫ ɤɨɦɩɨɧɟɧɬɨɦ COM ɩɪɢɥɨɠɟɧɢɟ ɞɨɥɠɧɨ ɩɪɹɦɨ ɢɥɢ ɤɨɫɜɟɧɧɨ ɩɨɥɭɱɢɬɶ ɫɫɵɥɤɭ ɧɚ ɟɝɨ ɢɧɬɟɪɮɟɣɫ. Ⱦɥɹ ɷɬɢɯ ɰɟɥɟɣ ɜ Delphi ɨɩɪɟɞɟɥɟɧɚ ɮɭɧɤɰɢɹ CreateComObject, ɤɨɬɨɪɚɹ ɜ ɤɚɱɟɫɬɜɟ ɩɚɪɚɦɟɬɪɚ ɩɪɢɧɢɦɚɟɬ CLSID ɡɚɩɪɚɲɢɜɚɟɦɨɝɨ ɤɨɦɩɨɧɟɧɬɚ, ɢ, ɟɫɥɢ ɤɨɦɩɨɧɟɧɬ ɡɚɪɟɝɢɫɬɪɢɪɨɜɚɧ ɜ ɫɢɫɬɟɦɟ, ɜɨɡɜɪɚɳɚɟɬ ɢɧɬɟɪɮɟɣɫ IUnknown, ɱɟɪɟɡ ɤɨɬɨɪɵɣ ɦɨɠɧɨ ɩɨɥɭɱɢɬɶ ɨɫɬɚɥɶɧɵɟ ɢɧɬɟɪɮɟɣɫɵ: function CreateComObject(const ClassID: TGUID): IUnknown;
uses ComServ; function TObjConvert.Bin (N: Word; var Str: WideString): WordBool; begin
Ⱦɨɫɬɭɩ ɤ ɢɧɬɟɪɮɟɣɫɚɦ ɨɛɴɟɤɬɨɜ ɚɜɬɨɦɚɬɢɡɚɰɢɢ (ɫɦ. ɝɥ. V) ɦɨɠɧɨ ɩɨɥɭɱɢɬɶ ɬɚɤɠɟ ɢ ɫ ɩɨɦɨɳɶɸ ɮɭɧɤɰɢɢ:
end;
ȼ ɤɚɱɟɫɬɜɟ ɩɚɪɚɦɟɬɪɚ ɜ ɞɚɧɧɭɸ ɮɭɧɤɰɢɸ ɩɟɪɟɞɚɟɬɫɹ ɞɪɭɠɟɫɬɜɟɧɧɨɟ ɢɦɹ, ɫɨɫɬɨɹɳɟɟ ɢɡ ɢɦɟɧɢ COM-ɫɟɪɜɟɪɚ ɢ ɢɦɟɧɢ ɤɨɦɩɨɧɟɧɬɚ, ɤɨɬɨɪɵɟ ɪɚɡɞɟɥɟɧɵ ɬɨɱɤɨɣ:
function TObjConvert.Oct(N: Word; var Str: WideString): WordBool; begin
function CreateOleObject(const ClassName: string): IDispatch;
‘<ɢɦɹ COM-ɢɧɬɟɪɮɟɣɫɚ>.<ɢɦɹ ɤɨɦɩɨɧɟɧɬɚ>’ CLSID ɤɨɦɩɨɧɟɧɬɚ ɦɨɠɧɨ ɩɨɥɭɱɢɬɶ ɫ ɩɨɦɨɳɶɸ ɮɭɧɤɰɢɢ ProgIdToClassId:
end; function TObjConvert.Hex(N: Word; var Str: WideString): WordBool; begin end; initialization TTypedComObjectFactory.Create(ComServer, TObjConvert, Class_ObjConvert, ciMultiInstance, tmSingle); end. ɗɬɨ ɲɚɛɥɨɧ ɧɚɲɟɝɨ COM-ɤɥɚɫɫɚ, ɩɨɞɝɨɬɨɜɥɟɧɧɵɣ ɫɪɟɞɨɣ Delphi, ɤɨɬɨɪɵɣ ɧɟɨɛɯɨɞɢɦɨ ɞɨɪɚɛɨɬɚɬɶ. 8. ȼ ɦɨɞɭɥɟ ObjConvertUnit ɪɟɚɥɢɡɭɟɦ ɮɭɧɤɰɢɢ Bin, Oct, Hex, ɤɚɤ ɜ ɱɟɬɜɟɪɬɨɦ ɩɭɧɤɬɟ ɜɬɨɪɨɣ ɱɚɫɬɢ. 9. ɉɪɢɥɨɠɟɧɢɟ ɦɨɠɧɨ ɨɬɤɨɦɩɢɥɢɪɨɜɚɬɶ, ɧɚɠɚɜ ɫɨɱɟɬɚɧɢɟ ɤɥɚɜɢɲ Ctrl+F9. ɋɨɡɞɚɧɧɵɣ ɩɪɢ ɷɬɨɦ COM-ɫɟɪɜɟɪ ɝɨɬɨɜ ɤ ɢɫɩɨɥɶɡɨɜɚɧɢɸ. 37
function ProgIDToClassID(const ProgID: string): TGUID; ȼ ɤɚɱɟɫɬɜɟ ɩɚɪɚɦɟɬɪɚ ɜ ɮɭɧɤɰɢɸ ɩɨɞɚɟɬɫɹ ɫɬɪɨɤɚ ɫ ɞɪɭɠɟɫɬɜɟɧɧɵɦ ɢɦɟɧɟɦ ɤɨɦɩɨɧɟɧɬɚ. Ɋɟɡɭɥɶɬɚɬɨɦ ɹɜɥɹɟɬɫɹ CLSID ɤɨɦɩɨɧɟɧɬɚ, ɯɪɚɧɹɳɟɝɨɫɹ ɜ COM-ɫɟɪɜɟɪɟ. ɇɚɩɪɢɦɟɪ, ɟɫɥɢ ɜɵɡɜɚɬɶ ɞɚɧɧɭɸ ɮɭɧɤɰɢɸ ɫ ɩɚɪɚɦɟɬɪɨɦ ‘Word.Application’, ɬɨ ɮɭɧɤɰɢɹ ɜɟɪɧɟɬ ɫɥɟɞɭɸɳɢɣ GUID (ɢɞɟɧɬɢɮɢɤɚɬɨɪ ɩɪɢɥɨɠɟɧɢɹ Microsoft Word): ‘{000209FF-0000-0000-C000000000000046}’. Ⱦɥɹ ɬɨɝɨ, ɱɬɨɛɵ ɩɨɥɭɱɢɬɶ ɭɤɚɡɚɬɟɥɶ ɧɚ ɭɠɟ ɡɚɩɭɳɟɧɧɵɣ ɜ ɫɢɫɬɟɦɟ ɨɛɴɟɤɬ ɚɜɬɨɦɚɬɢɡɚɰɢɢ, ɢɫɩɨɥɶɡɭɟɬɫɹ ɮɭɧɤɰɢɹ function GetActiveOleObject(const ClassName: string): IDispatch; Ʉɚɤ ɢ ɜ ɩɪɨɰɟɞɭɪɟ CreateOleObject, ɜ ɮɭɧɤɰɢɸ ɩɟɪɟɞɚɟɬɫɹ ɞɪɭɠɟɫɬɜɟɧɧɨɟ ɢɦɹ ɨɛɴɟɤɬɚ. Ɏɭɧɤɰɢɹ ɩɪɨɫɦɚɬɪɢɜɚɟɬ ɬɚɛɥɢɰɭ ɜɵɩɨɥɧɹɟɦɵɯ ɨɛɴɟɤɬɨɜ Windows (Running Table Objects) ɜ ɩɨɢɫɤɚɯ ɡɚɩɪɚɲɢɜɚɟɦɨɝɨ ɫɟɪɜɟɪɚ. ȿɫɥɢ ɬɚɤɨɣ ɛɭɞɟɬ ɧɚɣɞɟɧ, ɬɨ ɮɭɧɤɰɢɹ ɜɟɪɧɟɬ ɫɫɵɥɤɭ ɧɚ ɢɧɬɟɪɮɟɣɫ IDispatch ɷɬɨɝɨ ɫɟɪɜɟɪɚ. 38
4.2. ɗɤɫɩɨɪɬ ɨɩɢɫɚɧɢɹ ɢɧɬɟɪɮɟɣɫɨɜ ɢɡ ɛɢɛɥɢɨɬɟɤɢ ɬɢɩɨɜ COM-ɫɟɪɜɟɪɚ ȼ ɬɪɟɬɶɟɣ ɝɥɚɜɟ ɪɚɡɨɛɪɚɧɨ ɩɪɢɦɟɧɟɧɢɟ ɛɢɛɥɢɨɬɟɤɢ ɬɢɩɨɜ ɜ COMɫɟɪɜɟɪɚɯ. Ȼɢɛɥɢɨɬɟɤɚ ɬɢɩɨɜ ɯɪɚɧɢɬɫɹ ɜ COM-ɫɟɪɜɟɪɟ ɜ ɜɢɞɟ ɪɟɫɭɪɫɚ, ɢ ɞɨɫɬɭɩ ɤ ɟɟ ɞɚɧɧɵɦ ɦɨɠɧɨ ɩɨɥɭɱɢɬɶ ɫ ɩɨɦɨɳɶɸ ɫɬɚɧɞɚɪɬɧɵɯ ɫɪɟɞɫɬɜ WinAPI (ɩɨɞɪɨɛɧɟɟ ɫɦ. [7 ɝɥ. 3]). Ɍɚɤɠɟ ɜ Delphi ɩɪɟɞɭɫɦɨɬɪɟɧɨ ɞɪɭɝɨɟ ɫɬɚɧɞɚɪɬɧɨɟ ɫɪɟɞɫɬɜɨ ɞɥɹ ɝɟɧɟɪɚɰɢɢ ɩɪɨɝɪɚɦɦɧɨɝɨ ɤɨɞɚ ɧɚ ɹɡɵɤɟ Object Pascal, ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɝɨ ɨɩɢɫɚɧɢɸ ɛɢɛɥɢɨɬɟɤɢ ɬɢɩɨɜ. Ⱦɥɹ ɷɬɨɝɨ ɧɭɠɧɨ ɨɬɤɪɵɬɶ ɞɢɚɥɨɝɨɜɨɟ ɨɤɧɨ Import Type Library (ɪɢɫ. 4.1), ɜɵɛɪɚɜ ɜ ɝɥɚɜɧɨɦ ɦɟɧɸ ɷɥɟɦɟɧɬ Project/Import Type Library….
ȼɜɟɪɯɭ ɷɬɨɝɨ ɞɢɚɥɨɝɨɜɨɝɨ ɨɤɧɚ ɧɚɯɨɞɢɬɫɹ ɫɩɢɫɨɤ ɫɭɳɟɫɬɜɭɸɳɢɯ COM-ɫɟɪɜɟɪɨɜ. Ⱦɨɛɚɜɢɬɶ ɧɨɜɵɣ COM-ɫɟɪɜɟɪ ɜ ɷɬɨɬ ɫɩɢɫɨɤ ɦɨɠɧɨ ɫ ɩɨɦɨɳɶɸ ɤɧɨɩɤɢ Add…, ɭɞɚɥɢɬɶ – Remove. Ⱦɚɧɧɵɟ ɤɧɨɩɤɢ ɚɧɚɥɨɝɢɱɧɵ ɤɨɦɚɧɞɚɦ ɪɟɝɢɫɬɪɚɰɢɢ ɢ ɭɞɚɥɟɧɢɹ COM-ɫɟɪɜɟɪɨɜ ɜ ɫɢɫɬɟɦɟ. ɇɢɠɟ ɫɩɢɫɤɚ COM-ɫɟɪɜɟɪɨɜ ɧɚɯɨɞɢɬɫɹ ɫɩɢɫɨɤ ɤɨɦɩɨɧɟɧɬɨɜ ɜ ɜɵɛɪɚɧɧɨɦ ɫɟɪɜɟɪɟ, ɚ ɬɚɤɠɟ ɫɬɪɨɤɚ ɫ ɜɵɛɨɪɨɦ ɞɢɪɟɤɬɨɪɢɢ, ɜ ɤɨɬɨɪɨɣ ɛɭɞɟɬ ɫɨɯɪɚɧɟɧ ɷɤɫɩɨɪɬɢɪɭɟɦɵɣ ɦɨɞɭɥɶ. ɑɬɨɛɵ ɩɨɥɭɱɢɬɶ ɩɪɨɝɪɚɦɦɧɵɣ ɤɨɞ, ɚɧɚɥɨɝɢɱɧɵɣ ɨɩɢɫɚɧɢɸ ɛɢɛɥɢɨɬɟɤɢ ɬɢɩɨɜ, ɧɭɠɧɨ ɧɚɠɚɬɶ ɧɚ ɤɧɨɩɤɭ Create Unit. Ʉɧɨɩɤɚ Install… ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɝɟɧɟɪɢɪɭɟɬ ɩɪɨɝɪɚɦɦɧɵɣ ɤɨɞ ɤɨɦɩɨɧɟɧɬɚ Delphi, ɜ ɤɨɬɨɪɨɦ ɪɟɚɥɢɡɨɜɚɧɵ ɜɫɟ ɫɜɨɣɫɬɜɚ ɢ ɦɟɬɨɞɵ ɜɵɛɪɚɧɧɨɝɨ ɜ ɫɩɢɫɤɟ ɤɨɦɩɨɧɟɧɬɚ. ɉɨɥɭɱɟɧɧɵɣ ɤɨɦɩɨɧɟɧɬ ɭɫɬɚɧɚɜɥɢɜɚɟɬɫɹ ɧɚ ɡɚɤɥɚɞɤɟ Server. Ɋɚɛɨɬɚ ɫ ɬɚɤɢɦɢ ɤɨɦɩɨɧɟɧɬɚɦɢ ɬɪɢɜɢɚɥɶɧɚ ɢ ɚɧɚɥɨɝɢɱɧɚ, ɤɚɤ ɢ ɫɨ ɜɫɟɦɢ ɫɬɚɧɞɚɪɬɧɵɦɢ ɤɨɦɩɨɧɟɧɬɚɦɢ Delphi, ɩɨɷɬɨɦɭ ɞɚɧɧɵɣ ɦɟɬɨɞ ɪɚɫɫɦɚɬɪɢɜɚɬɶɫɹ ɧɟ ɛɭɞɟɬ.
4.3. Ɉɫɨɛɟɧɧɨɫɬɢ ɬɟɯɧɢɤɢ ɢɫɩɨɥɶɡɨɜɚɧɢɹ ɤɨɦɩɨɧɟɧɬɨɜ ɜ Delphi Delphi ɹɜɥɹɟɬɫɹ ɬɚɤɨɣ ɫɪɟɞɨɣ ɪɚɡɪɚɛɨɬɤɢ ɩɪɨɝɪɚɦɦ, ɜ ɤɨɬɨɪɨɣ ɨɱɟɧɶ ɦɧɨɝɨɟ ɚɜɬɨɦɚɬɢɡɢɪɨɜɚɧɨ ɢ ɭɩɪɨɳɟɧɨ. ɇɟ ɢɫɤɥɸɱɟɧɢɟɦ ɹɜɥɹɟɬɫɹ ɢ COMɬɟɯɧɨɥɨɝɢɹ. ȼ Delphi ɪɚɡɪɚɛɨɬɚɧɵ ɤɥɚɫɫɵ, ɤɨɬɨɪɵɟ ɫɭɳɟɫɬɜɟɧɧɨ ɨɛɥɟɝɱɚɸɬ ɪɟɚɥɢɡɚɰɢɸ ɪɚɡɥɢɱɧɵɯ ɡɚɞɚɱ ɬɟɯɧɨɥɨɝɢɢ COM: Ȼɚɡɨɜɵɣ ɤɥɚɫɫ ɤɨɦɩɨɧɟɧɬɚ
Ƚɟɧɟɪɚɬɨɪ ɤɨɦɩɨɧɟɧɬɨɜ
ɂɫɩɨɥɶɡɭɟɬɫɹ ɞɥɹ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ …
TAutoObject
TAutoObjectFactory
ɤɨɦɩɨɧɟɧɬɨɜ ɚɜɬɨɦɚɬɢɡɚɰɢɢ (ɝɥ. V)
TComObject
TComObjectFactory
ɩɪɨɫɬɵɯ ɤɨɦɩɨɧɟɧɬɨɜ
TTypedComObject
TTypedComObjectFactory
ɤɨɦɩɨɧɟɧɬɨɜ ɫ ɝɟɧɟɪɚɰɢɟɣ ɛɢɛɥɢɨɬɟɤɢ ɬɢɩɨɜ
ȿɳɟ ɨɞɧɨɣ ɜɚɠɧɨɣ ɨɫɨɛɟɧɧɨɫɬɶɸ ɪɚɡɪɚɛɨɬɤɢ COM-ɩɪɢɥɨɠɟɧɢɣ ɹɜɥɹɟɬɫɹ ɬɨ, ɱɬɨ ɧɟɬ ɧɟɨɛɯɨɞɢɦɨɫɬɢ ɩɨɞɫɱɢɬɵɜɚɬɶ ɫɫɵɥɤɢ, ɬɨ ɟɫɬɶ ɢɫɩɨɥɶɡɨɜɚɬɶ ɦɟɬɨɞɵ _AddRef ɢ _Release, ɬɚɤ ɤɚɤ ɫɢɫɬɟɦɚ ɫɚɦɚ ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɭɜɟɥɢɱɢɬ ɫɱɟɬɱɢɤ, ɤɨɝɞɚ ɫɫɵɥɤɚ ɛɭɞɟɬ ɩɪɢɫɜɨɟɧɚ, ɢ ɭɦɟɧɶɲɢɬ, ɤɨɝɞɚ ɩɟɪɟɦɟɧɧɨɣ ɛɭɞɟɬ ɩɪɢɫɜɨɟɧɨ ɡɧɚɱɟɧɢɟ nil. Ʉɨɝɞɚ ɫɱɟɬɱɢɤ ɫɫɵɥɨɤ ɛɭɞɟɬ ɪɚɜɟɧ ɧɭɥɸ, ɨɛɴɟɤɬ ɛɭɞɟɬ ɭɧɢɱɬɨɠɟɧ: Ɋɢɫ. 4.1. Ⱦɢɚɥɨɝɨɜɨɟ ɨɤɧɨ Import Type Library 39
V:=CreateComObject(GUID); //ɫɱɟɬɱɢɤ ɫɫɵɥɨɤ ɭɜɟɥɢɱɢɥɫɹ ɢ ɪɚɜɟɧ 1 40
W:=V; //ɫɱɟɬɱɢɤ ɫɫɵɥɨɤ ɭɜɟɥɢɱɢɥɫɹ ɢ ɪɚɜɟɧ 2
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Convert_TLB;
<……………………….> V:=nil; //ɫɱɟɬɱɢɤ ɫɫɵɥɨɤ ɭɦɟɧɶɲɢɥɫɹ ɢ ɪɚɜɟɧ 1 W:=nil; //ɫɱɟɬɱɢɤ ɫɫɵɥɨɤ ɭɦɟɧɶɲɢɥɫɹ, ɢ ɨɛɴɟɤɬ ɭɞɚɥɢɥɫɹ
type TForm1 = class(TForm) Edit1: TEdit; Bin: TButton; Oct: TButton; Hex: TButton; procedure FormCreate(Sender: TObject); procedure BinClick(Sender: TObject); procedure OctClick(Sender: TObject); procedure HexClick(Sender: TObject); private { Private declarations } Convert: IConvert; public { Public declarations } end;
Ɂɚɦɟɱɚɧɢɟ 4.1. ȼɫɟɝɞɚ ɩɪɢɫɜɚɢɜɚɣɬɟ ɫɫɵɥɤɚɦ ɧɚ ɤɨɦɩɨɧɟɧɬ ɡɧɚɱɟɧɢɟ nil, ɱɬɨɛɵ ɭɦɟɧɶɲɢɬɶ ɫɱɟɬɱɢɤ ɫɫɵɥɨɤ ɢ ɫɜɨɟɜɪɟɦɟɧɧɨ ɜɵɝɪɭɡɢɬɶ ɤɨɦɩɨɧɟɧɬ ɢɡ ɩɚɦɹɬɢ. Ɍɚɤ ɠɟ, ɤɚɤ ɢ ɜ ɤɥɚɫɫɚɯ, ɦɨɠɧɨ ɩɨɥɭɱɢɬɶ ɢɧɬɟɪɮɟɣɫ ɨɛɴɟɤɬɚ ɫ ɩɨɦɨɳɶɸ ɨɩɟɪɚɬɨɪɚ as: V:=CreateComObject(GUID); W:=V as ISomeInterface; //ɚɧɚɥɨɝɢɱɧɨ V.QueryInterface(IID,W) ȼ ɩɚɤɟɬɟ Delphi ɩɨɫɬɚɜɥɹɟɬɫɹ ɫɩɟɰɢɚɥɶɧɚɹ ɭɬɢɥɢɬɚ ɞɥɹ ɪɟɝɢɫɬɪɚɰɢɢ COM-ɫɟɪɜɟɪɨɜ ɢ ɛɢɛɥɢɨɬɟɤɢ ɬɢɩɨɜ tregsvr.exe. Ɉɛɵɱɧɨ ɷɬɚ ɭɬɢɥɢɬɚ ɧɚɯɨɞɢɬɫɹ ɜ ɩɚɩɤɟ Delphi\Bin.
4.4. ɉɪɢɦɟɪ ɪɟɚɥɢɡɚɰɢɢ COM-ɤɥɢɟɧɬɚ ɉɪɢɜɟɞɟɦ ɩɪɢɦɟɪ ɢɫɩɨɥɶɡɨɜɚɧɢɹ ɜɫɬɪɨɟɧɧɨɝɨ COM-ɫɟɪɜɟɪɚ, ɫɨɡɞɚɧɧɨɝɨ ɜ ɬɪɟɬɶɟɣ ɱɚɫɬɢ. 1. ɋɨɡɞɚɣɬɟ ɜɫɬɪɨɟɧɧɵɣ COM-ɫɟɪɜɟɪ ɢɡ ɩɪɢɦɟɪɚ ɜ ɬɪɟɬɶɟɣ ɱɚɫɬɢ. Ɂɚɪɟɝɢɫɬɪɢɪɭɣɬɟ ɟɝɨ, ɤɚɤ ɧɚɩɢɫɚɧɨ ɜ ɩɟɪɜɨɣ ɱɚɫɬɢ ɲɟɫɬɨɝɨ ɩɭɧɤɬɚ. 2. Ɉɬɤɪɨɣɬɟ ɧɨɜɵɣ ɩɪɨɟɤɬ ɜ Delphi. 3. Ɉɬɤɪɨɣɬɟ ɞɢɚɥɨɝɨɜɨɟ ɨɤɧɨ ɢɦɩɨɪɬɚ ɛɢɛɥɢɨɬɟɤɢ ɬɢɩɨɜ (ɪɢɫ. 4.1) ɢ ɧɚɣɞɢɬɟ ɛɢɛɥɢɨɬɟɤɭ ɧɚɲɟɝɨ COM-ɫɟɪɜɟɪɚ. ȿɫɥɢ CɈɆ-ɫɟɪɜɟɪ ɧɚɡɵɜɚɟɬɫɹ Convert.dll, ɬɨ ɜ ɫɩɢɫɤɟ ɛɭɞɟɬ ɫɬɪɨɤɚ Convert Library (Version 1.0). ɂɦɩɨɪɬɢɪɭɣɬɟ ɟɟ. 4. ɇɚ ɮɨɪɦɭ ɩɨɫɬɚɜɶɬɟ ɫɬɪɨɤɭ ɜɜɨɞɚ TEdit ɢ ɬɪɢ ɤɧɨɩɤɢ Bin, Oct, Hex (ɪɢɫ. 4.2).
Ɋɢɫ. 4.2. Ɏɨɪɦɚ ɩɪɢɦɟɪɚ 5. ȼ ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɦ ɮɨɪɦɟ ɦɨɞɭɥɟ ɧɚɛɟɪɢɬɟ ɫɥɟɞɭɸɳɢɣ ɬɟɤɫɬ:
var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject); begin //ɫɨɡɞɚɟɬɫɹ ɷɤɡɟɦɩɥɹɪ Ʉɨɦɩɨɧɟɧɬɚ Convert:=nil; Convert:=CoObjConvert.Create; if not Assigned(Convert) then begin ShowMessage('ɇɟ ɜɨɡɦɨɠɧɨ ɫɨɡɞɚɬɶ ɤɨɦɩɨɧɟɧɬ'); Close; end; end; procedure TForm1.BinClick(Sender: TObject); var str: WideString;
unit Unit1;
begin if Convert.Bin(StrToInt(Edit1.Text),Str) then ShowMessage(Str)
interface 41
42
else raise Exception.Create('Ɉɲɢɛɤɚ ɜ BinClick'); end; procedure TForm1.OctClick(Sender: TObject); var str: WideString;
ɇɢɠɟ ɩɪɢɜɟɞɟɧ ɩɪɢɦɟɪ ɩɪɨɝɪɚɦɦɧɨɝɨ ɤɨɞɚ, ɜ ɤɨɬɨɪɨɦ ɨɪɝɚɧɢɡɭɟɬɫɹ ɩɨɞɤɥɸɱɟɧɢɟ ɤ Microsoft Word ɢ ɟɦɭ ɩɟɪɟɞɚɟɬɫɹ ɤɨɦɚɧɞɚ ɫɮɨɪɦɢɪɨɜɚɬɶ ɧɨɜɵɣ ɮɚɣɥ: procedure TForm1.Button1.Click(sender: TObject); var V: Variant;
begin if Convert.Hex(StrToInt(Edit1.Text),Str) then ShowMessage(Str) else raise Exception.Create('Ɉɲɢɛɤɚ ɜ OctClick'); end; procedure TForm1.HexClick(Sender: TObject); var str: WideString begin if Convert.Hex(StrToInt(Edit1.Text),Str) then ShowMessage(Str) else raise Exception.Create('Ɉɲɢɛɤɚ ɜ HexClick'); end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin //ɪɚɡɪɵɜ ɫ ɫɫɵɥɤɨɣ. ɋɱɟɬɱɢɤ ɭɦɟɧɶɲɚɟɬɫɹ ɧɚ ɟɞɢɧɢɰɭ Convert:=nil; end;
begin V:=CreateOLEObject(‘Word.Basic’); V.AppShow; V.FileNew; V.Insert(‘Ⱥɜɬɨɦɚɬɢɡɚɰɢɹ ɷɬɨ ɩɪɨɫɬɨ!’); V:=NULL; end; ɉɪɢɦɟɱɚɧɢɟ 5.1. ɉɪɢ ɨɛɧɭɥɟɧɢɢ ɬɢɩɢɡɢɪɨɜɚɧɧɨɣ ɫɫɵɥɤɢ ɧɚ ɢɧɬɟɪɮɟɣɫ ɢɫɩɨɥɶɡɭɟɬɫɹ nil, ɚ ɩɪɢ ɨɛɧɭɥɟɧɢɢ ɫɫɵɥɤɢ ɬɢɩɚ Variant – NULL. ɉɪɢ ɩɨɡɞɧɟɦ ɫɜɹɡɵɜɚɧɢɢ ɤɨɦɩɢɥɹɬɨɪ Delphi ɩɪɢɧɢɦɚɟɬ ɜɫɟ ɛɟɡ ɜɨɡɪɚɠɟɧɢɣ ɢ ɧɟ ɜɵɩɨɥɧɹɟɬ ɤɨɧɬɪɨɥɶ ɤɨɪɪɟɤɬɧɨɫɬɢ ɬɢɩɨɜ, ɧɨ ɩɪɢ ɜɵɡɨɜɟ ɧɟɫɭɳɟɫɬɜɭɸɳɢɯ ɦɟɬɨɞɨɜ ɜɨ ɜɪɟɦɹ ɜɵɩɨɥɧɟɧɢɹ ɩɪɨɝɪɚɦɦɵ ɩɨɹɜɥɹɟɬɫɹ ɫɨɨɛɳɟɧɢɟ ɨɛ ɨɲɢɛɤɟ (ɪɢɫ. 5.1).
end.
V. Ⱥɜɬɨɦɚɬɢɡɚɰɢɹ Ⱥɜɬɨɦɚɬɢɡɚɰɢɹ (Automation) ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ ɦɟɯɚɧɢɡɦ ɚɜɬɨɦɚɬɢɱɟɫɤɨɝɨ ɭɩɪɚɜɥɟɧɢɹ ɨɞɧɢɦ ɩɪɢɥɨɠɟɧɢɟɦ ɢɡ ɞɪɭɝɨɝɨ. ɋɭɳɟɫɬɜɭɸɬ ɤɚɤ ɜɫɬɪɨɟɧɧɵɟ ɫɟɪɜɟɪɵ ɚɜɬɨɦɚɬɢɡɚɰɢɢ, ɬɚɤ ɢ ɜɧɟɲɧɢɟ. ȼ ɫɪɟɞɟ Delphi ɥɟɝɤɨ ɫɨɡɞɚɜɚɬɶ ɢ ɢɫɩɨɥɶɡɨɜɚɬɶ ɫɟɪɜɟɪɵ ɚɜɬɨɦɚɬɢɡɚɰɢɢ, ɢɦɟɹ ɜɟɫɶɦɚ ɫɥɚɛɨɟ ɩɪɟɞɫɬɚɜɥɟɧɢɟ ɨ ɩɪɢɧɰɢɩɚɯ COM-ɬɟɯɧɨɥɨɝɢɢ.
5.1. Ɋɚɧɧɟɟ ɢ ɩɨɡɞɧɟɟ ɫɜɹɡɵɜɚɧɢɟ. Ɍɢɩ Variant ɑɬɨɛɵ ɩɨɥɭɱɢɬɶ ɞɨɫɬɭɩ ɤ ɢɧɬɟɪɮɟɣɫɭ ɤɨɦɩɨɧɟɧɬɚ ɚɜɬɨɦɚɬɢɡɚɰɢɢ ɧɟɨɛɹɡɚɬɟɥɶɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɨɩɢɫɚɧɢɟ ɢɧɬɟɪɮɟɣɫɚ. ȼ Delphi ɟɫɬɶ ɜɨɡɦɨɠɧɨɫɬɶ ɢɫɩɨɥɶɡɨɜɚɬɶ ɢɧɬɟɪɮɟɣɫ ɛɟɡ ɟɝɨ ɨɩɢɫɚɧɢɹ. ȼ ɷɬɨɦ ɫɥɭɱɚɟ ɫɫɵɥɤɚ ɧɚ ɢɧɬɟɪɮɟɣɫ ɫɨɡɞɚɜɚɟɦɨɝɨ ɤɨɦɩɨɧɟɧɬɚ ɯɪɚɧɢɬɫɹ ɜ ɩɟɪɟɦɟɧɧɨɣ ɬɢɩɚ Variant. ȼ ɷɬɨɦ ɫɥɭɱɚɟ ɤɨɦɩɢɥɹɬɨɪ ɧɟ ɩɪɨɜɟɪɹɟɬ ɤɨɪɪɟɤɬɧɨɫɬɶ ɨɛɪɚɳɟɧɢɣ ɤ ɢɧɬɟɪɮɟɣɫɭ. Ʉɨɪɪɟɤɬɧɨɫɬɶ ɩɪɨɜɟɪɹɟɬɫɹ ɬɨɥɶɤɨ ɭɠɟ ɧɚ ɫɬɚɞɢɢ ɜɵɩɨɥɧɟɧɢɹ ɩɪɨɝɪɚɦɦɵ. Ɍɚɤɨɣ ɦɟɬɨɞ ɧɚɡɵɜɚɸɬ ɦɟɬɨɞɨɦ ɩɨɡɞɧɟɝɨ ɫɜɹɡɵɜɚɧɢɹ. 43
Ɋɢɫ. 5.1. ɋɨɨɛɳɟɧɢɟ ɨɛ ɨɲɢɛɤɟ ɩɪɢ ɨɛɪɚɳɟɧɢɢ ɤ ɧɟɫɭɳɟɫɬɜɭɸɳɟɦɭ ɦɟɬɨɞɭ ɫɟɪɜɟɪɚ ɚɜɬɨɦɚɬɢɡɚɰɢɢ Ɇɟɬɨɞ ɪɚɛɨɬɵ ɫ ɤɨɦɩɨɧɟɧɬɨɦ, ɤɨɝɞɚ ɢɫɩɨɥɶɡɭɟɬɫɹ ɨɩɢɫɚɧɢɟ ɟɝɨ ɢɧɬɟɪɮɟɣɫɚ, ɧɚɡɵɜɚɸɬ ɦɟɬɨɞɨɦ ɪɚɧɧɟɝɨ ɫɜɹɡɵɜɚɧɢɹ. ȼ ɷɬɨɦ ɫɥɭɱɚɟ ɤɨɪɪɟɤɬɧɨɫɬɶ ɪɚɛɨɬɵ ɜɵɡɨɜɚ ɩɪɨɰɟɞɭɪ ɩɪɨɜɟɪɹɟɬɫɹ ɧɚ ɫɬɚɞɢɢ ɤɨɦɩɢɥɹɰɢɢ ɩɪɨɝɪɚɦɦɵ. ɇɟɞɨɫɬɚɬɨɤ ɩɨɡɞɧɟɝɨ ɫɜɹɡɵɜɚɧɢɹ – ɡɚɦɟɞɥɟɧɧɨɟ ɨɛɪɚɳɟɧɢɟ ɤ ɤɨɦɩɨɧɟɧɬɭ, ɬɚɤ ɤɚɤ ɫɢɫɬɟɦɚ COM ɩɵɬɚɟɬɫɹ ɧɚɣɬɢ ɜɵɡɵɜɚɟɦɭɸ ɩɪɨɰɟɞɭɪɭ ɢɥɢ ɮɭɧɤɰɢɸ ɩɨ ɢɦɟɧɢ. ɇɨ ɜ ɩɪɨɬɢɜɨɜɟɫ ɧɟɞɨɫɬɚɬɤɚɦ ɫɭɳɟɫɬɜɭɸɬ ɪɹɞ ɩɪɟɢɦɭɳɟɫɬɜ ɞɚɧɧɨɝɨ ɦɟɬɨɞɚ: 1) ɢɧɨɝɞɚ ɩɪɨɝɪɚɦɦɵ ɢɥɢ ɦɚɤɪɨɫɵ ɪɚɡɪɚɛɚɬɵɜɚɸɬɫɹ ɜ ɫɪɟɞɟ, ɤɨɬɨɪɚɹ ɧɟ ɩɨɞɞɟɪɠɢɜɚɟɬ ɢɧɬɟɪɮɟɣɫɨɜ (ɧɚɩɪɢɦɟɪ, Visual Basic) ɢ ɬ. ɩ; 44
2) ɧɟ ɬɪɟɛɭɟɬɫɹ ɢɦɩɨɪɬɢɪɨɜɚɬɶ ɢ ɩɨɞɤɥɸɱɚɬɶ ɩɪɨɝɪɚɦɦɧɵɣ ɦɨɞɭɥɶ ɫ ɨɩɢɫɚɧɢɟɦ ɢɧɬɟɪɮɟɣɫɨɜ ɢ ɬɢɩɨɜ ɞɚɧɧɵɯ, ɩɪɢɦɟɧɹɟɦɵɯ ɤɨɦɩɨɧɟɧɬɨɦ; 3) ɜ ɧɟɤɨɬɨɪɵɯ ɦɟɬɨɞɚɯ ɤɨɦɩɨɧɟɧɬɨɜ ɩɚɪɚɦɟɬɪɵ ɨɩɪɟɞɟɥɟɧɵ ɩɨ ɭɦɨɥɱɚɧɢɸ, ɧɨ ɩɪɢ ɪɚɧɧɟɦ ɫɜɹɡɵɜɚɧɢɢ ɤɨɦɩɢɥɹɬɨɪ Delphi ɧɟ ɞɨɩɭɫɬɢɬ ɜɨɡɦɨɠɧɨɫɬɢ «ɨɩɭɫɬɢɬɶ» ɞɚɧɧɵɟ ɩɚɪɚɦɟɬɪɵ. ɇɚɩɪɢɦɟɪ, ɩɭɫɬɶ ɟɫɬɶ ɩɟɪɟɦɟɧɧɚɹ Word, ɜ ɤɨɬɨɪɨɣ ɯɪɚɧɢɬɫɹ ɭɤɚɡɚɬɟɥɶ ɧɚ ɤɨɦɩɨɧɟɧɬ Microsoft Word. ɑɬɨɛɵ ɫɨɡɞɚɬɶ ɧɨɜɵɣ ɞɨɤɭɦɟɧɬ ɩɪɢ ɩɨɡɞɧɟɦ ɫɜɹɡɵɜɚɧɢɢ, ɞɨɫɬɚɬɨɱɧɨ ɧɚɩɢɫɚɬɶ ɨɩɟɪɚɬɨɪ:
ɉɪɨɦɟɠɭɬɨɱɧɵɦ ɡɜɟɧɨɦ ɦɟɠɞɭ ɢɧɬɟɪɮɟɣɫɚɦɢ ɢ ɩɟɪɟɦɟɧɧɵɦɢ ɬɢɩɚ Variant ɹɜɥɹɸɬɫɹ ɢɧɬɟɪɮɟɣɫɵ ɞɢɫɩɟɬɱɟɪɢɡɚɰɢɢ (dispinterface). ɂɧɬɟɪɮɟɣɫɵ ɞɢɫɩɟɬɱɟɪɢɡɚɰɢɢ ɨɩɢɫɵɜɚɸɬɫɹ ɩɨɱɬɢ ɬɚɤ ɠɟ, ɤɚɤ ɢ ɨɛɵɱɧɵɟ ɢɧɬɟɪɮɟɣɫɵ: IMyInterfase=interface(IDispatch) [] {<ɦɟɬɨɞ ɢɧɬɟɪɮɟɣɫɚ>; safecall;} end; IMyInterfaseDisp=dispinterface [] {<ɦɟɬɨɞ ɢɧɬɟɪɮɟɣɫɚ>; dispid <ɱɢɫɥɨɜɨɣ ɢɞɟɧɬɢɮɢɤɚɬɨɪ ɦɟ-
Word.Documents.Add ɉɪɢ ɪɚɧɧɟɦ ɫɜɹɡɵɜɚɧɢɢ ɩɪɢɞɟɬɫɹ ɜɵɡɵɜɚɬɶ ɞɚɧɧɭɸ ɮɭɧɤɰɢɸ ɫ ɩɚɪɚɦɟɬɪɚɦɢ: Word.Documents.Add(Template, NewTemplate, DocumentType, Visible).
5.2. ɂɧɬɟɪɮɟɣɫɵ ɞɢɫɩɟɬɱɟɪɢɡɚɰɢɢ Ɇɟɯɚɧɢɡɦ ɩɨɡɞɧɟɝɨ ɫɜɹɡɵɜɚɧɢɹ ɪɟɚɥɢɡɭɟɬɫɹ ɜ ɫɚɦɢɯ ɤɨɦɩɨɧɟɧɬɚɯ ɚɜɬɨɦɚɬɢɡɚɰɢɢ, ɤɨɬɨɪɵɟ ɨɛɹɡɚɧɵ ɩɨɞɞɟɪɠɢɜɚɬɶ ɢɧɬɟɪɮɟɣɫ IDispatch: IDispatch = interface(IUnknown) ['{00020400-0000-0000-C000-000000000046}'] function GetTypeInfoCount(out Count: Integer): HResult; stdcall; function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; stdcall; function GetIDsOfNames(const IID: TGUID; Names: Pointer; NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall; function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall; end; ɉɪɢ ɨɛɪɚɳɟɧɢɢ ɤ ɦɟɬɨɞɭ ɢɧɬɟɪɮɟɣɫɚ ɤɨɦɩɨɧɟɧɬɚ ɜ ɦɟɬɨɞɟ ɩɨɡɞɧɟɝɨ ɫɜɹɡɵɜɚɧɢɹ ɜɵɩɨɥɧɹɸɬɫɹ ɫɥɟɞɭɸɳɢɟ ɞɟɣɫɬɜɢɹ: 1) ɢɦɹ ɜɵɡɵɜɚɟɦɨɣ ɩɪɨɰɟɞɭɪɵ ɩɟɪɟɞɚɟɬɫɹ ɜ ɦɟɬɨɞ GetIDsOfNames, ɤɨɬɨɪɚɹ ɜɨɡɜɪɚɳɚɟɬ ɱɢɫɥɨɜɨɣ ɢɞɟɧɬɢɮɢɤɚɬɨɪ, ɨɞɧɨɡɧɚɱɧɨ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɣ ɢɦɟɧɢ ɦɟɬɨɞɚ ɫɟɪɜɟɪɚ; 2) ɱɢɫɥɨɜɨɣ ɢɞɟɧɬɢɮɢɤɚɬɨɪ ɩɟɪɟɞɚɟɬɫɹ ɜ Invoke ɞɥɹ ɩɨɥɭɱɟɧɢɹ ɚɞɪɟɫɚ ɧɚ ɜɵɡɵɜɚɟɦɵɣ ɦɟɬɨɞ ɢɧɬɟɪɮɟɣɫɚ.
45
ɬɨɞɚ>;} end; IID ɢɧɬɟɪɮɟɣɫɚ IMyInterface ɢ IMyInterfaceDisp ɨɛɹɡɚɬɟɥɶɧɨ ɞɨɥɠɧɵ ɛɵɬɶ ɨɞɢɧɚɤɨɜɵɦɢ. Ɇɟɬɨɞɵ ɭ IMyInterfaceDisp ɹɜɥɹɸɬɫɹ ɨɬɨɛɪɚɠɟɧɢɟɦ ɦɟɬɨɞɨɜ IMyInterface, ɧɨ ɜɦɟɫɬɨ safecall ɢɫɩɨɥɶɡɭɟɬɫɹ dispid ɞɥɹ ɬɨɝɨ, ɱɬɨɛɵ ɡɚɞɚɬɶ ɦɟɬɨɞɭ ɫɨɛɫɬɜɟɧɧɵɣ ɱɢɫɥɨɜɨɣ ɢɞɟɧɬɢɮɢɤɚɬɨɪ. ȼɫɟ ɦɟɬɨɞɵ ɫɟɪɜɟɪɚ ɚɜɬɨɦɚɬɢɡɚɰɢɢ ɞɨɥɠɧɵ ɜɨɡɜɪɚɳɚɬɶ ɡɧɚɱɟɧɢɟ ɬɢɩɚ HResult. ȼɨɡɜɪɚɳɚɟɦɨɟ ɡɧɚɱɟɧɢɟ ɹɜɥɹɟɬɫɹ ɢɧɞɢɤɚɬɨɪɨɦ, ɧɨɪɦɚɥɶɧɨ ɥɢ ɡɚɜɟɪɲɢɥ ɦɟɬɨɞ ɡɚɩɪɨɝɪɚɦɦɢɪɨɜɚɧɧɭɸ ɜ ɧɟɦ ɨɩɟɪɚɰɢɸ, ɢɥɢ ɜɵɩɨɥɧɢɬɶ ɟɟ ɧɟ ɭɞɚɥɨɫɶ. Ⱦɪɭɝɢɟ ɞɚɧɧɵɟ, ɩɨɥɭɱɟɧɧɵɟ ɜ ɪɟɡɭɥɶɬɚɬɟ ɪɚɛɨɬɵ ɮɭɧɤɰɢɢ, ɜɨɡɜɪɚɳɚɸɬɫɹ ɱɟɪɟɡ ɩɚɪɚɦɟɬɪɵ ɫ ɦɨɞɢɮɢɤɚɬɨɪɨɦ out. Ⱦɢɪɟɤɬɢɜɚ safecall ɢɧɫɬɪɭɤɬɢɪɭɟɬ ɤɨɦɩɢɥɹɬɨɪ Delphi ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɫɨɡɞɚɬɶ ɜɨɤɪɭɝ ɜɫɟɯ ɦɟɬɨɞɨɜ ɫɜɨɟɝɨ ɪɨɞɚ ɨɛɨɥɨɱɤɭ ɜ ɜɢɞɟ ɤɨɧɫɬɪɭɤɰɢɢ try-except. Ⱦɚɧɧɵɟ ɫɨɝɥɚɲɟɧɢɹ ɜ Delphi ɨɩɪɟɞɟɥɟɧɵ ɩɨ ɭɦɨɥɱɚɧɢɸ, ɩɨɷɬɨɦɭ ɩɪɢ ɨɛɪɚɳɟɧɢɢ ɢɥɢ ɪɟɚɥɢɡɚɰɢɢ ɤɨɦɩɨɧɟɧɬɚ ɚɜɬɨɦɚɬɢɡɚɰɢɢ ɦɟɬɨɞɵ ɟɝɨ ɢɧɬɟɪɮɟɣɫɨɜ ɜɵɝɥɹɞɹɬ ɤɚɤ ɨɛɵɱɧɵɟ ɦɟɬɨɞɵ. ɇɚɩɪɢɦɟɪ: //Ɍɚɤ ɞɨɥɠɟɧ ɜɵɝɥɹɞɟɬɶ ɦɟɬɨɞ ɢɧɬ-ɫɚ, ɩɪɨɢɡɜɨɞɧɨɝɨ ɨɬ IDspatch function MyMethod(out param: <ɬɢɩ ɩɟɪɟɦɟɧɧɨɣ>): HResult; stdcall; //Ɍɚɤ ɦɟɬɨɞ ɢɧɬ-ɫɚ, ɩɪɨɢɡɜɨɞɧɨɝɨ ɨɬ IDspatch, ɪɟɚɥɢɡɨɜɚɧ ɜ Delphi function MyMethod: <ɬɢɩ ɩɟɪɟɦɟɧɧɨɣ>; ȼ ɢɧɬɟɪɮɟɣɫɚɯ, ɩɨɞɞɟɪɠɢɜɚɸɳɢɯ IDispatch, ɦɨɠɧɨ ɡɚɞɚɜɚɬɶ ɧɟ ɬɨɥɶɤɨ ɦɟɬɨɞɵ, ɧɨ ɢ ɫɜɨɣɫɬɜɚ. ɋɜɨɣɫɬɜɚ ɩɨɯɨɠɢ ɧɚ ɩɟɪɟɦɟɧɧɵɟ, ɧɨ ɞɥɹ ɜɜɨɞɚ ɢ ɜɵɜɨɞɚ ɞɚɧɧɵɯ ɜ ɫɜɨɣɫɬɜɚɯ ɢɫɩɨɥɶɡɭɸɬɫɹ ɮɭɧɤɰɢɢ, ɧɚɡɜɚɧɢɹ ɤɨɬɨɪɵɯ ɨɛɵɱɧɨ ɧɚɱɢɧɚɸɬɫɹ ɫ ɩɪɟɮɢɤɫɨɜ Get_ (ɞɥɹ ɫɱɢɬɵɜɚɧɢɹ) ɢ Set_ (ɞɥɹ ɡɚɩɢɫɢ) ɫɨɨɬɜɟɬɫɬɜɟɧɧɨ. ɋɜɨɣɫɬɜɚ ɦɨɝɭɬ ɛɵɬɶ ɞɨɫɬɭɩɧɵ ɧɟ ɬɨɥɶɤɨ ɧɚ ɱɬɟɧɢɟ ɢ ɡɚɩɢɫɶ ɨɞɧɨɜɪɟɦɟɧɧɨ, ɧɨ ɢ ɬɨɥɶɤɨ ɧɚ ɱɬɟɧɢɟ ɢɥɢ ɬɨɥɶɤɨ ɧɚ ɡɚɩɢɫɶ. ɉɪɨɰɟɞɭɪɵ Get_ ɢ Set_, ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɟ ɫɜɨɣɫɬɜɭ, ɨɫɬɚɸɬɫɹ ɞɨɫɬɭɩɧɵɦɢ ɞɥɹ ɬɟɯ ɫɥɭɱɚɟɜ, ɤɨɝɞɚ ɜ ɤɚɤɨɦ-ɥɢɛɨ ɹɡɵɤɟ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɪɟɚɥɢɡɚɰɢɹ ɫɜɨɣɫɬɜ ɧɟ ɩɨɞɞɟɪɠɢɜɚɟɬɫɹ. 46
ɉɪɢɦɟɧɟɧɢɟ ɢɧɬɟɪɮɟɣɫɨɜ ɞɢɫɩɟɬɱɟɪɢɡɚɰɢɢ ɨɛɟɫɩɟɱɢɜɚɟɬ ɛɵɫɬɪɵɣ ɜɵɡɨɜ ɦɟɬɨɞɨɜ ɢɧɬɟɪɮɟɣɫɚ ɩɪɢ ɩɨɡɞɧɟɦ ɫɜɹɡɵɜɚɧɢɢ, ɬɚɤ ɤɚɤ ɜ ɷɬɨɦ ɫɥɭɱɚɟ ɫɢɫɬɟɦɟ ɧɟ ɬɪɟɛɭɟɬɫɹ ɜɵɡɵɜɚɬɶ ɩɪɨɰɟɞɭɪɭ GetIDsOfNames. ȼ ɤɨɦɩɨɧɟɧɬɟ ɪɟɚɥɢɡɭɟɬɫɹ ɬɨɥɶɤɨ ɨɛɵɱɧɵɣ ɢɧɬɟɪɮɟɣɫ, ɚ ɢɧɬɟɪɮɟɣɫ ɞɢɫɩɟɬɱɟɪɢɡɚɰɢɢ ɦɨɠɟɬ ɩɪɢɦɟɧɹɬɶɫɹ ɬɨɥɶɤɨ ɜ ɩɪɨɰɟɫɫɟ ɨɛɪɚɳɟɧɢɹ ɤɥɢɟɧɬɚ ɤ COMɫɟɪɜɟɪɭ.
5.3. ɉɪɢɦɟɪ ɪɟɚɥɢɡɚɰɢɢ ɜɫɬɪɨɟɧɧɨɝɨ COM-ɫɟɪɜɟɪɚ ɚɜɬɨɦɚɬɢɡɚɰɢɢ ȼ ɤɚɱɟɫɬɜɟ ɩɪɢɦɟɪɚ ɪɟɚɥɢɡɚɰɢɢ ɜɫɬɪɨɟɧɧɨɝɨ COM-ɫɟɪɜɟɪɚ ɚɜɬɨɦɚɬɢɡɚɰɢɢ ɪɟɚɥɢɡɭɟɦ ɤɨɦɩɨɧɟɧɬ ɩɟɪɟɜɨɞɚ ɰɟɥɨɝɨ ɱɢɫɥɚ ɜ ɞɜɨɢɱɧɵɣ, ɜɨɫɶɦɟɪɢɱɧɵɣ ɢ ɲɟɫɬɧɚɞɰɚɬɟɪɢɱɧɵɣ ɜɢɞ, ɤɨɬɨɪɵɣ ɛɭɞɟɬ ɜɵɜɨɞɢɬɶɫɹ ɜ ɜɢɞɟ ɫɬɪɨɤɢ. 1. ȼɵɩɨɥɧɢɜ ɞɟɣɫɬɜɢɹ, ɨɩɢɫɚɧɧɵɟ ɜ ɩ. 2.2, ɫɨɯɪɚɧɢɦ ɩɪɨɟɤɬ ɩɨɞ ɢɦɟɧɟɦ Convert.dpr. 2. Ɂɚɬɟɦ ɜɵɛɟɪɟɦ ɷɥɟɦɟɧɬ Automation Object ɜ ɞɢɚɥɨɝɨɜɨɦ ɨɤɧɟ New Items (ɩ. 2.3). ȼ ɩɨɥɟ Class Name, ɦɚɫɬɟɪɚ Automation Objeɫt Wizard ɜɜɟɞɟɦ ObjConvert. ɇɚɠɚɜ ɧɚ ɤɧɨɩɤɭ OK, ɩɨɥɭɱɢɦ ɦɨɞɭɥɶ ɫɥɟɞɭɸɳɟɝɨ ɫɨɞɟɪɠɚɧɢɹ: unit Unit1; interface uses ComObj, ActiveX, ObjConvert_TLB, StdVcl; type TObjConvert_ = class(TAutoObject, IObjConvert) protected { Protected declarations } end; implementation uses ComServ; initialization TAutoObjectFactory.Create(ComServer, TObjConvert_, Class_ObjConvert_, ciMultiInstance, tmApartment); end. 3. Ⱥɧɚɥɨɝɢɱɧɨ, ɤɚɤ ɢ ɜ ɩɪɢɦɟɪɟ ɩ. 3.4, ɨɩɢɲɟɦ ɜ ɢɧɬɟɪɮɟɣɫɟ IObjConvert ɦɟɬɨɞɵ Bin, Hex, Oct. 47
4. ȼɨɡɜɪɚɬɢɦɫɹ ɜ ɦɨɞɭɥɶ Unit1. ȼ ɪɚɡɞɟɥɟ ɨɩɢɫɚɧɢɹ ɬɢɩɨɜ ɞɚɧɨ ɧɚɱɚɥɶɧɨɟ ɨɩɢɫɚɧɢɟ ɧɚɲɟɝɨ ɤɨɦɩɨɧɟɧɬɚ. ɂɫɯɨɞɹ ɢɡ ɷɬɨɝɨ ɨɩɢɫɚɧɢɹ, ɧɚɲ ɤɨɦɩɨɧɟɧɬ ɧɚɫɥɟɞɭɟɬɫɹ ɨɬ ɤɥɚɫɫɚ TAutoɋomObject ɢ ɩɨɞɞɟɪɠɢɜɚɟɬ ɢɧɬɟɪɮɟɣɫ IObjConvert. ȼ ɪɚɡɞɟɥɟ protected ɨɛɴɹɜɥɟɧɵ ɬɪɢ ɮɭɧɤɰɢɢ: TObjConvert = class(TComObject, IConvert) protected {Declare IConvert methods here} function Bin (const n: Word; var Str: WideString): Boolean; stdcall; function Oct(const n: Word; var Str: WideString): Boolean; stdcall; function Hex(const n: Word; var Str: WideString): Boolean; stdcall; end; 5. Ɉɫɬɚɟɬɫɹ ɬɨɥɶɤɨ ɪɟɚɥɢɡɨɜɚɬɶ ɮɭɧɤɰɢɢ Bin, Hex ɢ Oct ɚɧɚɥɨɝɢɱɧɨ, ɤɚɤ ɢ ɜ ɩɪɢɦɟɪɟ ɩ. 3.4. 6. Ɉɬɤɨɦɩɢɥɢɪɨɜɚɜ ɩɪɢɥɨɠɟɧɢɟ, ɩɨɥɭɱɢɦ COM-ɫɟɪɜɟɪ, ɝɨɬɨɜɵɣ ɤ ɢɫɩɨɥɶɡɨɜɚɧɢɸ. Ɉɧ ɛɭɞɟɬ ɧɚɯɨɞɢɬɶɫɹ ɜ ɛɢɛɥɢɨɬɟɤɟ Convert.dll.
5.4. ɉɪɢɦɟɪ ɪɟɚɥɢɡɚɰɢɢ ɜɧɟɲɧɟɝɨ COM-ɫɟɪɜɟɪɚ ɚɜɬɨɦɚɬɢɡɚɰɢɢ ȼɧɟɲɧɢɟ COM-ɫɟɪɜɟɪɵ ɨɛɵɱɧɨ ɰɟɥɟɫɨɨɛɪɚɡɧɨ ɫɨɡɞɚɜɚɬɶ ɬɨɥɶɤɨ ɜ ɬɟɯ ɫɥɭɱɚɹɯ, ɤɨɝɞɚ ɫɟɪɜɟɪ ɞɨɥɠɟɧ ɪɚɛɨɬɚɬɶ ɤɚɤ ɚɜɬɨɧɨɦɧɨɟ ɩɪɢɥɨɠɟɧɢɟ ɫɨ ɫɜɨɢɦɢ ɫɨɛɫɬɜɟɧɧɵɦɢ ɩɪɚɜɚɦɢ. ɉɪɢɦɟɱɚɧɢɟ 5.2. ɍɞɚɥɟɧɧɵɣ ɞɨɫɬɭɩ, ɬ. ɟ. ɞɨɫɬɭɩ ɨɬ ɤɥɢɟɧɬɚ, ɜɵɩɨɥɧɹɟɦɨɝɨ ɧɚ ɞɪɭɝɨɦ ɤɨɦɩɶɸɬɟɪɟ ɫ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ DCOM, ɜɨɡɦɨɠɟɧ ɬɨɥɶɤɨ ɤ ɜɧɟɲɧɟɦɭ ɫɟɪɜɟɪɭ ɚɜɬɨɦɚɬɢɡɚɰɢɢ. ɉɪɢɜɟɞɟɦ ɩɪɨɫɬɨɣ ɩɪɢɦɟɪ ɪɟɚɥɢɡɚɰɢɢ ɜɧɟɲɧɟɝɨ COM-ɫɟɪɜɟɪɚ, ɤɨɬɨɪɵɣ ɩɨɞɞɟɪɠɢɜɚɟɬ ɤɨɦɩɨɧɟɧɬ ɚɜɬɨɦɚɬɢɡɚɰɢɢ ɞɥɹ ɢɡɦɟɧɟɧɢɹ ɰɜɟɬɚ ɮɨɪɦɵ ɤɥɢɟɧɬɨɦ. 1. ɋɨɡɞɚɞɢɦ ɧɨɜɵɣ ɩɪɨɟɤɬ-ɩɪɢɥɨɠɟɧɢɟ MyServer.dpr ɫɨ ɫɜɨɟɣ ɮɨɪɦɨɣ. 2. ɉɨɦɟɫɬɢɦ ɜ ɩɪɨɟɤɬ ɨɛɴɟɤɬ ɚɜɬɨɦɚɬɢɡɚɰɢɢ, ɜɵɛɪɚɜ ɧɚ ɡɚɤɥɚɞɤɟ ActiveX ɞɢɚɥɨɝɨɜɨɝɨ ɨɤɧɚ New Items (ɪɢɫ. 2.1) ɷɥɟɦɟɧɬ Automation Object. ȼ ɩɨɥɟ Class Name ɦɚɫɬɟɪɚ Automation Objeɫt Wizard ɭɤɚɠɟɦ AutoServer. ɇɚɠɚɜ ɧɚ ɤɧɨɩɤɭ OK, ɩɨɥɭɱɢɦ ɲɚɛɥɨɧ ɞɥɹ ɪɟɚɥɢɡɚɰɢɢ ɤɨɦɩɨɧɟɧɬɚ ɚɜɬɨɦɚɬɢɡɚɰɢɢ. 3. ɂɫɩɨɥɶɡɭɹ ɪɟɞɚɤɬɨɪ ɛɢɛɥɢɨɬɟɤɢ ɬɢɩɨɜ, ɡɚɞɚɞɢɦ ɫɜɨɣɫɬɜɨ Color, ɞɨɫɬɭɩɧɨɟ ɞɥɹ ɱɬɟɧɢɹ ɢ ɡɚɩɢɫɢ, ɜ ɢɧɬɟɪɮɟɣɫɟ IObjServer.
48
4. Ɋɟɚɥɢɡɭɟɦ ɦɟɬɨɞɵ Get_Color ɢ Set_Color: function TAutoServer.Get_Color: SYSUINT; begin result:=Form1.Color; end; procedure TAutoServer.Set_Color(Value: SYSUINT); begin Form1.Color:=Value; end; 5. Ɉɬɤɨɦɩɢɥɢɪɭɣɬɟ ɢ ɡɚɪɟɝɢɫɬɪɢɪɭɣɬɟ ɩɨɥɭɱɟɧɧɨɟ ɩɪɢɥɨɠɟɧɢɟ. ɑɬɨɛɵ ɭɜɢɞɟɬɶ ɪɚɛɨɬɭ ɤɥɢɟɧɬɚ ɢ ɜɧɟɲɧɟɝɨ COM-ɫɟɪɜɟɪɚ, ɪɟɚɥɢɡɭɟɦ ɞɥɹ ɧɚɲɟɝɨ ɩɪɢɦɟɪɚ ɤɥɢɟɧɬ. 1. ɋɨɡɞɚɣɬɟ ɧɨɜɵɣ ɩɪɨɟɤɬ-ɩɪɢɥɨɠɟɧɢɟ MyClient.dpr ɫɨ ɫɜɨɟɣ ɮɨɪɦɨɣ. 2. ȼ ɪɚɡɞɟɥɟ ɨɩɢɫɚɧɢɹ private ɮɨɪɦɵ ɫɨɡɞɚɜɚɟɦɨɝɨ ɩɪɢɥɨɠɟɧɢɹ ɜɜɟɞɢɬɟ ɩɟɪɟɦɟɧɧɭɸ Server ɬɢɩɚ Variant. 3. ɇɚ ɫɨɛɵɬɢɟ OnCreate ɢ OnDestroy ɮɨɪɦɵ ɭɫɬɚɧɨɜɢɬɟ ɩɪɨɰɟɞɭɪɭ ɜɵɡɨɜɚ ɧɚɲɟɝɨ ɫɟɪɜɟɪɚ:
ɉɪɢ ɫɬɚɪɬɟ ɩɪɢɥɨɠɟɧɢɹ-ɤɥɢɟɧɬɚ ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɡɚɩɭɫɤɚɟɬɫɹ ɜɧɟɲɧɢɣ COM-ɫɟɪɜɟɪ. Ɏɨɪɦɚ ɫɟɪɜɟɪɚ ɦɟɧɹɟɬ ɫɜɨɣ ɰɜɟɬ ɜ ɫɨɨɬɜɟɬɫɬɜɢɢ ɫ ɜɵɛɪɚɧɧɵɦ ɰɜɟɬɨɦ ɜ ɞɢɚɥɨɝɨɜɨɦ ɨɤɧɟ ɤɥɢɟɧɬɚ, ɜɵɡɵɜɚɟɦɨɦ ɩɪɢ ɧɚɠɚɬɢɢ ɧɚ ɤɧɨɩɤɭ Button1. Ɉɛɪɚɬɢɬɟ ɜɧɢɦɚɧɢɟ, ɤɨɝɞɚ ɤɥɢɟɧɬ ɡɚɜɟɪɲɚɟɬ ɪɚɛɨɬɭ, ɫɟɪɜɟɪ ɬɨɠɟ ɜɵɝɪɭɠɚɟɬɫɹ.
5.5. ɋɨɛɵɬɢɹ ɜ COM ɢ ɨɛɪɚɬɧɵɟ ɜɵɡɨɜɵ ɉɪɢ ɪɚɡɪɚɛɨɬɤɟ ɫɟɪɶɟɡɧɵɯ COM-ɩɪɢɥɨɠɟɧɢɣ ɱɚɫɬɨ ɬɪɟɛɭɟɬɫɹ ɜɵɩɨɥɧɹɬɶ ɩɪɨɝɪɚɦɦɧɵɣ ɤɨɞ ɤɥɢɟɧɬɚ, ɟɫɥɢ ɜ COM-ɫɟɪɜɟɪɟ ɩɪɨɢɡɨɲɥɨ ɬɨ ɢɥɢ ɢɧɨɟ ɞɟɣɫɬɜɢɟ. Ɉɫɨɛɟɧɧɨ ɷɬɨ ɚɤɬɭɚɥɶɧɨ, ɟɫɥɢ COM-ɫɟɪɜɟɪ ɩɨɞɞɟɪɠɢɜɚɟɬ ɞɢɚɥɨɝɨɜɵɟ ɨɤɧɚ ɞɥɹ ɜɜɨɞɚ ɤɚɤɢɯ-ɥɢɛɨ ɞɚɧɧɵɯ ɢ ɩɟɪɟɞɚɱɢ ɪɟɡɭɥɶɬɚɬɚ ɤɥɢɟɧɬɭ. Ɉɫɧɨɜɧɚɹ ɢɞɟɹ ɦɟɯɚɧɢɡɦɚ ɫɨɛɵɬɢɣ ɫɥɟɞɭɸɳɚɹ: ɤɥɢɟɧɬ ɩɪɢ ɩɨɞɤɥɸɱɟɧɢɢ ɤ COM-ɫɟɪɜɟɪɭ ɩɟɪɟɞɚɟɬ ɢɧɬɟɪɮɟɣɫ, ɜ ɤɨɬɨɪɨɦ ɪɟɚɥɢɡɨɜɚɧɵ ɮɭɧɤɰɢɢ ɫɨɛɵɬɢɣ. ɂɧɬɟɪɮɟɣɫ, ɩɟɪɟɞɚɜɚɟɦɵɣ ɤɥɢɟɧɬɨɦ COM-ɫɟɪɜɟɪɭ, ɧɚɡɵɜɚɟɬɫɹ ɢɧɬɟɪɮɟɣɫɨɦ ɨɛɪɚɬɧɨɝɨ ɜɵɡɨɜɚ. Ⱦɥɹ ɪɟɚɥɢɡɚɰɢɢ ɤɨɦɩɨɧɟɧɬɚ, ɩɨɞɞɟɪɠɢɜɚɸɳɟɝɨ ɦɟɯɚɧɢɡɦ ɨɛɪɚɛɨɬɤɢ ɫɨɛɵɬɢɣ ɢ ɨɛɪɚɬɧɨɝɨ ɜɵɡɨɜɚ, ɜ ɦɚɫɬɟɪɟ Automation Object Wizard ɧɭɠɧɨ ɭɫɬɚɧɨɜɢɬɶ ɮɥɚɠɨɤ Generate Event support code. ɗɬɨ ɩɨɡɜɨɥɢɬ ɫɝɟɧɟɪɢɪɨɜɚɬɶ ɲɚɛɥɨɧ ɫɨɡɞɚɜɚɟɦɨɝɨ ɤɨɦɩɨɧɟɧɬɚ ɫ ɩɨɞɞɟɪɠɤɨɣ ɫɨɛɵɬɢɣ:
procedure TForm1.Button1Click(Sender: TObject);
<ɢɦɹ ɤɨɦɩɨɧɟɧɬɚ> = class(TAutoObject, IConnectionPointContainer, {<ɢɧɬɟɪɮɟɣɫɵ ɤɨɦɩɨɧɟɧɬɚ>}) private { Private declarations } FConnectionPoints: TConnectionPoints; FConnectionPoint: TConnectionPoint; FSinkList: TList; FEvents: <ɢɧɬɟɪɮɟɣɫ ɫɨɛɵɬɢɣ>; public procedure Initialize; override; protected { Protected declarations } property ConnectionPoints: TConnectionPoints read FConnectionPoints implements IConnectionPointContainer;
begin if ColorDialog1.Execute then Server.Color:=ColorDialog1.Color; end;
procedure EventSinkChanged(const EventSink: IUnknown); override; end;
procedure TForm1.FormShow(Sender: TObject); begin Server:=CreateOLEObject('MyServer.AutoServer'); end; procedure TForm1.FormDestroy(Sender: TObject); begin Server:=NULL; end; 4. ɇɚ ɮɨɪɦɟ ɭɫɬɚɧɨɜɢɬɟ ColorDialog ɢ Button1. Ɋɟɚɥɢɡɭɣɬɟ ɫɨɛɵɬɢɟ OnClick ɤɧɨɩɤɢ:
5. Ɉɬɤɨɦɩɢɥɢɪɭɣɬɟ ɩɨɥɭɱɟɧɧɨɟ ɩɪɢɥɨɠɟɧɢɟ.
49
ɗɬɨ ɨɫɧɨɜɧɵɟ ɫɜɨɣɫɬɜɚ ɢ ɦɟɬɨɞɵ ɞɥɹ ɩɨɞɞɟɪɠɤɢ ɤɨɦɩɨɧɟɧɬɨɦ COMɫɟɪɜɟɪɚ ɦɟɯɚɧɢɡɦɚ ɫɨɛɵɬɢɣ ɢ ɮɭɧɤɰɢɢ ɨɛɪɚɬɧɨɝɨ ɜɵɡɨɜɚ. ɉɪɨɰɟɞɭɪɵ 50
Intialize ɢ EventSinkChanged ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɪɟɚɥɢɡɭɸɬɫɹ ɫɪɟɞɨɣ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ Delphi: procedure <ɢɦɹ ɤɨɦɩɨɧɟɧɬɚ>.EventSinkChanged(const EventSink: IUnknown); begin FEvents := EventSink as <ɢɧɬɟɪɮɟɣɫ ɫɨɛɵɬɢɣ>; if FConnectionPoint <> nil then FSinkList := FConnectionPoint.SinkList; end; procedure <ɢɦɹ ɤɨɦɩɨɧɟɧɬɚ>.Initialize; begin inherited Initialize; FConnectionPoints := TConnectionPoints.Create(Self); if AutoFactory.EventTypeInfo <> nil then FConnectionPoint := FConnectionPoints.CreateConnectionPoint( AutoFactory.EventIID, ckSingle, EventConnect) else FConnectionPoint := nil; end; Ʉɨɝɞɚ ɫɨɡɞɚɟɬɫɹ ɤɨɦɩɨɧɟɧɬ ɚɜɬɨɦɚɬɢɡɚɰɢɢ ɫ ɩɨɞɞɟɪɠɤɨɣ ɫɨɛɵɬɢɣ, ɜ ɛɢɛɥɢɨɬɟɤɟ ɬɢɩɨɜ ɜɜɨɞɹɬɫɹ ɞɜɚ ɢɧɬɟɪɮɟɣɫɚ: ɢɧɬɟɪɮɟɣɫ ɤɨɦɩɨɧɟɧɬɚ ɢ ɢɧɬɟɪɮɟɣɫ ɞɢɫɩɟɬɱɟɪɢɡɚɰɢɢ ɫɨɛɵɬɢɣ (ɨɛɵɱɧɨ ɭ ɧɟɝɨ ɨɤɨɧɱɚɧɢɟ Events). ɉɪɢɦɟɱɚɧɢɟ 5.3. ɂɧɬɟɪɮɟɣɫɨɦ ɫɨɛɵɬɢɣ ɦɨɠɟɬ ɛɵɬɶ ɥɸɛɨɣ ɢɧɬɟɪɮɟɣɫ ɞɢɫɩɟɬɱɟɪɢɡɚɰɢɢ, ɧɨ ɬɨɝɞɚ ɩɪɢɞɟɬɫɹ ɜɪɭɱɧɭɸ ɟɝɨ ɭɫɬɚɧɨɜɢɬɶ ɜ ɋɨ-ɤɥɚɫɫ (ɡɚɤɥɚɞɤɚ implements ɜ ɛɢɛɥɢɨɬɟɤɟ ɬɢɩɨɜ) ɢ ɡɚɞɚɬɶ ɟɦɭ ɡɧɚɱɟɧɢɹ Source ɢ Default (ɨɫɧɨɜɧɨɣ ɢɧɬɟɪɮɟɣɫ ɛɭɞɟɬ ɩɨɞɞɟɪɠɢɜɚɬɶ Default). FɋonnectionPoints – ɚɝɪɟɝɢɪɨɜɚɧɧɵɣ ɨɛɴɟɤɬ, ɜ ɤɨɬɨɪɨɦ ɪɟɚɥɢɡɨɜɚɧ ɢɧɬɟɪɮɟɣɫ IConnectionPointContainer, ɤɨɬɨɪɵɣ ɧɟɨɛɯɨɞɢɦ ɞɥɹ ɩɨɞɞɟɪɠɤɢ ɦɟɯɚɧɢɡɦɚ ɫɨɛɵɬɢɣ. FConnectionPoint – ɷɥɟɦɟɧɬ ɨɛɴɟɤɬɚ FɋonnectionPoints. FSinkList – ɫɩɢɫɨɤ ɢɧɬɟɪɮɟɣɫɨɜ ɤɥɢɟɧɬɨɜ. FEvents – ɢɧɬɟɪɮɟɣɫ ɨɛɪɚɬɧɨɝɨ ɜɵɡɨɜɚ. ȿɫɥɢ ɧɭɠɧɨ ɜɵɡɜɚɬɶ ɮɭɧɤɰɢɸ, ɨɬɜɟɱɚɸɳɭɸ ɡɚ ɤɚɤɨɟ-ɥɢɛɨ ɫɨɛɵɬɢɟ, ɬɨ ɧɟɨɛɯɨɞɢɦɨ ɧɚɩɢɫɚɬɶ ɫɥɟɞɭɸɳɢɣ ɤɨɞ: FEvents.< ɮɭɧɤɰɢɹ ɫɨɛɵɬɢɹ>; ɉɪɢɦɟɱɚɧɢɟ 5.4. ɉɟɪɟɞ ɬɟɦ, ɤɚɤ ɜɵɡɜɚɬɶ ɮɭɧɤɰɢɸ ɫɨɛɵɬɢɹ, ɢɫɩɨɥɶɡɭɣɬɟ ɩɪɨɜɟɪɤɭ ɧɚɥɢɱɢɹ ɫɫɵɥɤɢ ɜ ɩɟɪɟɦɟɧɧɨɣ FEvents: if Assigned(FEvents) then FEvents.< ɮɭɧɤɰɢɹ ɫɨɛɵɬɢɹ>; ɗɬɨ ɧɟɨɛɯɨɞɢɦɨ ɞɥɹ ɢɡɛɟɠɚɧɢɹ ɨɲɢɛɨɤ, ɤɨɝɞɚ ɤɨɦɩɨɧɟɧɬ ɧɟ ɩɨɥɭɱɢɥ ɨɬ ɤɥɢɟɧɬɚ ɢɧɬɟɪɮɟɣɫ ɨɛɪɚɬɧɨɝɨ ɜɵɡɨɜɚ. 51
ȼ Delphi ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɫɨɡɞɚɟɬɫɹ ɩɪɨɝɪɚɦɦɧɵɣ ɤɨɞ, ɩɨɞɞɟɪɠɢɜɚɸɳɢɣ ɦɟɯɚɧɢɡɦ ɨɛɪɚɛɨɬɤɢ ɫɨɛɵɬɢɣ ɬɨɥɶɤɨ ɞɥɹ ɩɟɪɜɨɝɨ ɜ ɩɨɪɹɞɤɟ ɩɨɞɤɥɸɱɟɧɢɣ ɤɥɢɟɧɬɚ. ɑɬɨɛɵ ɛɵɥɚ ɜɨɡɦɨɠɧɨɫɬɶ ɩɟɪɟɞɚɜɚɬɶ ɫɨɛɵɬɢɹ ɦɧɨɠɟɫɬɜɭ ɩɨɞɤɥɸɱɟɧɧɵɯ ɤɥɢɟɧɬɨɜ, ɧɭɠɧɨ ɜɧɟɫɬɢ ɧɟɤɨɬɨɪɵɟ ɢɡɦɟɧɟɧɢɹ ɜ ɩɪɨɝɪɚɦɦɧɵɣ ɤɨɞ. ȼɨ-ɩɟɪɜɵɯ, ɞɥɹ ɜɨɡɦɨɠɧɨɫɬɢ ɩɨɥɭɱɟɧɢɹ ɫɫɵɥɤɢ ɧɚ ɞɚɧɧɵɣ COMɫɟɪɜɟɪ ɫ ɩɨɦɨɳɶɸ ɮɭɧɤɰɢɢ GetOleObject ɟɝɨ ɧɭɠɧɨ ɡɚɪɟɝɢɫɬɪɢɪɨɜɚɬɶ ɜ ɬɚɛɥɢɰɟ ɜɵɩɨɥɧɹɟɦɵɯ ɨɛɴɟɤɬɨɜ Windows. Ⱦɥɹ ɷɬɢɯ ɰɟɥɟɣ ɜ ɤɥɚɫɫ ɩɪɨɟɤɬɢɪɭɟɦɨɝɨ ɤɨɦɩɨɧɟɧɬɚ ɧɭɠɧɨ ɞɨɛɚɜɢɬɶ ɩɨɥɟ FObjectID: integer ɞɥɹ ɯɪɚɧɟɧɢɹ ɢɞɟɧɬɢɮɢɤɚɬɨɪɚ, ɚ ɜ ɩɪɨɰɟɞɭɪɭ Intitialize ɞɨɛɚɜɢɬɶ ɩɪɨɰɟɞɭɪɭ ɪɟɝɢɫɬɪɚɰɢɢ ɤɨɦɩɨɧɟɧɬɚ: RegisterActiveObject(self as Iunknown, , ACTIVEOBJECT_WEAK, FObjectID) Ⱦɥɹ ɬɨɝɨ ɱɬɨɛɵ ɭɞɚɥɢɬɶ ɤɨɦɩɨɧɟɧɬ ɢɡ ɬɚɛɥɢɰɵ ɜɵɩɨɥɧɹɟɦɵɯ ɨɛɴɟɤɬɨɜ Windows, ɧɭɠɧɨ ɫɨɡɞɚɬɶ ɦɟɬɨɞ Destroy ɢ ɜ ɧɟɦ ɜɵɡɜɚɬɶ ɮɭɧɤɰɢɸ RevokeActiveObject: destructor <ɢɦɹ ɤɨɦɩɨɧɟɧɬɚ>.Destroy; begin RevokeActiveObject(FObjectID, nil); end; ɉɪɢ ɫɨɡɞɚɧɢɢ ɷɥɟɦɟɧɬɚ FConnectionPoint ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɚ CreateConnectionPoint ɜ ɤɚɱɟɫɬɜɟ ɩɪɟɞɩɨɫɥɟɞɧɟɝɨ ɩɚɪɚɦɟɬɪɚ ɩɟɪɟɞɚɟɬɫɹ ɧɟ ckSingle (ɡɚɞɚɧɧɵɣ ɩɨ ɭɦɨɥɱɚɧɢɸ), ɚ ckMulti. ȼ ɢɬɨɝɟ ɪɟɚɥɢɡɚɰɢɹ ɦɟɬɨɞɚ Initialize ɛɭɞɟɬ ɫɥɟɞɭɸɳɚɹ: procedure <ɢɦɹ ɤɨɦɩɨɧɟɧɬɚ>.Initialize; begin inherited Initialize; FConnectionPoints := TConnectionPoints.Create(Self); if AutoFactory.EventTypeInfo <> nil then FConnectionPoint := FConnectionPoints.CreateConnectionPoint (AutoFactory.EventIID, ckMulti, EventConnect) else FConnectionPoint := nil; RegisterActiveObject(self as Iunknown, , ACTIVEOBJECT_WEAK, FObjectID) end; Ⱦɥɹ ɬɨɝɨ ɱɬɨɛɵ ɨɬɫɥɟɠɢɜɚɬɶ ɦɧɨɠɟɫɬɜɨ ɩɚɪɚɥɥɟɥɶɧɵɯ ɩɨɞɤɥɸɱɟɧɢɣ, ɧɭɠɧɨ ɪɟɚɥɢɡɨɜɚɬɶ ɫɥɟɞɭɸɳɭɸ ɮɭɧɤɰɢɸ: function <ɢɦɹ ɤɨɦɩɨɧɟɧɬɚ>.GetEnumerator: IEnumConnections; var 52
Container: IConnectionPointContainer; ConnectionPoint: IconnectionPoint; begin OleCheck(QueryInterface(IConnectionPointContainer, Container)); OleCheck(Container.FindConnectionPoint(AutoFactory.EventIID, ConnectionPoint)); ConnectionPoint.EnumConnections(result); end; Ⱦɚɧɧɚɹ ɮɭɧɤɰɢɹ ɩɨɡɜɨɥɹɟɬ ɩɨɥɭɱɢɬɶ ɞɚɧɧɵɟ ɨɬ ɫɱɟɬɱɢɤɚ ɩɨɞɤɥɸɱɟɧɢɣ, ɩɨɫɥɟ ɱɟɝɨ ɧɭɠɧɨ «ɩɪɨɣɬɢɫɶ» ɩɨ ɜɫɟɦ ɩɨɞɤɥɸɱɟɧɢɹɦ: var Enum: IEnumConnections; ConnectData: TConnectData; Fetched: Cardinal; <………………………>
ɉɪɢɦɟɱɚɧɢɟ 5.6. Ɉɛɪɚɬɢɬɟ ɜɧɢɦɚɧɢɟ, ɱɬɨ ɢɧɬɟɪɮɟɣɫɵ ɤɨɦɩɨɧɟɧɬɚ ɢ ɨɛɴɟɤɬɚ ɜ ɩɪɢɥɨɠɟɧɢɢ ɞɨɥɠɧɵ ɢɦɟɬɶ ɨɞɢɧɚɤɨɜɵɣ IID. Ɍɚɤɠɟ ɞɨɥɠɧɚ ɫɨɜɩɚɞɚɬɶ ɧɭɦɟɪɚɰɢɹ ɦɟɬɨɞɨɜ ɢɧɬɟɪɮɟɣɫɨɜ. ɉɪɢ ɩɨɞɤɥɸɱɟɧɢɢ ɩɪɢɥɨɠɟɧɢɹ ɤ ɤɨɦɩɨɧɟɧɬɭ ɧɭɠɧɨ ɜɵɩɨɥɧɢɬɶ ɫɥɟɞɭɸɳɢɣ ɩɪɨɝɪɚɦɦɧɵɣ ɤɨɞ: with <ɢɧɬɟɪɮɟɣɫ ɤɨɦɩɨɧɟɧɬɚ> as IConnectionPointContainer do begin FindConnectionPoint(, ESink); OleCheck(ESink.Advise(<ɤɨɦɩɨɧɟɧɬ ɨɛɪɚɛɨɬɤɢ ɫɨɛɵɬɢɣ> as IDispatch, ConnectId)); end; ȼ ɞɚɧɧɨɦ ɩɪɨɝɪɚɦɦɧɨɦ ɤɨɞɟ ɜɵɩɨɥɧɹɸɬɫɹ ɫɥɟɞɭɸɳɢɟ ɞɟɣɫɬɜɢɹ:
begin {ɦɟɬɨɞ ɪɚɡɪɚɛɚɬɵɜɚɟɦɨɝɨ ɤɨɦɩɨɧɟɧɬɚ} <…………………………….> {ɨɛɪɚɛɨɬɤɚ ɫɨɛɵɬɢɣ ɩɪɢ ɩɨɞɤɥɸɱɟɧɢɢ ɦɧɨɠɟɫɬɜɚ ɤɥɢɟɧɬɨɜ} Enum:=GetEnumerator; if Enum <> nil then begin while Enum.Next(1,ConnectData,@Fetched) = S_OK do if ConnectData.pUnk <> nil then (ConnectData.pUnk as <ɢɧɬɟɪɮɟɣɫ ɫɨɛɵɬɢɣ>).<ɮɭɧɤɰɢɹ ɫɨɛɵɬɢɣ> end; <…………………………….> end; Ɋɚɫɫɦɨɬɪɢɦ ɞɚɥɟɟ ɪɟɚɥɢɡɚɰɢɸ ɩɪɢɥɨɠɟɧɢɣ, ɢɫɩɨɥɶɡɭɸɳɢɯ COMɫɟɪɜɟɪɵ, ɩɨɞɞɟɪɠɢɜɚɸɳɢɟ ɫɨɛɵɬɢɹ. ɉɪɢɦɟɱɚɧɢɟ 5.5. Ɋɟɚɥɢɡɚɰɢɹ ɩɪɢɥɨɠɟɧɢɣ-ɤɥɢɟɧɬɨɜ ɞɥɹ COMɫɟɪɜɟɪɨɜ, ɩɨɞɞɟɪɠɢɜɚɸɳɢɯ ɫɨɛɵɬɢɹ, ɭɩɪɨɳɟɧɚ ɬɟɦ, ɱɬɨ ɫ ɩɨɦɨɳɶɸ ɞɢɚɥɨɝɨɜɨɝɨ ɨɤɧɚ ɢɦɩɨɪɬɚ ɛɢɛɥɢɨɬɟɤɢ ɬɢɩɨɜ ɦɨɠɧɨ ɫɝɟɧɟɪɢɪɨɜɚɬɶ ɤɨɦɩɨɧɟɧɬ Delphi, ɹɜɥɹɸɳɢɣɫɹ ɨɛɨɥɨɱɤɨɣ, ɩɨɞɞɟɪɠɢɜɚɸɳɟɣ ɜɫɟ ɫɜɨɣɫɬɜɚ ɢ ɦɟɬɨɞɵ ɤɨɦɩɨɧɟɧɬɚ COM-ɫɟɪɜɟɪɚ. Ⱦɥɹ ɬɨɝɨ ɱɬɨɛɵ ɨɛɪɚɛɚɬɵɜɚɬɶ ɫɨɛɵɬɢɹ ɩɨɞɤɥɸɱɟɧɧɨɝɨ ɤɨɦɩɨɧɟɧɬɚ, ɧɟɨɛɯɨɞɢɦɨ ɫɨɡɞɚɬɶ ɜ ɩɪɢɥɨɠɟɧɢɢ ɨɛɴɟɤɬ, ɩɨɞɞɟɪɠɢɜɚɸɳɢɣ ɢɧɬɟɪɮɟɣɫ, ɫɯɨɠɢɣ ɫ ɢɧɬɟɪɮɟɣɫɨɦ ɫɨɛɵɬɢɣ ɤɨɦɩɨɧɟɧɬɚ. Ɉɬɥɢɱɢɟ ɞɚɧɧɵɯ ɢɧɬɟɪɮɟɣɫɨɜ ɡɚɤɥɸɱɚɟɬɫɹ ɜ ɬɨɦ, ɱɬɨ ɜ ɤɨɦɩɨɧɟɧɬɟ ɢɫɩɨɥɶɡɭɟɬɫɹ ɢɧɬɟɪɮɟɣɫ ɞɢɫɩɟɬɱɟɪɢɡɚɰɢɢ (dispinterface), ɚ ɜ ɩɪɢɥɨɠɟɧɢɢ – ɨɛɵɱɧɵɣ ɢɧɬɟɪɮɟɣɫ. 53
1. ɂɳɟɬɫɹ ɬɨɱɤɚ «ɜɯɨɞɚ» ɞɥɹ ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɝɨ ɨɛɪɚɛɨɬɱɢɤɚ ɫɨɛɵɬɢɣ. 2. ȼ ɤɨɦɩɨɧɟɧɬ ɩɟɪɟɞɚɟɬɫɹ ɭɤɚɡɚɬɟɥɶ ɧɚ ɢɧɬɟɪɮɟɣɫ ɨɛɴɟɤɬɚ ɩɨɞɤɥɸɱɟɧɢɹ ɞɥɹ ɨɛɪɚɛɨɬɤɢ ɫɨɛɵɬɢɣ. Ɏɭɧɤɰɢɹ ESink.Advise ɜ ɩɚɪɚɦɟɬɪɟ ConnectId ɜɨɡɜɪɚɳɚɟɬ ɢɞɟɧɬɢɮɢɤɚɬɨɪ ɫɨɟɞɢɧɟɧɢɹ, ɤɨɬɨɪɵɣ ɛɭɞɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɞɥɹ ɪɚɡɪɵɜɚ ɫɜɹɡɢ ɤɥɢɟɧɬɚ ɫ ɫɟɪɜɟɪɨɦ. ɉɪɢ ɡɚɜɟɪɲɟɧɢɢ ɪɚɛɨɬɵ ɫ ɤɨɦɩɨɧɟɧɬɨɦ ɧɭɠɧɨ ɪɚɡɨɪɜɚɬɶ ɫɜɹɡɶ ɦɟɠɞɭ ɤɥɢɟɧɬɨɦ ɢ ɫɟɪɜɟɪɨɦ. Ⱦɥɹ ɷɬɢɯ ɰɟɥɟɣ ɢɫɩɨɥɶɡɭɟɬɫɹ ɫɥɟɞɭɸɳɢɣ ɩɪɨɝɪɚɦɦɧɵɣ ɤɨɞ: with <ɢɧɬɟɮɟɣɫ ɤɨɦɩɨɧɟɧɬɚ> as IConnectionPointContainer do begin FindConnectionPoint(, ESink); ESink.UnAdvise(ConnectId); end; Ⱥɧɚɥɨɝɢɱɧɨ, ɤɚɤ ɢ ɩɪɢ ɩɨɞɤɥɸɱɟɧɢɢ, ɢɳɟɬɫɹ ɬɨɱɤɚ «ɜɯɨɞɚ» ɞɥɹ ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɝɨ ɨɛɪɚɛɨɬɱɢɤɚ ɫɨɛɵɬɢɣ, ɡɚɬɟɦ ɜɵɡɵɜɚɟɬɫɹ ɩɪɨɰɟɞɭɪɚ ɪɚɡɪɵɜɚ ɫɨɟɞɢɧɟɧɢɹ ESink.UnAdvise, ɜ ɤɨɬɨɪɭɸ ɩɟɪɟɞɚɟɬɫɹ ɢɞɟɧɬɢɮɢɤɚɬɨɪ ɫɨɟɞɢɧɟɧɢɹ, ɩɨɥɭɱɟɧɧɵɣ ɜɨ ɜɪɟɦɹ ɩɨɞɤɥɸɱɟɧɢɹ ɤ ɫɟɪɜɟɪɭ. ɉɪɢɦɟɱɚɧɢɟ 5.7. ɇɟ ɡɚɛɵɜɚɣɬɟ ɨɬɤɥɸɱɚɬɶɫɹ ɨɬ ɫɟɪɜɟɪɨɜ, ɢɧɚɱɟ ɫɢɫɬɟɦɚ COM ɛɭɞɟɬ ɫɱɢɬɚɬɶ, ɱɬɨ ɤɨɦɩɨɧɟɧɬ COM-ɫɟɪɜɟɪɚ ɟɳɟ ɢɫɩɨɥɶɡɭɟɬɫɹ ɢ ɧɟ ɫɦɨɠɟɬ ɟɝɨ ɜɵɝɪɭɡɢɬɶ.
VI. Ⱥɜɬɨɦɚɬɢɡɚɰɢɹ ɩɪɢɥɨɠɟɧɢɣ Microsoft Office ȼɨ ɦɧɨɝɢɯ ɩɪɢɥɨɠɟɧɢɹɯ, ɫɜɹɡɚɧɧɵɯ ɫ ɮɨɪɦɢɪɨɜɚɧɢɟɦ ɞɨɤɭɦɟɧɬɨɜ ɢ ɨɬɱɟɬɨɜ, ɧɟɪɟɞɤɨ ɩɪɢɦɟɧɹɸɬ ɫɟɪɜɢɫɵ, ɩɪɟɞɨɫɬɚɜɥɹɟɦɵɟ Microsoft Office. ɉɪɨɝɪɚɦɦɵ, ɩɪɟɞɨɫɬɚɜɥɹɟɦɵɟ ɜ ɞɚɧɧɨɦ ɩɚɤɟɬɟ, ɪɚɡɪɚɛɨɬɚɧɵ ɩɨ ɬɟɯɧɨɥɨ54
ɝɢɢ COM, ɱɬɨ ɨɛɟɫɩɟɱɢɜɚɟɬ ɜɨɡɦɨɠɧɨɫɬɶ ɭɩɪɚɜɥɟɧɢɹ ɞɚɧɧɵɦ ɩɪɢɥɨɠɟɧɢɟɦ ɫ ɩɨɦɨɳɶɸ ɞɪɭɝɢɯ ɩɪɢɥɨɠɟɧɢɣ ɢ ɦɚɤɪɨɹɡɵɤɨɜ. ȼɫɟ ɤɨɦɩɨɧɟɧɬɵ Microsoft Office ɩɨɞɞɟɪɠɢɜɚɸɬ ɨɞɢɧ ɢ ɬɨɬ ɠɟ ɦɚɤɪɨɹɡɵɤ: Visual Basic for Applications (VBA), ɩɨɡɜɨɥɹɸɳɢɣ ɫɨɡɞɚɜɚɬɶ ɩɪɢɥɨɠɟɧɢɹ ɧɟɩɨɫɪɟɞɫɬɜɟɧɧɨ ɜɧɭɬɪɢ ɞɨɤɭɦɟɧɬɨɜ Office (ɷɬɨ ɧɚɡɵɜɚɟɬɫɹ «ɪɟɲɟɧɢɹɦɢ ɧɚ ɛɚɡɟ Microsoft Office»). ɍɩɪɚɜɥɟɧɢɟ ɠɟ ɤɨɦɩɨɧɟɧɬɚɦɢ Office ɢɡ ɞɪɭɝɢɯ ɩɪɢɥɨɠɟɧɢɣ ɨɫɭɳɟɫɬɜɥɹɟɬɫɹ ɫ ɩɨɦɨɳɶɸ ɚɜɬɨɦɚɬɢɡɚɰɢɢ (Automation) – ɜɫɟ ɩɪɢɥɨɠɟɧɢɹ Microsoft Office ɹɜɥɹɸɬɫɹ ɫɟɪɜɟɪɚɦɢ ɚɜɬɨɦɚɬɢɡɚɰɢɢ (ɢɥɢ COM-ɫɟɪɜɟɪɚɦɢ). Ⱦɥɹ ɫɨɡɞɚɧɢɹ ɬɚɤɢɯ ɩɪɢɥɨɠɟɧɢɣ ɩɪɢɝɨɞɧɵ ɥɸɛɵɟ ɫɪɟɞɫɬɜɚ ɪɚɡɪɚɛɨɬɤɢ, ɩɨɡɜɨɥɹɸɳɢɟ ɫɨɡɞɚɜɚɬɶ ɤɨɧɬɪɨɥɥɟɪɵ ɚɜɬɨɦɚɬɢɡɚɰɢɢ (COM-ɤɥɢɟɧɬɵ). ɇɚɢɛɨɥɟɟ ɱɚɫɬɨ ɞɥɹ ɷɬɨɣ ɰɟɥɢ ɢɫɩɨɥɶɡɭɟɬɫɹ Visual Basic, ɧɨ ɷɬɨ ɦɨɝɭɬ ɛɵɬɶ ɢ Delphi, C++ Builder, Visual C++ ɢ ɬ. ɩ. Ɉɞɧɚɤɨ, ɩɪɟɠɞɟ ɱɟɦ ɨɛɫɭɠɞɚɬɶ ɜɨɡɦɨɠɧɨɫɬɢ ɬɟɯ ɢɥɢ ɢɧɵɯ ɫɪɟɞɫɬɜ ɪɚɡɪɚɛɨɬɤɢ, ɫɥɟɞɭɟɬ ɪɚɡɨɛɪɚɬɶɫɹ, ɱɬɨ ɬɚɤɨɟ ɚɜɬɨɦɚɬɢɡɚɰɢɹ. ɉɪɚɤɬɢɱɟɫɤɢ ɜɫɟ, ɱɬɨ ɜ ɫɨɫɬɨɹɧɢɢ ɫɞɟɥɚɬɶ ɩɨɥɶɡɨɜɚɬɟɥɶ ɥɸɛɨɝɨ ɩɪɢɥɨɠɟɧɢɹ Microsoft Office ɫ ɩɨɦɨɳɶɸ ɦɟɧɸ, ɤɥɚɜɢɚɬɭɪɵ ɢ ɢɧɫɬɪɭɦɟɧɬɚɥɶɧɨɣ ɩɚɧɟɥɢ, ɦɨɠɟɬ ɛɵɬɶ ɪɟɚɥɢɡɨɜɚɧɨ ɢɡ ɩɪɢɥɨɠɟɧɢɹ, ɫɨɡɞɚɧɧɨɝɨ ɫ ɩɨɦɨɳɶɸ ɨɞɧɨɝɨ ɢɡ ɫɪɟɞɫɬɜ ɪɚɡɪɚɛɨɬɤɢ. Ɉɫɧɨɜɧɨɟ ɩɪɟɢɦɭɳɟɫɬɜɨ ɩɪɢɦɟɧɟɧɢɹ ɩɪɢɥɨɠɟɧɢɣ Microsoft Office ɫɜɹɡɚɧɨ ɫ ɬɟɦ, ɱɬɨ ɧɟɬ ɧɟɨɛɯɨɞɢɦɨɫɬɢ ɪɚɡɪɚɛɚɬɵɜɚɬɶ ɫɨɛɫɬɜɟɧɧɵɟ ɫɪɟɞɫɬɜɚ ɮɨɪɦɢɪɨɜɚɧɢɹ ɢ ɩɪɨɫɦɨɬɪɚ ɨɬɱɟɬɨɜ. Ʉ ɬɨɦɭ ɠɟ, ɧɟɤɨɬɨɪɵɟ ɩɨɥɶɡɨɜɚɬɟɥɢ, ɩɪɢɜɵɤɲɢɟ ɢɫɩɨɥɶɡɨɜɚɬɶ ɩɪɢɥɨɠɟɧɢɹ Microsoft Office ɜ ɩɨɜɫɟɞɧɟɜɧɨɣ ɪɚɛɨɬɟ, ɩɪɟɞɩɨɱɢɬɚɸɬ ɯɪɚɧɢɬɶ ɨɬɱɟɬɵ ɢ ɞɪɭɝɢɟ ɞɨɤɭɦɟɧɬɵ ɜ ɨɞɧɨɦ ɢɡ ɮɨɪɦɚɬɨɜ Microsoft Office. ȼ ɞɚɧɧɨɣ ɝɥɚɜɟ ɩɪɢɜɟɞɟɧɨ ɨɩɢɫɚɧɢɟ ɦɟɬɨɞɢɤɢ ɪɟɚɥɢɡɚɰɢɢ ɭɩɪɚɜɥɟɧɢɹ ɩɪɢɥɨɠɟɧɢɹɦɢ Word ɢ Excel ɜ ɩɪɨɝɪɚɦɦɚɯ, ɪɚɡɪɚɛɚɬɵɜɚɟɦɵɯ ɜ ɜɢɡɭɚɥɶɧɨɣ ɫɪɟɞɟ Delphi, ɧɚ ɨɫɧɨɜɟ ɬɟɯɧɨɥɨɝɢɢ ɚɜɬɨɦɚɬɢɡɚɰɢɢ COMɩɪɢɥɨɠɟɧɢɣ. ɋɨɜɪɟɦɟɧɧɵɟ ɜɢɡɭɚɥɶɧɵɟ ɫɪɟɞɵ Delphi (ɜɟɪɫɢɢ 7 ɢ ɜɵɲɟ) ɩɨɡɜɨɥɹɸɬ ɫɨɡɞɚɜɚɬɶ ɤɨɦɩɨɧɟɧɬɵ, ɜɧɭɬɪɢ ɤɨɬɨɪɵɯ ɪɟɚɥɢɡɨɜɚɧɚ ɫɜɹɡɶ ɫ COMɫɟɪɜɟɪɚɦɢ. ɋ ɨɞɧɨɣ ɫɬɨɪɨɧɵ, ɬɚɤɨɣ ɩɨɞɯɨɞ ɤ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɸ ɩɨɡɜɨɥɹɟɬ ɢɡɛɟɠɚɬɶ ɧɟɤɨɬɨɪɵɯ ɬɪɭɞɧɨɫɬɟɣ, ɫɜɹɡɚɧɧɵɯ ɫ ɪɟɚɥɢɡɚɰɢɟɣ ɬɟɯɧɨɥɨɝɢɢ COM, ɚ ɫ ɞɪɭɝɨɣ ɫɬɨɪɨɧɵ – ɩɪɨɝɪɚɦɦɵ, ɪɟɚɥɢɡɨɜɚɧɧɵɟ ɧɚ ɬɚɤɢɯ ɤɨɦɩɨɧɟɧɬɚɯ, ɪɚɛɨɬɚɸɬ ɫɭɳɟɫɬɜɟɧɧɨ ɦɟɞɥɟɧɧɟɟ, ɱɟɦ ɬɟ, ɤɨɬɨɪɵɟ ɢɫɩɨɥɶɡɭɸɬ ɢɧɬɟɪɮɟɣɫɵ ɧɚɩɪɹɦɭɸ. Ⱦɪɭɝɢɦ ɨɬɪɢɰɚɬɟɥɶɧɵɦ ɦɨɦɟɧɬɨɦ ɹɜɥɹɟɬɫɹ ɬɨ, ɱɬɨ ɧɟɤɨɬɨɪɵɟ ɦɟɬɨɞɵ ɢɦɟɸɬ ɞɨɫɬɚɬɨɱɧɨ ɛɨɥɶɲɨɟ ɤɨɥɢɱɟɫɬɜɨ ɩɚɪɚɦɟɬɪɨɜ (ɧɚɩɪɢɦɟɪ, ɦɟɬɨɞ PrintOut ɩɪɢɥɨɠɟɧɢɹ Word ɢɦɟɟɬ ɨɤɨɥɨ ɞɜɚɞɰɚɬɢ ɩɚɪɚɦɟɬɪɨɜ), ɭɫɬɚɧɨɜɥɟɧɧɵɯ ɩɨ ɭɦɨɥɱɚɧɢɸ, ɧɨ ɜ ɩɪɨɰɟɫɫɟ ɮɨɪɦɢɪɨɜɚɧɢɹ ɤɨɦɩɨɧɟɧɬɨɜ ɜ ɫɪɟɞɟ Delphi ɞɚɧɧɵɟ ɡɧɚɱɟɧɢɹ ɬɟɪɹɸɬɫɹ ɢ ɢɯ ɜɫɟ ɪɚɜɧɨ ɩɪɢɯɨɞɢɬɫɹ ɜɜɨɞɢɬɶ. ȼɜɢɞɭ ɩɟɪɟɱɢɫɥɟɧɧɵɯ ɧɟɭɞɨɛɫɬɜ ɛɨɥɶɲɢɧɫɬɜɨ ɩɪɨɝɪɚɦɦɢɫɬɨɜ ɩɪɟɞɩɨɱɢɬɚɸɬ ɢɫɩɨɥɶɡɨɜɚɬɶ ɩɨɡɞɧɟɟ ɫɜɹɡɵɜɚɧɢɟ, ɝɞɟ ɪɟɚɥɶɧɚɹ ɫɫɵɥɤɚ ɧɚ ɨɛɴɟɤɬ ɜ ɩɟɪɟɦɟɧɧɨɣ ɢɥɢ ɤɥɚɫɫɟ ɩɨɹɜɥɹɟɬɫɹ ɧɚ ɷɬɚɩɟ ɜɵɩɨɥɧɟɧɢɹ ɩɪɢɥɨɠɟ-
ɧɢɹ-ɤɨɧɬɪɨɥɥɟɪɚ. ɏɨɬɹ ɷɬɨɬ ɫɩɨɫɨɛ ɛɨɥɟɟ ɦɟɞɥɟɧɧɵɣ, ɱɟɦ ɪɚɧɟɟ ɫɜɹɡɵɜɚɧɢɟ, ɧɨ ɨɧ ɞɨɫɬɭɩɟɧ ɜɨ ɜɫɟɯ ɫɪɟɞɫɬɜɚɯ ɪɚɡɪɚɛɨɬɤɢ, ɩɨɡɜɨɥɹɸɳɢɯ ɫɨɡɞɚɜɚɬɶ ɤɨɧɬɪɨɥɥɟɪɵ ɚɜɬɨɦɚɬɢɡɚɰɢɢ, ɢ ɦɟɧɟɟ ɱɭɜɫɬɜɢɬɟɥɟɧ ɤ ɬɨɦɭ, ɜɫɟ ɥɢ ɩɚɪɚɦɟɬɪɵ ɦɟɬɨɞɨɜ ɩɟɪɟɱɢɫɥɟɧɵ ɩɪɢ ɢɯ ɜɵɡɨɜɚɯ ɜ ɤɨɞɟ ɩɪɢɥɨɠɟɧɢɹɤɨɧɬɪɨɥɥɟɪɚ. ɉɪɢ ɩɨɡɞɧɟɦ ɫɜɹɡɵɜɚɧɢɢ ɤɨɪɪɟɤɬɧɨɫɬɶ ɜɵɡɨɜɨɜ ɦɟɬɨɞɨɜ ɩɪɨɜɟɪɹɟɬɫɹ ɜ ɦɨɦɟɧɬ ɢɯ ɨɫɭɳɟɫɬɜɥɟɧɢɹ, ɬɨ ɟɫɬɶ ɧɚ ɷɬɚɩɟ ɜɵɩɨɥɧɟɧɢɹ ɩɪɢɥɨɠɟɧɢɹ, ɚ ɧɟ ɧɚ ɷɬɚɩɟ ɟɝɨ ɤɨɦɩɢɥɹɰɢɢ. ȼɨ ɜɫɟɯ ɩɪɢɦɟɪɚɯ ɞɚɧɧɨɣ ɝɥɚɜɵ ɢɫɩɨɥɶɡɭɟɬɫɹ ɪɚɧɧɟɟ ɫɜɹɡɵɜɚɧɢɟ.
55
56
6.1. VBA ɢ ɫɪɟɞɫɬɜɚ ɪɚɡɪɚɛɨɬɤɢ ɤɨɧɬɪɨɥɥɟɪɨɜ ɚɜɬɨɦɚɬɢɡɚɰɢɢ Ʉɚɤ ɭɡɧɚɬɶ, ɤɚɤɢɟ ɨɛɴɟɤɬɵ ɞɨɫɬɭɩɧɵ ɜ ɫɟɪɜɟɪɚɯ ɚɜɬɨɦɚɬɢɡɚɰɢɢ ɩɪɢɥɨɠɟɧɢɣ Microsoft Office? Ⱦɥɹ ɷɬɨɣ ɰɟɥɢ ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɞɨɤɭɦɟɧɬɚɰɢɸ, ɨɩɢɫɵɜɚɸɳɭɸ ɢɯ ɨɛɴɟɤɬɧɭɸ ɦɨɞɟɥɶ [3]. Ɍɚɤɠɟ ɦɨɝɭɬ ɛɵɬɶ ɩɨɥɟɡɧɵɦɢ ɫɩɪɚɜɨɱɧɵɟ ɮɚɣɥɵ ɞɥɹ ɩɪɨɝɪɚɦɦɢɫɬɨɜ ɧɚ Visual Basic for Applications. ɂɧɨɝɞɚ ɞɥɹ ɨɫɜɨɟɧɢɹ ɨɛɴɟɤɬɧɨɣ ɦɨɞɟɥɢ ɚɜɬɨɦɚɬɢɡɢɪɭɟɦɨɝɨ ɫɟɪɜɟɪɚ ɦɨɠɧɨ ɧɚɱɚɬɶ ɫ ɡɚɩɢɫɢ ɧɟɨɛɯɨɞɢɦɨɣ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ ɞɟɣɫɬɜɢɣ ɜ ɜɢɞɟ ɦɚɤɪɨɫɚ ɫ ɩɨɦɨɳɶɸ VBȺ. ɋɨɡɞɚɬɶ ɦɚɤɪɨɫ ɦɨɠɧɨ, ɜɵɛɪɚɜ ɢɡ ɦɟɧɸ ɩɪɢɥɨɠɟɧɢɹ Microsoft Office ɩɭɧɤɬ Tools/Macro/Record New Macro. ɉɪɨɫɦɨɬɪ ɩɨɥɭɱɟɧɧɨɝɨ ɦɚɤɪɨɫɚ ɜ ɪɟɞɚɤɬɨɪɟ ɤɨɞɚ VBA (ɪɢɫ. 6.1) ɨɛɵɱɧɨ ɩɨɡɜɨɥɹɟɬ ɩɨɧɹɬɶ, ɤɚɤ ɞɨɥɠɟɧ ɜɵɝɥɹɞɟɬɶ ɤɨɞ, ɪɟɚɥɢɡɭɸɳɢɣ ɷɬɭ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɞɟɣɫɬɜɢɣ.
Ɋɢɫ. 6.1. Ɋɟɞɚɤɬɨɪ ɤɨɞɚ VBA
Ɍɚɤɢɦ ɨɛɪɚɡɨɦ, ɱɬɨɛɵ ɭɡɧɚɬɶ, ɤɚɤ ɪɟɚɥɢɡɨɜɚɬɶ ɤɚɤɢɟ-ɥɢɛɨ ɞɟɣɫɬɜɢɹ, ɢɫɩɨɥɶɡɭɹ ɨɛɴɟɤɬɵ ɚɜɬɨɦɚɬɢɡɚɰɢɢ, ɧɟɨɛɯɨɞɢɦɨ ɜɵɩɨɥɧɢɬɶ ɫɥɟɞɭɸɳɢɟ ɞɟɣɫɬɜɢɹ: 1) ɡɚɩɢɫɚɬɶ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɞɟɣɫɬɜɢɣ ɜ ɦɚɤɪɨɫ; 2) ɪɚɫɫɦɨɬɪɟɬɶ ɩɨɥɭɱɟɧɧɵɣ ɤɨɞ ɜ ɪɟɞɚɤɬɨɪɟ (ɪɢɫ. 1) ɢ ɩɟɪɟɜɟɫɬɢ ɟɝɨ ɧɚ ɹɡɵɤ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ, ɤɨɬɨɪɵɣ ɢɫɩɨɥɶɡɭɟɬɫɹ ɞɥɹ ɪɚɡɪɚɛɨɬɤɢ ɩɪɢɥɨɠɟɧɢɹ; 3) ɞɥɹ ɩɨɢɫɤɚ ɞɨɩɨɥɧɢɬɟɥɶɧɨɣ ɢɧɮɨɪɦɚɰɢɢ ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɢɧɮɨɪɦɚɰɢɨɧɧɨ-ɫɩɪɚɜɨɱɧɭɸ ɫɢɫɬɟɦɭ MSDN [1] ɢɥɢ VBAxxx9.CHM (ɞɥɹ Microsoft Office 97 — VBxxx8.HLP ɫɨɨɬɜɟɬɫɬɜɟɧɧɨ). ɉɪɢɦɟɱɚɧɢɟ 6.1. ɉɚɪɚɦɟɬɪɵ ɧɟɤɨɬɨɪɵɯ ɮɭɧɤɰɢɣ ɫɟɪɜɟɪɨɜ ɚɜɬɨɦɚɬɢɡɚɰɢɢ ɡɚɞɚɸɬɫɹ ɜ ɜɢɞɟ ɤɨɧɫɬɚɧɬ, ɤɨɬɨɪɵɟ ɧɟ ɭɫɬɚɧɨɜɥɟɧɵ ɜ Delphi. ɑɬɨɛɵ ɩɨɥɭɱɢɬɶ ɡɧɚɱɟɧɢɟ ɬɚɤɢɯ ɤɨɧɫɬɚɧɬ ɜ VisualBasic, ɧɚɞɨ ɜɧɭɬɪɢ ɦɚɤɪɨɫɚ ɧɚɩɢɫɚɬɶ MsgBox(<ɧɭɠɧɚɹ_ɤɨɧɫɬɚɧɬɚ>).
6.2. Ɉɛɴɟɤɬɧɵɟ ɦɨɞɟɥɢ Microsoft Office ȼ ɨɛɴɟɤɬɧɵɯ ɦɨɞɟɥɹɯ ɜɫɟɯ ɩɪɢɥɨɠɟɧɢɣ Microsoft Office ɜɫɟɝɞɚ ɢɦɟɟɬɫɹ ɫɚɦɵɣ ɝɥɚɜɧɵɣ ɨɛɴɟɤɬ, ɞɨɫɬɭɩɧɵɣ ɩɪɢɥɨɠɟɧɢɸ-ɤɨɧɬɪɨɥɥɟɪɭ ɢ ɩɪɟɞɫɬɚɜɥɹɸɳɢɣ ɫɚɦɨ ɚɜɬɨɦɚɬɢɡɢɪɭɟɦɨɟ ɩɪɢɥɨɠɟɧɢɟ. Ⱦɥɹ ɜɫɟɯ ɩɪɢɥɨɠɟɧɢɣ Microsoft Office ɨɧ ɧɚɡɵɜɚɟɬɫɹ Application, ɢ ɦɧɨɝɢɟ ɟɝɨ ɫɜɨɣɫɬɜɚ ɢ ɦɟɬɨɞɵ ɞɥɹ ɜɫɟɯ ɷɬɢɯ ɩɪɢɥɨɠɟɧɢɣ ɬɚɤɠɟ ɨɞɢɧɚɤɨɜɵ. ɇɚɢɛɨɥɟɟ ɱɚɫɬɨ ɢɫɩɨɥɶɡɭɸɬɫɹ ɫɥɟɞɭɸɳɢɟ ɢɡ ɧɢɯ. 1. ɋɜɨɣɫɬɜɨ Visible (ɞɨɫɬɭɩɧɨɟ ɞɥɹ ɨɛɴɟɤɬɚ Application ɜɫɟɯ ɩɪɢɥɨɠɟɧɢɣ Microsoft Office) ɩɨɡɜɨɥɹɟɬ ɩɪɢɥɨɠɟɧɢɸ ɩɨɹɜɢɬɶɫɹ ɧɚ ɷɤɪɚɧɟ ɢ ɜ ɩɚɧɟɥɢ ɡɚɞɚɱ; ɨɧɨ ɩɪɢɧɢɦɚɟɬ ɡɧɚɱɟɧɢɹ True (ɩɨɥɶɡɨɜɚɬɟɥɶɫɤɢɣ ɢɧɬɟɪɮɟɣɫ ɩɪɢɥɨɠɟɧɢɹ ɞɨɫɬɭɩɟɧ) ɢɥɢ False (ɩɨɥɶɡɨɜɚɬɟɥɶɫɤɢɣ ɢɧɬɟɪɮɟɣɫ ɩɪɢɥɨɠɟɧɢɹ ɧɟɞɨɫɬɭɩɟɧ; ɷɬɨ ɡɧɚɱɟɧɢɟ ɭɫɬɚɧɚɜɥɢɜɚɟɬɫɹ ɩɨ ɭɦɨɥɱɚɧɢɸ). ȿɫɥɢ ɜɚɦ ɧɭɠɧɨ ɫɞɟɥɚɬɶ ɱɬɨ-ɬɨ ɫ ɞɨɤɭɦɟɧɬɨɦ Office ɜ ɮɨɧɨɜɨɦ ɪɟɠɢɦɟ, ɧɟ ɢɧɮɨɪɦɢɪɭɹ ɨɛ ɷɬɨɦ ɩɨɥɶɡɨɜɚɬɟɥɹ, ɦɨɠɧɨ ɧɟ ɨɛɪɚɳɚɬɶɫɹ ɤ ɷɬɨɦɭ ɫɜɨɣɫɬɜɭ — ɜ ɷɬɨɦ ɫɥɭɱɚɟ ɩɪɢɥɨɠɟɧɢɟ ɦɨɠɧɨ ɛɭɞɟɬ ɧɚɣɬɢ ɬɨɥɶɤɨ ɜ ɫɩɢɫɤɟ ɩɪɨɰɟɫɫɨɜ ɫ ɩɨɦɨɳɶɸ ɩɪɢɥɨɠɟɧɢɹ Task Manager. 2. Ɇɟɬɨɞ Quit ɡɚɤɪɵɜɚɟɬ ɩɪɢɥɨɠɟɧɢɟ Office. ȼ ɡɚɜɢɫɢɦɨɫɬɢ ɨɬ ɬɨɝɨ, ɤɚɤɨɟ ɩɪɢɥɨɠɟɧɢɟ Office ɚɜɬɨɦɚɬɢɡɢɪɭɟɬɫɹ, ɨɧɨ ɦɨɠɟɬ ɢɦɟɬɶ ɩɚɪɚɦɟɬɪɵ ɢɥɢ ɧɟ ɢɦɟɬɶ ɬɚɤɨɜɵɯ. ɉɪɢɦɟɱɚɧɢɟ 6.2. ȼ ɧɟɤɨɬɨɪɵɯ ɩɪɨɝɪɚɦɦɚɯ ɦɟɬɨɞ Quit ɦɨɠɟɬ ɢɦɟɬɶ ɩɚɪɚɦɟɬɪɵ (ɧɚɩɪɢɦɟɪ, ɜ Microsoft Word), ɨɞɧɚɤɨ ɩɪɢ ɩɨɡɞɧɟɦ ɫɜɹɡɵɜɚɧɢɢ ɢɯ ɦɨɠɧɨ ɧɟ ɭɱɢɬɵɜɚɬɶ.
6.3. Ɉɛɳɢɟ ɩɪɢɧɰɢɩɵ ɫɨɡɞɚɧɢɹ ɤɨɧɬɪɨɥɥɟɪɨɜ ɚɜɬɨɦɚɬɢɡɚɰɢɢ ɉɪɢɥɨɠɟɧɢɟ, ɜ ɤɨɬɨɪɨɦ ɢɫɩɨɥɶɡɭɸɬɫɹ ɫɟɪɜɟɪɵ ɚɜɬɨɦɚɬɢɡɚɰɢɢ, ɞɚɥɟɟ ɛɭɞɭɬ ɧɚɡɵɜɚɬɶɫɹ ɤɨɧɬɪɨɥɥɟɪɚɦɢ ɚɜɬɨɦɚɬɢɡɚɰɢɢ. ȼ ɨɛɳɟɦ ɫɥɭɱɚɟ ɤɨɧɬɪɨɥɥɟɪ ɚɜɬɨɦɚɬɢɡɚɰɢɢ ɞɨɥɠɟɧ ɜɵɩɨɥɧɹɬɶ ɫɥɟɞɭɸɳɢɟ ɞɟɣɫɬɜɢɹ: 1) ɩɪɨɜɟɪɢɬɶ, ɡɚɩɭɳɟɧɚ ɥɢ ɤɨɩɢɹ ɩɪɢɥɨɠɟɧɢɹ-ɫɟɪɜɟɪɚ; 2) ɜ ɡɚɜɢɫɢɦɨɫɬɢ ɨɬ ɪɟɡɭɥɶɬɚɬɨɜ ɩɪɨɜɟɪɤɢ ɡɚɩɭɫɬɢɬɶ ɤɨɩɢɸ ɚɜɬɨɦɚɬɢɡɢɪɭɟɦɨɝɨ ɩɪɢɥɨɠɟɧɢɹ Office ɥɢɛɨ ɩɨɞɤɥɸɱɢɬɶɫɹ ɤ ɭɠɟ ɢɦɟɸɳɟɣɫɹ ɤɨɩɢɢ; 3) ɫɞɟɥɚɬɶ ɨɤɧɨ ɩɪɢɥɨɠɟɧɢɹ-ɫɟɪɜɟɪɚ ɜɢɞɢɦɵɦ (ɜ ɨɛɳɟɦ ɫɥɭɱɚɟ ɷɬɨ ɧɟ ɨɛɹɡɚɬɟɥɶɧɨ); 4) ɜɵɩɨɥɧɢɬɶ ɤɚɤɢɟ-ɬɨ ɞɟɣɫɬɜɢɹ ɫ ɩɪɢɥɨɠɟɧɢɟɦ-ɫɟɪɜɟɪɨɦ (ɧɚɩɪɢɦɟɪ, ɫɨɡɞɚɬɶ ɢɥɢ ɨɬɤɪɵɬɶ ɞɨɤɭɦɟɧɬɵ, ɢɡɦɟɧɢɬɶ ɢɯ ɞɚɧɧɵɟ, ɫɨɯɪɚɧɢɬɶ ɞɨɤɭɦɟɧɬɵ ɢ ɩɪ.); 5) ɡɚɤɪɵɬɶ ɩɪɢɥɨɠɟɧɢɟ-ɫɟɪɜɟɪ, ɟɫɥɢ ɟɝɨ ɤɨɩɢɹ ɛɵɥɚ ɡɚɩɭɳɟɧɚ ɞɚɧɧɵɦ ɤɨɧɬɪɨɥɥɟɪɨɦ, ɢɥɢ ɨɬɤɥɸɱɢɬɶɫɹ ɨɬ ɧɟɝɨ, ɟɫɥɢ ɤɨɧɬɪɨɥɥɟɪ ɩɨɞɤɥɸɱɢɥɫɹ ɤ ɭɠɟ ɢɦɟɸɳɟɣɫɹ ɤɨɩɢɢ. Ⱦɥɹ ɷɬɢɯ ɰɟɥɟɣ ɦɨɠɧɨ ɜɨɫɩɨɥɶɡɨɜɚɬɶɫɹ ɮɭɧɤɰɢɹɦɢ GetActiveOleObject ɞɥɹ ɩɨɞɤɥɸɱɟɧɢɹ ɤ ɭɠɟ ɡɚɩɭɳɟɧɧɨɣ ɤɨɩɢɢ ɩɪɢɥɨɠɟɧɢɹ-ɫɟɪɜɟɪɚ ɢ CreateOleObject ɞɥɹ ɡɚɩɭɫɤɚ ɧɨɜɨɣ, ɟɫɥɢ ɫɟɪɜɟɪ ɧɟ ɡɚɩɭɳɟɧ. ɇɚɩɪɢɦɟɪ, ɱɬɨɛɵ ɫɨɡɞɚɬɶ ɧɨɜɵɣ ɷɤɡɟɦɩɥɹɪ COM-ɫɟɪɜɟɪɚ ɩɪɢɥɨɠɟɧɢɹ Word, ɧɟɨɛɯɨɞɢɦɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɫɥɟɞɭɸɳɢɣ ɤɨɞ: App := CreateOleObject(‘App.Application’); ȼ ɪɟɡɭɥɶɬɚɬɟ ɜ ɩɟɪɟɦɟɧɧɭɸ ɜɚɪɢɚɧɬɧɨɝɨ ɬɢɩɚ App ɛɭɞɟɬ ɩɟɪɟɞɚɧ ɨɫɧɨɜɧɨɣ ɢɧɬɟɪɮɟɣɫ ɩɪɢɥɨɠɟɧɢɹ.
6.4. Ⱥɜɬɨɦɚɬɢɡɚɰɢɹ Microsoft Word ȼ ɞɚɧɧɨɦ ɪɚɡɞɟɥɟ ɦɵ ɨɛɫɭɞɢɦ ɧɚɢɛɨɥɟɟ ɱɚɫɬɨ ɜɫɬɪɟɱɚɸɳɢɟɫɹ ɡɚɞɚɱɢ, ɫɜɹɡɚɧɧɵɟ ɫ ɚɜɬɨɦɚɬɢɡɚɰɢɟɣ Microsoft Word. ɇɨ ɩɟɪɟɞ ɷɬɢɦ ɪɚɫɫɦɨɬɪɢɦ, ɤɚɤɨɜɵ ɩɪɨɝɪɚɦɦɧɵɟ ɢɞɟɧɬɢɮɢɤɚɬɨɪɵ ɨɫɧɨɜɧɵɯ ɨɛɴɟɤɬɨɜ Microsoft Word ɢ ɱɬɨ ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ ɟɝɨ ɨɛɴɟɤɬɧɚɹ ɦɨɞɟɥɶ. 6.4.1. ɉɪɨɝɪɚɦɦɧɵɟ ɢɞɟɧɬɢɮɢɤɚɬɨɪɵ ɢ ɨɛɴɟɤɬɧɚɹ ɦɨɞɟɥɶ Microsoft Word Ⱦɥɹ ɩɪɢɥɨɠɟɧɢɹ-ɤɨɧɬɪɨɥɥɟɪɚ ɞɨɫɬɭɩɧɵ ɧɟɩɨɫɪɟɞɫɬɜɟɧɧɨ ɫɥɟɞɭɸɳɢɟ ɨɛɴɟɤɬɵ:
57
58
Ɉɛɴɟɤɬ Application Document
ɉɪɨɝɪɚɦɦɧɵɣ ɢɞɟɧɬɢɮɢɤɚɬɨɪ Word.Application, Word.Application.9 Word.Document, Word.Document.9, Word.Template.8
Ʉɨɦɦɟɧɬɚɪɢɣ ɫɨɡɞɚɟɬɫɹ ɷɤɡɟɦɩɥɹɪ Word ɛɟɡ ɨɬɤɪɵɬɵɯ ɞɨɤɭɦɟɧɬɨɜ ɫɨɡɞɚɟɬɫɹ ɷɤɡɟɦɩɥɹɪ Word ɫ ɨɞɧɢɦ ɜɧɨɜɶ ɫɨɡɞɚɧɧɵɦ ɞɨɤɭɦɟɧɬɨɦ
ȼɫɟ ɨɫɬɚɥɶɧɵɟ ɨɛɴɟɤɬɵ Word ɹɜɥɹɸɬɫɹ ɬɚɤ ɧɚɡɵɜɚɟɦɵɦɢ ɜɧɭɬɪɟɧɧɢɦɢ ɨɛɴɟɤɬɚɦɢ. ɗɬɨ ɨɡɧɚɱɚɟɬ, ɱɬɨ ɨɧɢ ɧɟ ɦɨɝɭɬ ɛɵɬɶ ɫɨɡɞɚɧɵ ɫɚɦɢ ɩɨ ɫɟɛɟ; ɬɚɤ, ɨɛɴɟɤɬ Paragraph (ɚɛɡɚɰ) ɧɟ ɦɨɠɟɬ ɛɵɬɶ ɫɨɡɞɚɧ ɨɬɞɟɥɶɧɨ ɨɬ ɫɨɞɟɪɠɚɳɟɝɨ ɟɝɨ ɞɨɤɭɦɟɧɬɚ. ȿɫɥɢ ɜɫɩɨɦɧɢɬɶ, ɱɬɨ ɨɫɧɨɜɧɨɟ ɧɚɡɧɚɱɟɧɢɟ Word — ɪɚɛɨɬɚ ɫ ɞɨɤɭɦɟɧɬɚɦɢ, ɦɨɠɧɨ ɥɟɝɤɨ ɩɨɧɹɬɶ ɢɟɪɚɪɯɢɸ ɟɝɨ ɨɛɴɟɤɬɧɨɣ ɦɨɞɟɥɢ (ɪɢɫ. 2).
6.4.2. ɋɨɡɞɚɧɢɟ ɢ ɨɬɤɪɵɬɢɟ ɞɨɤɭɦɟɧɬɨɜ Microsoft Word Ⱦɥɹ ɫɨɡɞɚɧɢɹ ɩɪɢɦɟɪɨɜ ɢɫɩɨɥɶɡɨɜɚɧɢɹ Microsoft Word ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɤɨɞ ɫɨɡɞɚɧɢɹ ɤɨɧɬɪɨɥɥɟɪɚ, ɩɪɢɜɟɞɟɧɧɵɣ ɜɵɲɟ, ɢ ɦɨɞɢɮɢɰɢɪɨɜɚɬɶ ɟɝɨ, ɡɚɦɟɧɹɹ ɤɨɦɦɟɧɬɚɪɢɢ ɤɨɞɨɦ, ɦɚɧɢɩɭɥɢɪɭɸɳɢɦ ɫɜɨɣɫɬɜɚɦɢ ɢ ɦɟɬɨɞɚɦɢ ɨɛɴɟɤɬɚ Word.Application. Ɇɵ ɧɚɱɧɟɦ ɫ ɫɨɡɞɚɧɢɹ ɢ ɨɬɤɪɵɬɢɹ ɞɨɤɭɦɟɧɬɨɜ. ɋɨɡɞɚɬɶ ɧɨɜɵɣ ɞɨɤɭɦɟɧɬ Word ɦɨɠɧɨ, ɢɫɩɨɥɶɡɭɹ ɦɟɬɨɞ Add ɤɨɥɥɟɤɰɢɢ Documents ɨɛɴɟɤɬɚ Application: App.Documents.Add; ɑɬɨɛɵ ɫɨɡɞɚɬɶ ɧɟɫɬɚɧɞɚɪɬɧɵɣ ɞɨɤɭɦɟɧɬ, ɧɭɠɧɨ ɭɤɚɡɚɬɶ ɢɦɹ ɲɚɛɥɨɧɚ ɜ ɤɚɱɟɫɬɜɟ ɩɚɪɚɦɟɬɪɚ ɦɟɬɨɞɚ Add: App.Documents.Add(‘C:\Program Files\ Microsoft Office\Templates\1033\Manual.dot’); Ⱦɥɹ ɨɬɤɪɵɬɢɹ ɭɠɟ ɫɭɳɟɫɬɜɭɸɳɟɝɨ ɞɨɤɭɦɟɧɬɚ ɫɥɟɞɭɟɬ ɜɨɫɩɨɥɶɡɨɜɚɬɶɫɹ ɦɟɬɨɞɨɦ Open ɤɨɥɥɟɤɰɢɢ Documents: App.Documents.Open(‘C:\MyWordFile.doc’); Ɉɬɦɟɬɢɦ, ɱɬɨ ɫɜɨɣɫɬɜɨ ActiveDocument ɨɛɴɟɤɬɚ Word.Application ɭɤɚɡɵɜɚɟɬ ɧɚ ɬɟɤɭɳɢɣ ɚɤɬɢɜɧɵɣ ɞɨɤɭɦɟɧɬ ɫɪɟɞɢ ɨɞɧɨɝɨ ɢɥɢ ɧɟɫɤɨɥɶɤɢɯ ɨɬɤɪɵɬɵɯ. ɉɨɦɢɦɨ ɷɬɨɝɨ ɤ ɞɨɤɭɦɟɧɬɭ ɦɨɠɧɨ ɨɛɪɚɳɚɬɶɫɹ ɩɨ ɟɝɨ ɩɨɪɹɞɤɨɜɨɦɭ ɧɨɦɟɪɭ ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɚ Item; ɧɚɩɪɢɦɟɪ ɤɨ ɜɬɨɪɨɦɭ ɨɬɤɪɵɬɨɦɭ ɞɨɤɭɦɟɧɬɭ ɦɨɠɧɨ ɨɛɪɚɬɢɬɶɫɹ ɬɚɤ: App.Documents.Item(2) ɉɪɢɦɟɱɚɧɢɟ 6.3. ɇɭɦɟɪɚɰɢɹ ɱɥɟɧɨɜ ɤɨɥɥɟɤɰɢɣ ɜ Microsoft Office ɧɚɱɢɧɚɟɬɫɹ ɫ ɟɞɢɧɢɰɵ.
Ɋɢɫ. 6.2. Ɏɪɚɝɦɟɧɬ ɨɛɴɟɤɬɧɨɣ ɦɨɞɟɥɢ Microsoft Word Ɉɫɧɨɜɧɵɦ ɨɛɴɟɤɬɨɦ ɜ ɧɟɣ, ɤɚɤ ɢ ɜɨ ɜɫɟɯ ɞɪɭɝɢɯ ɩɪɢɥɨɠɟɧɢɹɯ Microsoft Office, ɹɜɥɹɟɬɫɹ ɨɛɴɟɤɬ Application, ɫɨɞɟɪɠɚɳɢɣ ɤɨɥɥɟɤɰɢɸ Documents ɨɛɴɟɤɬɨɜ ɬɢɩɚ Document. Ʉɚɠɞɵɣ ɨɛɴɟɤɬ ɬɢɩɚ Document ɫɨɞɟɪɠɢɬ ɤɨɥɥɟɤɰɢɸ Paragraphs ɨɛɴɟɤɬɨɜ ɬɢɩɚ Paragraph, Bookmarks ɬɢɩɚ Bookmark, Characters ɬɢɩɚ Character ɢ ɬ.ɞ. Ɇɚɧɢɩɭɥɹɰɢɹ ɞɨɤɭɦɟɧɬɚɦɢ, ɚɛɡɚɰɚɦɢ, ɫɢɦɜɨɥɚɦɢ, ɡɚɤɥɚɞɤɚɦɢ ɪɟɚɥɶɧɨ ɨɫɭɳɟɫɬɜɥɹɟɬɫɹ ɩɭɬɟɦ ɨɛɪɚɳɟɧɢɹ ɤ ɫɜɨɣɫɬɜɚɦ ɢ ɦɟɬɨɞɚɦ ɷɬɢɯ ɨɛɴɟɤɬɨɜ.
ɋɞɟɥɚɬɶ ɞɨɤɭɦɟɧɬ ɚɤɬɢɜɧɵɦ ɦɨɠɧɨ ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɚ Activate: App.Documents.Item(1).Activate; ɋɥɟɞɭɸɳɟɟ, ɱɟɦɭ ɫɥɟɞɭɟɬ ɧɚɭɱɢɬɶɫɹ, – ɷɬɨ ɫɨɯɪɚɧɹɬɶ ɞɨɤɭɦɟɧɬ Word ɢ ɡɚɤɪɵɜɚɬɶ ɫɚɦ Word. 6.4.3. ɋɨɯɪɚɧɟɧɢɟ, ɩɟɱɚɬɶ ɢ ɡɚɤɪɵɬɢɟ ɞɨɤɭɦɟɧɬɨɜ Microsoft Word Ɂɚɤɪɵɬɢɟ ɞɨɤɭɦɟɧɬɚ ɦɨɠɟɬ ɛɵɬɶ ɨɫɭɳɟɫɬɜɥɟɧɨ ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɚ Close: App.Documents.Item(2).Close; ɢɥɢ App.ActiveDocument.Close;
59
60
Ɇɟɬɨɞ Close ɢɦɟɟɬ ɧɟɫɤɨɥɶɤɨ ɧɟɨɛɹɡɚɬɟɥɶɧɵɯ (ɜ ɫɥɭɱɚɟ ɩɨɡɞɧɟɝɨ ɫɜɹɡɵɜɚɧɢɹ) ɩɚɪɚɦɟɬɪɨɜ, ɜɥɢɹɸɳɢɯ ɧɚ ɩɪɚɜɢɥɚ ɫɨɯɪɚɧɟɧɢɹ ɞɨɤɭɦɟɧɬɚ. ɉɟɪɜɵɣ ɢɡ ɧɢɯ ɜɥɢɹɟɬ ɧɚ ɬɨ, ɫɨɯɪɚɧɹɸɬɫɹ ɥɢ ɜɧɟɫɟɧɧɵɟ ɜ ɞɨɤɭɦɟɧɬ ɢɡɦɟɧɟɧɢɹ, ɢ ɩɪɢɧɢɦɚɟɬ ɬɪɢ ɜɨɡɦɨɠɧɵɯ ɡɧɚɱɟɧɢɹ (ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɟ ɤɨɧɫɬɚɧɬɵ ɪɟɤɨɦɟɧɞɭɟɬɫɹ ɨɩɢɫɚɬɶ ɜ ɩɪɢɥɨɠɟɧɢɢ). Ɍɪɟɬɢɣ ɩɚɪɚɦɟɬɪ ɩɪɢɧɢɦɚɟɬ ɡɧɚɱɟɧɢɹ True ɢɥɢ False ɢ ɜɥɢɹɟɬ ɧɚ ɬɨ, ɩɟɪɟɫɵɥɚɬɶ ɥɢ ɞɨɤɭɦɟɧɬ ɫɥɟɞɭɸɳɟɦɭ ɩɨɥɶɡɨɜɚɬɟɥɸ ɩɨ ɷɥɟɤɬɪɨɧɧɨɣ ɩɨɱɬɟ. ȿɫɥɢ ɷɬɚ ɮɭɧɤɰɢɨɧɚɥɶɧɨɫɬɶ ɧɟ ɩɪɢɦɟɧɹɟɬɫɹ, ɦɨɠɧɨ ɩɪɨɢɝɧɨɪɢɪɨɜɚɬɶ ɷɬɨɬ ɩɚɪɚɦɟɬɪ. Ɍɚɤɢɦ ɨɛɪɚɡɨɦ, ɩɪɢ ɢɫɩɨɥɶɡɨɜɚɧɢɢ ɷɬɢɯ ɩɚɪɚɦɟɬɪɨɜ ɡɚɤɪɵɬɶ ɞɨɤɭɦɟɧɬ ɦɨɠɧɨ, ɧɚɩɪɢɦɟɪ, ɬɚɤ: App.ActiveDocument.Close(wdSaveChanges, wdPromptUser) ; ɉɪɨɫɬɨ ɫɨɯɪɚɧɢɬɶ ɞɨɤɭɦɟɧɬ, ɧɟ ɡɚɤɪɵɜɚɹ ɟɝɨ, ɦɨɠɧɨ ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɚ Save: App.ActiveDocument.Save; ɗɬɨɬ ɦɟɬɨɞ ɬɚɤɠɟ ɢɦɟɟɬ ɧɟɫɤɨɥɶɤɨ ɧɟɨɛɹɡɚɬɟɥɶɧɵɯ (ɜ ɫɥɭɱɚɟ ɩɨɡɞɧɟɝɨ ɫɜɹɡɵɜɚɧɢɹ) ɩɚɪɚɦɟɬɪɨɜ, ɩɟɪɜɵɣ ɢɡ ɤɨɬɨɪɵɯ ɪɚɜɟɧ True, ɟɫɥɢ ɞɨɤɭɦɟɧɬ ɫɨɯɪɚɧɹɟɬɫɹ ɚɜɬɨɦɚɬɢɱɟɫɤɢ, ɢ False, ɟɫɥɢ ɧɭɠɧɨ ɜɵɜɨɞɢɬɶ ɞɢɚɥɨɝɨɜɭɸ ɩɚɧɟɥɶ ɞɥɹ ɩɨɥɭɱɟɧɢɹ ɩɨɞɬɜɟɪɠɞɟɧɢɹ ɩɨɥɶɡɨɜɚɬɟɥɹ ɨ ɫɨɯɪɚɧɟɧɢɢ ɢɡɦɟɧɟɧɢɣ (ɟɫɥɢ ɬɚɤɨɜɵɟ ɛɵɥɢ ɫɞɟɥɚɧɵ). ȼɬɨɪɨɣ ɩɚɪɚɦɟɬɪ ɜɥɢɹɟɬ ɧɚ ɮɨɪɦɚɬ ɫɨɯɪɚɧɹɟɦɨɝɨ ɞɨɤɭɦɟɧɬɚ, ɢ ɫɩɢɫɨɤ ɟɝɨ ɜɨɡɦɨɠɧɵɯ ɡɧɚɱɟɧɢɣ ɫɨɜɩɚɞɚɟɬ ɫɨ ɫɩɢɫɤɨɦ ɡɧɚɱɟɧɢɣ ɜɬɨɪɨɝɨ ɩɚɪɚɦɟɬɪɚ ɦɟɬɨɞɚ Close. ɇɚɩɨɦɢɧɚɟɦ, ɱɬɨ ɡɚɤɪɵɬɶ ɫɚɦ Word ɦɨɠɧɨ ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɚ Quit ɨɛɴɟɤɬɚ Word.Application. ɗɬɨɬ ɦɟɬɨɞ ɢɦɟɟɬ ɜ ɨɛɳɟɦ ɫɥɭɱɚɟ ɬɪɢ ɩɚɪɚɦɟɬɪɚ, ɫɨɜɩɚɞɚɸɳɢɯ ɫ ɩɚɪɚɦɟɬɪɚɦɢ ɦɟɬɨɞɚ Close ɨɛɴɟɤɬɚ Document. ȼɵɜɨɞ ɞɨɤɭɦɟɧɬɚ ɧɚ ɭɫɬɪɨɣɫɬɜɨ ɩɟɱɚɬɢ ɦɨɠɧɨ ɨɫɭɳɟɫɬɜɢɬɶ ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɚ PrintOut ɨɛɴɟɤɬɚ Document, ɧɚɩɪɢɦɟɪ: App.ActiveDocument.PrintOut; ȿɫɥɢ ɧɭɠɧɨ ɢɡɦɟɧɢɬɶ ɩɚɪɚɦɟɬɪɵ ɩɟɱɚɬɢ, ɫɥɟɞɭɟɬ ɭɤɚɡɚɬɶ ɡɧɚɱɟɧɢɹ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɯ ɩɚɪɚɦɟɬɪɨɜ ɦɟɬɨɞɚ PrintOut (ɜ ɫɥɭɱɚɟ Microsoft Word ɢɯ ɨɤɨɥɨ ɞɜɚɞɰɚɬɢ). 6.4.4. ȼɫɬɚɜɤɚ ɬɟɤɫɬɚ ɢ ɨɛɴɟɤɬɨɜ ɜ ɞɨɤɭɦɟɧɬ ɢ ɮɨɪɦɚɬɢɪɨɜɚɧɢɟ ɬɟɤɫɬɚ Ⱦɥɹ ɫɨɡɞɚɧɢɹ ɚɛɡɚɰɟɜ ɜ ɞɨɤɭɦɟɧɬɟ ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɤɨɥɥɟɤɰɢɸ Paragraphs ɨɛɴɟɤɬɚ Document, ɩɪɟɞɫɬɚɜɥɹɸɳɭɸ ɧɚɛɨɪ ɚɛɡɚɰɟɜ ɞɚɧɧɨɝɨ ɞɨɤɭɦɟɧɬɚ. Ⱦɨɛɚɜɢɬɶ ɧɨɜɵɣ ɚɛɡɚɰ ɦɨɠɧɨ ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɚ Add ɷɬɨɣ ɤɨɥɥɟɤɰɢɢ: App.ActiveDocument.Paragraphs.Add; Ⱦɥɹ ɜɫɬɚɜɤɢ ɫɨɛɫɬɜɟɧɧɨ ɬɟɤɫɬɚ ɜ ɞɨɤɭɦɟɧɬ, ɬɟɦ ɧɟ ɦɟɧɟɟ, ɩɪɢɦɟɧɹɟɬɫɹ ɧɟ ɨɛɴɟɤɬ Paragraph, ɚ ɨɛɴɟɤɬ Range, ɩɪɟɞɫɬɚɜɥɹɸɳɢɣ ɥɸɛɭɸ ɧɟɩɪɟɪɵɜɧɭɸ ɱɚɫɬɶ ɞɨɤɭɦɟɧɬɚ (ɜ ɬɨɦ ɱɢɫɥɟ ɢ ɜɧɨɜɶ ɫɨɡɞɚɧɧɵɣ ɚɛɡɚɰ). ɗɬɨɬ ɨɛɴɟɤɬ ɦɨɠɟɬ ɛɵɬɶ ɫɨɡɞɚɧ ɪɚɡɧɵɦɢ ɫɩɨɫɨɛɚɦɢ. ɇɚɩɪɢɦɟɪ, ɦɨɠɧɨ ɭɤɚɡɚɬɶ 61
ɧɚɱɚɥɶɧɵɣ ɢ ɤɨɧɟɱɧɵɣ ɫɢɦɜɨɥɵ ɞɢɚɩɚɡɨɧɚ (ɟɫɥɢ ɬɚɤɨɜɵɟ ɢɦɟɸɬɫɹ ɜ ɞɨɤɭɦɟɧɬɟ): var Rng : Variant; ... Rng := App.ActiveDocument.Range(2,4); //ɫɨ 2-ɝɨ ɩɨ 4-ɣ ɫɢɦɜɨɥɵ ɢɥɢ ɭɤɚɡɚɬɶ ɧɨɦɟɪ ɚɛɡɚɰɚ (ɧɚɩɪɢɦɟɪ, ɬɨɥɶɤɨ ɱɬɨ ɫɨɡɞɚɧɧɨɝɨ): Rng:= App.ActiveDocument.Paragraphs.Item(1).Range; ɢɥɢ ɭɤɚɡɚɬɶ ɧɟɫɤɨɥɶɤɨ ɚɛɡɚɰɟɜ, ɫɥɟɞɭɸɳɢɯ ɩɨɞɪɹɞ: Rng := App.ActiveDocument.Range (App.ActiveDocument.Paragraphs.Item(3).Range.Start, App.ActiveDocument.Paragraphs.Item(5).Range.End) ȼɫɬɚɜɢɬɶ ɬɟɤɫɬ ɦɨɠɧɨ ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɨɜ ɨɛɴɟɤɬɚ Range InsertBefore (ɩɟɪɟɞ ɞɢɚɩɚɡɨɧɨɦ) ɢɥɢ InsertAfter (ɩɨɫɥɟ ɞɢɚɩɚɡɨɧɚ), ɧɚɩɪɢɦɟɪ: Rng.InsertAfter(‘ɗɬɨ ɜɫɬɚɜɥɹɟɦɵɣ ɬɟɤɫɬ’); ɉɨɦɢɦɨ ɨɛɴɟɤɬɚ Range ɬɟɤɫɬ ɦɨɠɧɨ ɜɫɬɚɜɥɹɬɶ ɫ ɩɨɦɨɳɶɸ ɨɛɴɟɤɬɚ Selection, ɹɜɥɹɸɳɟɝɨɫɹ ɫɜɨɣɫɬɜɨɦ ɨɛɴɟɤɬɚ Word.Application ɢ ɩɪɟɞɫɬɚɜɥɹɸɳɟɝɨ ɫɨɛɨɣ ɜɵɞɟɥɟɧɧɭɸ ɱɚɫɬɶ ɞɨɤɭɦɟɧɬɚ (ɷɬɨɬ ɨɛɴɟɤɬ ɫɨɡɞɚɟɬɫɹ, ɟɫɥɢ ɩɨɥɶɡɨɜɚɬɟɥɶ ɜɵɞɟɥɹɟɬ ɱɚɫɬɶ ɞɨɤɭɦɟɧɬɚ ɫ ɩɨɦɨɳɶɸ ɦɵɲɢ, ɢ ɦɨɠɟɬ ɛɵɬɶ ɬɚɤɠɟ ɫɨɡɞɚɧ ɫ ɩɨɦɨɳɶɸ ɩɪɢɥɨɠɟɧɢɹ-ɤɨɧɬɪɨɥɥɟɪɚ). ɋɚɦ ɨɛɴɟɤɬ Selection ɦɨɠɧɨ ɫɨɡɞɚɬɶ, ɩɪɢɦɟɧɢɜ ɦɟɬɨɞ Select ɤ ɨɛɴɟɤɬɭ Range, ɧɚɩɪɢɦɟɪ: var Sel : Variant; ... App.ActiveDocument.Paragraphs.Item(3).Range.Select; ȼ ɩɪɢɜɟɞɟɧɧɨɦ ɜɵɲɟ ɩɪɢɦɟɪɟ ɜ ɬɟɤɭɳɟɦ ɞɨɤɭɦɟɧɬɟ ɜɵɞɟɥɹɟɬɫɹ ɬɪɟɬɢɣ ɚɛɡɚɰ. ȿɫɥɢ ɦɵ ɯɨɬɢɦ ɜɫɬɚɜɢɬɶ ɫɬɪɨɤɭ ɬɟɤɫɬɚ ɜ ɞɨɤɭɦɟɧɬ ɥɢɛɨ ɜɦɟɫɬɨ ɜɵɞɟɥɟɧɧɨɝɨ ɮɪɚɝɦɟɧɬɚ ɬɟɤɫɬɚ, ɥɢɛɨ ɩɟɪɟɞ ɧɢɦ, ɷɬɨ ɦɨɠɧɨ ɫɞɟɥɚɬɶ ɫ ɩɨɦɨɳɶɸ ɫɥɟɞɭɸɳɟɝɨ ɮɪɚɝɦɟɧɬɚ ɤɨɞɚ: var Sel : Variant; ... Sel := App.Selection; Sel.TypeText(‘ɗɬɨ ɬɟɤɫɬ, ɤɨɬɨɪɵɦ ɦɵ ɡɚɦɟɧɢɦ ɜɵɞɟɥɟɧɧɵɣ ɮɪɚɝɦɟɧɬ’); Ɉɬɦɟɬɢɦ, ɱɬɨ ɟɫɥɢ ɫɜɨɣɫɬɜɨ Options.ReplaceSelection ɨɛɴɟɤɬɚ Word.Application ɪɚɜɧɨ True, ɜɵɞɟɥɟɧɧɵɣ ɬɟɤɫɬ ɛɭɞɟɬ ɡɚɦɟɧɟɧ ɧɚ ɧɨɜɵɣ 62
ɬɟɤɫɬ (ɷɬɨɬ ɪɟɠɢɦ ɞɟɣɫɬɜɭɟɬ ɩɨ ɭɦɨɥɱɚɧɢɸ); ɟɫɥɢ ɠɟ ɧɭɠɧɨ, ɱɬɨɛɵ ɬɟɤɫɬ ɛɵɥ ɜɫɬɚɜɥɟɧ ɩɟɪɟɞ ɜɵɞɟɥɟɧɧɵɦ ɮɪɚɝɦɟɧɬɨɦ, ɚ ɧɟ ɜɦɟɫɬɨ ɧɟɝɨ, ɫɥɟɞɭɟɬ ɭɫɬɚɧɨɜɢɬɶ ɷɬɨ ɫɜɨɣɫɬɜɨ ɪɚɜɧɵɦ False: App.Options.ReplaceSelection := False; ɋɢɦɜɨɥ ɤɨɧɰɚ ɚɛɡɚɰɚ ɩɪɢ ɢɫɩɨɥɶɡɨɜɚɧɢɢ ɨɛɴɟɤɬɚ Selection ɦɨɠɟɬ ɛɵɬɶ ɜɫɬɚɜɥɟɧ ɫ ɩɨɦɨɳɶɸ ɫɥɟɞɭɸɳɟɝɨ ɮɪɚɝɦɟɧɬɚ ɤɨɞɚ: Sel.TypeParagraph; Ʉ ɨɛɴɟɤɬɭ Selection, ɬɚɤ ɠɟ ɤɚɤ ɢ ɤ ɨɛɴɟɤɬɭ Range, ɦɨɠɧɨ ɩɪɢɦɟɧɢɬɶ ɦɟɬɨɞɵ InsertBefore ɢ InsertAfter. ȼ ɷɬɨɦ ɫɥɭɱɚɟ, ɜ ɨɬɥɢɱɢɟ ɨɬ ɩɪɟɞɵɞɭɳɟɝɨ, ɜɫɬɚɜɥɹɟɦɵɣ ɬɟɤɫɬ ɫɬɚɧɟɬ ɱɚɫɬɶɸ ɜɵɞɟɥɟɧɧɨɝɨ ɮɪɚɝɦɟɧɬɚ ɬɟɤɫɬɚ. ɋ ɩɨɦɨɳɶɸ ɨɛɴɟɤɬɚ Selection, ɢɫɩɨɥɶɡɭɹ ɟɝɨ ɫɜɨɣɫɬɜɨ Font ɢ ɫɜɨɣɫɬɜɚ ɨɛɴɟɤɬɚ Font, ɬɚɤɢɟ ɤɚɤ Bold, Italic, Size, …, ɦɨɠɧɨ ɨɬɮɨɪɦɚɬɢɪɨɜɚɬɶ ɬɟɤɫɬ. ɇɚɩɪɢɦɟɪ, ɬɚɤɢɦ ɨɛɪɚɡɨɦ ɦɨɠɧɨ ɜɫɬɚɜɢɬɶ ɫɬɪɨɤɭ, ɜɵɞɟɥɟɧɧɭɸ ɠɢɪɧɵɦ ɲɪɢɮɬɨɦ: Sel.Font.Bold := True; Sel.TypeText(‘ɗɬɨ ɬɟɤɫɬ, ɤɨɬɨɪɵɣ ɦɵ ɜɵɞɟɥɢɦ ɠɢɪɧɵɦ ɲɪɢɮɬɨɦ.’); Sel.Font.Bold := False; Sel.TypeParagraph; Ⱦɥɹ ɧɚɥɨɠɟɧɢɹ ɧɚ ɜɫɬɚɜɥɹɟɦɵɣ ɬɟɤɫɬ ɨɩɪɟɞɟɥɟɧɧɨɝɨ ɡɚɪɚɧɟɟ ɫɬɢɥɹ ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɫɜɨɣɫɬɜɨ Style ɷɬɨɝɨ ɠɟ ɨɛɴɟɤɬɚ, ɧɚɩɪɢɦɟɪ: Sel.Style := ‘Heading 1’; Sel.TypeText(‘ɗɬɨ ɬɟɤɫɬ, ɤɨɬɨɪɵɣ ɫɬɚɧɟɬ ɡɚɝɨɥɨɜɤɨɦ’); Sel.TypeParagraph; ɇɟɪɟɞɤɨ ɞɨɤɭɦɟɧɬɵ Word ɫɨɞɟɪɠɚɬ ɞɚɧɧɵɟ ɞɪɭɝɢɯ ɩɪɢɥɨɠɟɧɢɣ. ɉɪɨɫɬɟɣɲɢɣ ɫɩɨɫɨɛ ɜɫɬɚɜɢɬɶ ɬɚɤɢɟ ɞɚɧɧɵɟ ɜ ɞɨɤɭɦɟɧɬ – ɢɫɩɨɥɶɡɨɜɚɬɶ ɦɟɬɨɞ Paste ɨɛɴɟɤɬɚ Range: Var Rng: Variant; ... Rng := App.Selection.Range; Rng.Collapse(wdCollapseEnd); Rng.Paste; ȿɫɬɟɫɬɜɟɧɧɨ, ɜ ɷɬɨɦ ɫɥɭɱɚɟ ɜ ɛɭɮɟɪɟ ɨɛɦɟɧɚ ɭɠɟ ɞɨɥɠɧɵ ɫɨɞɟɪɠɚɬɶɫɹ ɜɫɬɚɜɥɹɟɦɵɟ ɞɚɧɧɵɟ. ȿɫɥɢ ɧɭɠɧɨ ɩɨɦɟɫɬɢɬɶ ɜ ɛɭɮɟɪ ɨɛɦɟɧɚ ɱɚɫɬɶ ɞɨɤɭɦɟɧɬɚ Word, ɷɬɨ ɦɨɠɧɨ ɫɞɟɥɚɬɶ ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɚ Copy ɨɛɴɟɤɬɚ Range: Var Rng: Variant; ... 63
Rng := App.Selection.Range; Rng.Copy; ɋɥɟɞɭɸɳɢɣ ɷɬɚɩ – ɩɟɪɟɦɟɳɟɧɢɟ ɤɭɪɫɨɪɚ ɜ ɧɭɠɧɨɟ ɦɟɫɬɨ ɬɟɤɫɬɚ. 6.4.5. ɉɟɪɟɦɟɳɟɧɢɟ ɤɭɪɫɨɪɚ ɩɨ ɬɟɤɫɬɭ ɂɫɩɨɥɶɡɭɹ ɦɟɬɨɞ Collapse, ɦɨɠɧɨ «ɫɠɚɬɶ» ɨɛɴɟɤɬ Range ɢɥɢ ɨɛɴɟɤɬ Selection, ɫɨɤɪɚɬɢɜ ɟɝɨ ɪɚɡɦɟɪ ɞɨ ɧɭɥɹ ɫɢɦɜɨɥɨɜ: Rng.Collapse(wdCollapseEnd); ɉɚɪɚɦɟɬɪ ɷɬɨɝɨ ɦɟɬɨɞɚ ɭɤɚɡɵɜɚɟɬ, ɜ ɧɚɱɚɥɟ ɢɥɢ ɜ ɤɨɧɰɟ ɢɫɯɨɞɧɨɝɨ ɮɪɚɝɦɟɧɬɚ ɨɤɚɠɟɬɫɹ ɧɨɜɵɣ ɨɛɴɟɤɬ Range ɢɥɢ Selection. ȿɫɥɢ ɢɫɩɨɥɶɡɭɟɬɫɹ ɩɨɡɞɧɟɟ ɫɜɹɡɵɜɚɧɢɟ, ɬɨ ɧɭɠɧɨ ɨɩɪɟɞɟɥɢɬɶ ɜ ɩɪɢɥɨɠɟɧɢɢ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɟ ɤɨɧɫɬɚɧɬɵ: const wdCollapseStart = $00000001;//ɧɨɜɵɣ ɨɛɴɟɤɬ ɧɚɯɨɞɢɬɫɹ ɜ ɧɚɱɚɥɟ ɮɪɚɝɦɟɧɬɚ wdCollapseEnd = $00000000;//ɧɨɜɵɣ ɨɛɴɟɤɬ ɧɚɯɨɞɢɬɫɹ ɜ ɤɨɧɰɟ ɮɪɚɝɦɟɧɬɚ ɉɟɪɟɦɟɳɚɬɶ ɤɭɪɫɨɪ ɩɨ ɬɟɤɫɬɭ ɦɨɠɧɨ ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɚ Move ɨɛɴɟɤɬɨɜ Range ɢ Selection. ɗɬɨɬ ɦɟɬɨɞ ɢɦɟɟɬ ɞɜɚ ɩɚɪɚɦɟɬɪɚ. ɉɟɪɜɵɣ ɭɤɚɡɵɜɚɟɬ ɧɚ ɬɨ, ɜ ɤɚɤɢɯ ɟɞɢɧɢɰɚɯ ɢɡɦɟɪɹɟɬɫɹ ɩɟɪɟɦɟɳɟɧɢɟ — ɜ ɫɢɦɜɨɥɚɯ (ɩɨ ɭɦɨɥɱɚɧɢɸ), ɫɥɨɜɚɯ, ɩɪɟɞɥɨɠɟɧɢɹɯ, ɚɛɡɚɰɚɯ ɢ ɞɪ. ȼɬɨɪɨɣ ɩɚɪɚɦɟɬɪ ɭɤɚɡɵɜɚɟɬ, ɧɚ ɫɤɨɥɶɤɨ ɟɞɢɧɢɰ ɩɪɢ ɷɬɨɦ ɧɭɠɧɨ ɩɟɪɟɦɟɫɬɢɬɶɫɹ (ɷɬɨ ɱɢɫɥɨ ɦɨɠɟɬ ɛɵɬɶ ɢ ɨɬɪɢɰɚɬɟɥɶɧɵɦ; ɩɨ ɭɦɨɥɱɚɧɢɸ ɨɧɨ ɪɚɜɧɨ 1). ɇɚɩɪɢɦɟɪ, ɫɥɟɞɭɸɳɢɣ ɮɪɚɝɦɟɧɬ ɤɨɞɚ: Rng.Move; ɩɪɢɜɟɞɟɬ ɤ ɩɟɪɟɦɟɳɟɧɢɸ ɤɭɪɫɨɪɚ ɧɚ ɨɞɢɧ ɫɢɦɜɨɥ ɜɩɟɪɟɞ, ɚ Rng.Move(wdParagraph,3); ɩɪɢɜɟɞɟɬ ɤ ɩɟɪɟɦɟɳɟɧɢɸ ɤɭɪɫɨɪɚ ɧɚ ɬɪɢ ɚɛɡɚɰɚ ɜɩɟɪɟɞ. Ɉɬɦɟɬɢɦ, ɱɬɨ ɷɬɨɬ ɦɟɬɨɞ ɢɫɩɨɥɶɡɭɟɬ ɫɥɟɞɭɸɳɢɟ ɤɨɧɫɬɚɧɬɵ: const //ȿɞɢɧɢɰɟɣ ɩɟɪɟɦɟɳɟɧɢɹ ɹɜɥɹɟɬɫɹ: wdCharacter = $00000001; //ɫɢɦɜɨɥ wdWord = $00000002; //ɫɥɨɜɨ wdSentence = $00000003; //ɩɪɟɞɥɨɠɟɧɢɟ wdParagraph = $00000004; //ɚɛɡɚɰ wdStory = $00000006; //ɱɚɫɬɶ ɞɨɤɭɦɟɧɬɚ (ɧɚɩɪ., ɤɨɥɨɧɬɢɬɭɥ, ɨɝɥɚɜɥɟɧɢɟ ɢ ɞɪ.) wdSection = $00000008; //ɪɚɡɞɟɥ wdColumn = $00000009; //ɤɨɥɨɧɤɚ ɬɚɛɥɢɰɵ wdRow = $0000000A; //ɫɬɪɨɤɚ ɬɚɛɥɢɰɵ wdCell = $0000000C; //ɹɱɟɣɤɚ ɬɚɛɥɢɰɵ wdTable = $0000000F; //ɬɚɛɥɢɰɚ 64
ɇɟɪɟɞɤɨ ɞɥɹ ɩɟɪɟɦɟɳɟɧɢɹ ɩɨ ɬɟɤɫɬɭ ɢɫɩɨɥɶɡɭɸɬɫɹ ɡɚɤɥɚɞɤɢ. ɋɨɡɞɚɬɶ ɡɚɤɥɚɞɤɭ ɜ ɬɟɤɭɳɟɣ ɩɨɡɢɰɢɢ ɤɭɪɫɨɪɚ ɦɨɠɧɨ ɩɭɬɟɦ ɞɨɛɚɜɥɟɧɢɹ ɱɥɟɧɚ ɤɨɥɥɟɤɰɢɢ Bookmarks ɨɛɴɟɤɬɚ Document c ɩɨɦɨɳɶɸ ɦɟɬɨɞɚ Add, ɭɤɚɡɚɜ ɢɦɹ ɡɚɤɥɚɞɤɢ ɜ ɤɚɱɟɫɬɜɟ ɩɚɪɚɦɟɬɪɚ, ɧɚɩɪɢɦɟɪ: App.ActiveDocument.Bookmarks.Add(‘MyBookmark’); ɉɪɨɜɟɪɢɬɶ ɫɭɳɟɫɬɜɨɜɚɧɢɟ ɡɚɤɥɚɞɤɢ ɜ ɞɨɤɭɦɟɧɬɟ ɦɨɠɧɨ ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɚ Exists, ɚ ɩɟɪɟɦɟɫɬɢɬɶɫɹ ɧɚ ɧɟɟ – ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɚ Goto ɨɛɴɟɤɬɨɜ Document, Range ɢɥɢ Selection: Rng := App.ActiveDocument.Goto(wdGoToBookmark, wdGoToNext, ,‘MyBookmark’); Rng.InsertAfter(‘Ɍɟɤɫɬ, ɜɫɬɚɜɥɟɧɧɵɣ ɩɨɫɥɟ ɡɚɤɥɚɞɤɢ’); Ɂɧɚɱɟɧɢɹ ɤɨɧɫɬɚɧɬ ɞɥɹ ɷɬɨɝɨ ɩɪɢɦɟɪɚ ɬɚɤɨɜɵ: const wdGoToBookmark = $FFFFFFFF; //ɩɟɪɟɣɬɢ ɤ ɡɚɤɥɚɞɤɟ wdGoToNext = $00000002; //ɢɫɤɚɬɶ ɫɥɟɞɭɸɳɢɣ ɨɛɴɟɤɬ ɜ ɬɟɤɫɬɟ Ɉɬɦɟɬɢɦ, ɱɬɨ ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɚ Goto ɦɨɠɧɨ ɩɟɪɟɦɟɳɚɬɶɫɹ ɧɟ ɬɨɥɶɤɨ ɧɚ ɭɤɚɡɚɧɧɭɸ ɡɚɤɥɚɞɤɭ, ɧɨ ɢ ɧɚ ɞɪɭɝɢɟ ɨɛɴɟɤɬɵ (ɪɢɫɭɧɤɢ, ɝɪɚɦɦɚɬɢɱɟɫɤɢɟ ɨɲɢɛɤɢ ɢ ɞɪ.), ɢ ɧɚɩɪɚɜɥɟɧɢɟ ɩɟɪɟɦɟɳɟɧɢɹ ɬɨɠɟ ɦɨɠɟɬ ɛɵɬɶ ɪɚɡɥɢɱɧɵɦ. ɉɨɷɬɨɦɭ ɫɩɢɫɨɤ ɤɨɧɫɬɚɧɬ, ɤɨɬɨɪɵɟ ɦɨɝɭɬ ɛɵɬɶ ɢɫɩɨɥɶɡɨɜɚɧɵ ɜ ɤɚɱɟɫɬɜɟ ɩɚɪɚɦɟɬɪɨɜ ɞɚɧɧɨɝɨ ɦɟɬɨɞɚ, ɞɨɜɨɥɶɧɨ ɜɟɥɢɤ.
Rng.InsertAfter(‘4,5,6’); Rng.InsertParagraphAfter; Rng.InsertAfter(‘7,8,9’); Rng.InsertParagraphAfter; Rng.ConvertToTable(‘,’); ȼɧɟɲɧɢɣ ɜɢɞ ɬɚɛɥɢɰɵ ɦɨɠɧɨ ɢɡɦɟɧɢɬɶ ɫ ɩɨɦɨɳɶɸ ɫɜɨɣɫɬɜɚ Format, ɚ ɬɚɤɠɟ ɫ ɩɨɦɨɳɶɸ ɫɜɨɣɫɬɜ ɤɨɥɥɟɤɰɢɢ Columns, ɩɪɟɞɫɬɚɜɥɹɸɳɟɣ ɤɨɥɨɧɤɢ ɬɚɛɥɢɰɵ, ɢ ɤɨɥɥɟɤɰɢɢ Rows, ɩɪɟɞɫɬɚɜɥɹɸɳɟɣ ɫɬɪɨɤɢ ɬɚɛɥɢɰɵ ɨɛɴɟɤɬɚ Table. 6.4.7. Ɉɛɪɚɳɟɧɢɟ ɤ ɫɜɨɣɫɬɜɚɦ ɞɨɤɭɦɟɧɬɚ ɋɜɨɣɫɬɜɚ ɞɨɤɭɦɟɧɬɚ ɦɨɠɧɨ ɩɨɥɭɱɢɬɶ ɫ ɩɨɦɨɳɶɸ ɤɨɥɥɟɤɰɢɢ BuiltInDocumentProperties ɨɛɴɟɤɬɚ Document, ɧɚɩɪɢɦɟɪ: Memo1.Lines.Add(‘ɇɚɡɜɚɧɢɟ ‘+App.ActiveDocument.BuiltInDocumentProperties [wdPropertyTitle].Value); Memo1.Lines.Add(‘Ⱥɜɬɨɪ ‘+App.ActiveDocument.BuiltInDocumentProperties [wdPropertyAuthor].Value); Memo1.Lines.Add(‘ɒɚɛɥɨɧ ‘+App.ActiveDocument.BuiltInDocumentProperties [wdPropertyTemplate].Value);
6.4.6. ɋɨɡɞɚɧɢɟ ɬɚɛɥɢɰ ɋɨɡɞɚɜɚɬɶ ɬɚɛɥɢɰɵ ɦɨɠɧɨ ɞɜɭɦɹ ɫɩɨɫɨɛɚɦɢ. ɉɟɪɜɵɣ ɡɚɤɥɸɱɚɟɬɫɹ ɜ ɜɵɡɨɜɟ ɦɟɬɨɞɚ Add ɤɨɥɥɟɤɰɢɢ Tables ɨɛɴɟɤɬɚ Document ɢ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɦ ɡɚɩɨɥɧɟɧɢɢ ɹɱɟɟɤ ɞɚɧɧɵɦɢ. ɗɬɨɬ ɫɩɨɫɨɛ ɩɪɢ ɩɨɡɞɧɟɦ ɫɜɹɡɵɜɚɧɢɢ ɪɚɛɨɬɚɟɬ ɞɨɜɨɥɶɧɨ ɦɟɞɥɟɧɧɨ. ȼɬɨɪɨɣ ɫɩɨɫɨɛ, ɧɚɦɧɨɝɨ ɛɨɥɟɟ «ɛɵɫɬɪɵɣ», ɡɚɤɥɸɱɚɟɬɫɹ ɜ ɫɨɡɞɚɧɢɢ ɬɟɤɫɬɚ ɢɡ ɧɟɫɤɨɥɶɤɢɯ ɫɬɪɨɤ, ɫɨɞɟɪɠɚɳɢɯ ɩɨɞɫɬɪɨɤɢ ɫ ɪɚɡɞɟɥɢɬɟɥɹɦɢ (ɜ ɤɚɱɟɫɬɜɟ ɪɚɡɞɟɥɢɬɟɥɹ ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɥɸɛɨɣ ɢɥɢ ɩɨɱɬɢ ɥɸɛɨɣ ɫɢɦɜɨɥ, ɧɭɠɧɨ ɬɨɥɶɤɨ, ɱɬɨɛɵ ɨɧ ɡɚɜɟɞɨɦɨ ɧɟ ɜɫɬɪɟɱɚɥɫɹ ɜ ɞɚɧɧɵɯ, ɤɨɬɨɪɵɟ ɛɭɞɭɬ ɩɨɦɟɳɟɧɵ ɜ ɛɭɞɭɳɭɸ ɬɚɛɥɢɰɭ), ɢ ɩɨɫɥɟɞɭɸɳɟɣ ɤɨɧɜɟɪɬɚɰɢɢ ɬɚɤɨɝɨ ɬɟɤɫɬɚ ɜ ɬɚɛɥɢɰɭ ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɚ ConvertToTable ɨɛɴɟɤɬɚ Range. ɇɢɠɟ ɩɪɢɜɟɞɟɧ ɩɪɢɦɟɪ ɫɨɡɞɚɧɢɹ ɬɚɛɥɢɰɵ ɢɡ ɬɪɟɯ ɫɬɪɨɤ ɢ ɬɪɟɯ ɫɬɨɥɛɰɨɜ ɷɬɢɦ ɫɩɨɫɨɛɨɦ (ɜ ɤɚɱɟcɬɜɟ ɪɚɡɞɟɥɢɬɟɥɹ, ɹɜɥɹɸɳɟɝɨɫɹ ɩɟɪɜɵɦ ɩɚɪɚɦɟɬɪɨɦ ɦɟɬɨɞɚ ConvertToTable, ɢɫɩɨɥɶɡɭɟɬɫɹ ɡɚɩɹɬɚɹ): Var Rng: Variant; ... Rng := App.Selection.Range; Rng.Collapse(wdCollapseEnd); Rng.InsertAfter(‘1, 2, 3’); Rng.InsertParagraphAfter; 65
ȼ ɞɚɧɧɨɦ ɪɚɡɞɟɥɟ ɩɪɢɜɟɞɟɧɵ ɨɫɧɨɜɧɵɟ ɨɩɟɪɚɰɢɢ, ɤɨɬɨɪɵɟ ɧɚɢɛɨɥɟɟ ɱɚɫɬɨ ɩɪɢɦɟɧɹɸɬɫɹ ɩɪɢ ɚɜɬɨɦɚɬɢɡɚɰɢɢ Microsoft Word. ȿɫɬɟɫɬɜɟɧɧɨ, ɜɨɡɦɨɠɧɨɫɬɢ ɚɜɬɨɦɚɬɢɡɚɰɢɢ Word ɞɚɥɟɤɨ ɧɟ ɢɫɱɟɪɩɵɜɚɸɬɫɹ ɩɪɢɜɟɞɟɧɧɵɦɢ ɩɪɢɦɟɪɚɦɢ, ɨɞɧɚɤɨ, ɪɭɤɨɜɨɞɫɬɜɭɹɫɶ ɨɫɧɨɜɧɵɦɢ ɩɪɢɧɰɢɩɚɦɢ ɫɨɡɞɚɧɢɹ ɤɨɧɬɪɨɥɥɟɪɨɜ Word, ɢɡɥɨɠɟɧɧɵɦɢ ɜ ɞɚɧɧɨɣ ɝɥɚɜɟ, ɢ ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɣ ɫɩɪɚɜɨɱɧɨɣ ɢɧɮɨɪɦɚɰɢɟɣ [Ɉɲɢɛɤɚ! ɂɫɬɨɱɧɢɤ ɫɫɵɥɤɢ ɧɟ ɧɚɣɞɟɧ.], ɦɨɠɧɨ ɢɦɢ ɜɨɫɩɨɥɶɡɨɜɚɬɶɫɹ ɫɚɦɨɫɬɨɹɬɟɥɶɧɨ.
6.5. Ⱥɜɬɨɦɚɬɢɡɚɰɢɹ Microsoft Excel ȼ ɞɚɧɧɨɦ ɪɚɡɞɟɥɟ ɪɚɫɫɦɚɬɪɢɜɚɸɬɫɹ ɧɚɢɛɨɥɟɟ ɱɚɫɬɨ ɜɫɬɪɟɱɚɸɳɢɟɫɹ ɡɚɞɚɱɢ, ɫɜɹɡɚɧɧɵɟ ɫ ɚɜɬɨɦɚɬɢɡɚɰɢɟɣ Microsoft Excel. ɇɨ ɩɟɪɟɞ ɷɬɢɦ ɪɚɫɫɦɨɬɪɢɦ, ɤɚɤɨɜɵ ɩɪɨɝɪɚɦɦɧɵɟ ɢɞɟɧɬɢɮɢɤɚɬɨɪɵ ɨɫɧɨɜɧɵɯ ɨɛɴɟɤɬɨɜ Microsoft Excel ɢ ɱɬɨ ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ ɟɝɨ ɨɛɴɟɤɬɧɚɹ ɦɨɞɟɥɶ. 6.5.1. ɉɪɨɝɪɚɦɦɧɵɟ ɢɞɟɧɬɢɮɢɤɚɬɨɪɵ ɢ ɨɛɴɟɤɬɧɚɹ ɦɨɞɟɥɶ Microsoft Excel ɋɭɳɟɫɬɜɭɟɬ ɬɪɢ ɬɢɩɚ ɨɛɴɟɤɬɨɜ Excel, ɤɨɬɨɪɵɟ ɦɨɝɭɬ ɛɵɬɶ ɫɨɡɞɚɧɵ ɧɟɩɨɫɪɟɞɫɬɜɟɧɧɨ ɫ ɩɨɦɨɳɶɸ ɩɪɢɥɨɠɟɧɢɹ-ɤɨɧɬɪɨɥɥɟɪɚ. ɗɬɢ ɨɛɴɟɤɬɵ ɢ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɟ ɢɦ ɩɪɨɝɪɚɦɦɧɵɟ ɢɞɟɧɬɢɮɢɤɚɬɨɪɵ ɩɟɪɟɱɢɫɥɟɧɵ ɧɢɠɟ. 66
Ɉɛɴɟɤɬ
ɉɪɨɝɪɚɦɦɧɵɣ ɢɞɟɧɬɢɮɢɤɚɬɨɪ
Application
Excel.Application, Excel.Application.9
WorkBook
Excel.AddIn
Chart
Excel.Chart, Excel.Chart.8
WorkSheet
Excel.Sheet, Excel.Sheet.8
Ʉɨɦɦɟɧɬɚɪɢɣ ɋ ɩɨɦɨɳɶɸ ɷɬɨɝɨ ɩɪɨɝɪɚɦɦɧɨɝɨ ɢɞɟɧɬɢɮɢɤɚɬɨɪɚ ɫɨɡɞɚɟɬɫɹ ɷɤɡɟɦɩɥɹɪ ɩɪɢɥɨɠɟɧɢɹ ɛɟɡ ɨɬɤɪɵɬɵɯ ɪɚɛɨɱɢɯ ɤɧɢɝ ɋ ɩɨɦɨɳɶɸ ɷɬɨɝɨ ɩɪɨɝɪɚɦɦɧɨɝɨ ɢɞɟɧɬɢɮɢɤɚɬɨɪɚ ɫɨɡɞɚɟɬɫɹ ɷɤɡɟɦɩɥɹɪ ɪɚɫɲɢɪɟɧɢɹ (add-in) Excel (ɢɦɟɸɳɢɟɫɹ ɪɚɫɲɢɪɟɧɢɹ ɞɨɫɬɭɩɧɵ ɫ ɩɨɦɨɳɶɸ ɩɭɧɤɬɚ ɦɟɧɸ Tools/Add-Ins) Ɋɚɛɨɱɚɹ ɤɧɢɝɚ, ɫɨɡɞɚɧɧɚɹ ɫ ɩɨɦɨɳɶɸ ɷɬɢɯ ɩɪɨɝɪɚɦɦɧɵɯ ɢɞɟɧɬɢɮɢɤɚɬɨɪɨɜ, ɫɨɫɬɨɢɬ ɢɡ ɞɜɭɯ ɥɢɫɬɨɜ – ɨɞɧɨɝɨ ɞɥɹ ɞɢɚɝɪɚɦɦɵ, ɞɪɭɝɨɝɨ – ɞɥɹ ɞɚɧɧɵɯ, ɧɚ ɨɫɧɨɜɟ ɤɨɬɨɪɵɯ ɨɧɚ ɩɨɫɬɪɨɟɧɚ Ɋɚɛɨɱɚɹ ɤɧɢɝɚ, ɫɨɡɞɚɧɧɚɹ ɫ ɩɨɦɨɳɶɸ ɷɬɢɯ ɩɪɨɝɪɚɦɦɧɵɯ ɢɞɟɧɬɢɮɢɤɚɬɨɪɨɜ, ɫɨɫɬɨɢɬ ɢɡ ɨɞɧɨɝɨ ɥɢɫɬɚ
ȼɫɟ ɨɫɬɚɥɶɧɵɟ ɨɛɴɟɤɬɵ Excel ɹɜɥɹɸɬɫɹ ɜɧɭɬɪɟɧɧɢɦɢ ɨɛɴɟɤɬɚɦɢ ɢ ɫɭɳɟɫɬɜɨɜɚɬɶ ɫɚɦɨɫɬɨɹɬɟɥɶɧɨ ɧɟ ɦɨɝɭɬ. Ɉɫɧɨɜɧɵɦ ɜ ɨɛɴɟɤɬɧɨɣ ɦɨɞɟɥɢ Excel ɹɜɥɹɟɬɫɹ ɨɛɴɟɤɬ Application, ɫɨɞɟɪɠɚɳɢɣ ɤɨɥɥɟɤɰɢɸ Workbooks ɨɛɴɟɤɬɨɜ ɬɢɩɚ WorkBook (ɪɢɫ. 3).
Ʉɚɠɞɵɣ ɨɛɴɟɤɬ ɬɢɩɚ WorkBook ɫɨɞɟɪɠɢɬ ɤɨɥɥɟɤɰɢɸ WorkSheetsɨɛɴɟɤɬɨɜ ɬɢɩɚ WorkSheet, Charts ɬɢɩɚ Chart ɢ ɞɪ. Ɇɚɧɢɩɭɥɹɰɢɹ ɪɚɛɨɱɢɦɢ ɤɧɢɝɚɦɢ, ɢɯ ɥɢɫɬɚɦɢ, ɹɱɟɣɤɚɦɢ, ɞɢɚɝɪɚɦɦɚɦɢ ɪɟɚɥɶɧɨ ɨɫɭɳɟɫɬɜɥɹɟɬɫɹ ɩɭɬɟɦ ɨɛɪɚɳɟɧɢɹ ɤ ɫɜɨɣɫɬɜɚɦ ɢ ɦɟɬɨɞɚɦ ɷɬɢɯ ɨɛɴɟɤɬɨɜ. Ɋɚɫɫɦɨɬɪɢɦ ɧɚɢɛɨɥɟɟ ɱɚɫɬɨ ɜɫɬɪɟɱɚɸɳɢɟɫɹ ɡɚɞɚɱɢ, ɫɜɹɡɚɧɧɵɟ ɫ ɚɜɬɨɦɚɬɢɡɚɰɢɟɣ Microsoft Excel. 6.5.2. Ɂɚɩɭɫɤ Microsoft Excel, ɫɨɡɞɚɧɢɟ ɢ ɨɬɤɪɵɬɢɟ ɪɚɛɨɱɢɯ ɤɧɢɝ Ⱦɥɹ ɫɨɡɞɚɧɢɹ ɩɪɢɦɟɪɨɜ ɢɫɩɨɥɶɡɨɜɚɧɢɹ Microsoft Excel ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɮɭɧɤɰɢɸ CreateOleObject: App := CreateOleObject(‘Excel.Application’); ɋɨɡɞɚɬɶ ɧɨɜɭɸ ɪɚɛɨɱɭɸ ɤɧɢɝɭ Excel ɦɨɠɧɨ, ɢɫɩɨɥɶɡɭɹ ɦɟɬɨɞ Add ɤɨɥɥɟɤɰɢɢ Workbooks ɨɛɴɟɤɬɚ Application: App.WorkBooks.Add; Ⱦɥɹ ɫɨɡɞɚɧɢɹ ɪɚɛɨɱɟɣ ɤɧɢɝɢ ɧɚ ɨɫɧɨɜɟ ɲɚɛɥɨɧɚ ɫɥɟɞɭɟɬ ɭɤɚɡɚɬɶ ɟɝɨ ɢɦɹ ɜ ɤɚɱɟɫɬɜɟ ɩɟɪɜɨɝɨ ɩɚɪɚɦɟɬɪɚ ɦɟɬɨɞɚ Add: App.WorkBooks.Add (‘C:\Program Files\Microsoft Office\Templates\1033\invoice.xlt’); ȼ ɤɚɱɟɫɬɜɟ ɩɟɪɜɨɝɨ ɩɚɪɚɦɟɬɪɚ ɷɬɨɝɨ ɦɟɬɨɞɚ ɦɨɠɧɨ ɬɚɤɠɟ ɢɫɩɨɥɶɡɨɜɚɬɶ ɫɥɟɞɭɸɳɢɟ ɤɨɧɫɬɚɧɬɵ: const xlWBATChart = $FFFFEFF3; //ɪɚɛɨɱɚɹ ɤɧɢɝɚ ɫɨɫɬɨɢɬ ɢɡ ɥɢɫɬɚ ɫ ɞɢɚɝɪɚɦɦɨɣ xlWBATWorksheet = $FFFFEFB9; //ɪɚɛɨɱɚɹ ɤɧɢɝɚ ɫɨɫɬɨɢɬ ɢɡ ɥɢɫɬɚ ɫ ɞɚɧɧɵɦɢ ȼ ɷɬɨɦ ɫɥɭɱɚɟ ɪɚɛɨɱɚɹ ɤɧɢɝɚ ɛɭɞɟɬ ɫɨɞɟɪɠɚɬɶ ɨɞɢɧ ɥɢɫɬ ɬɢɩɚ, ɡɚɞɚɧɧɨɝɨ ɭɤɚɡɚɧɧɨɣ ɤɨɧɫɬɚɧɬɨɣ (ɝɪɚɮɢɤ, ɨɛɵɱɧɵɣ ɥɢɫɬ ɫ ɞɚɧɧɵɦɢ ɢ ɞɪ.) Ⱦɥɹ ɨɬɤɪɵɬɢɹ ɭɠɟ ɫɭɳɟɫɬɜɭɸɳɟɝɨ ɞɨɤɭɦɟɧɬɚ ɫɥɟɞɭɟɬ ɜɨɫɩɨɥɶɡɨɜɚɬɶɫɹ ɦɟɬɨɞɨɦ Open ɤɨɥɥɟɤɰɢɢ WorkBooks: App.Documents.Open(‘C:\MyExcelFile.xls’);
Ɋɢɫ. 6.3. Ɏɪɚɝɦɟɧɬ ɨɛɴɟɤɬɧɨɣ ɦɨɞɟɥɢ Microsoft Excel
67
Ɉɬɦɟɬɢɦ, ɱɬɨ ɫɜɨɣɫɬɜɨ ActiveWorkBook ɨɛɴɟɤɬɚ Excel.Application ɭɤɚɡɵɜɚɟɬ ɧɚ ɬɟɤɭɳɭɸ ɚɤɬɢɜɧɭɸ ɪɚɛɨɱɭɸ ɤɧɢɝɭ ɫɪɟɞɢ ɨɞɧɨɣ ɢɥɢ ɧɟɫɤɨɥɶɤɢɯ ɨɬɤɪɵɬɵɯ. ɉɨɦɢɦɨ ɷɬɨɝɨ ɤ ɪɚɛɨɱɟɣ ɤɧɢɝɟ ɦɨɠɧɨ ɨɛɪɚɳɚɬɶɫɹ ɩɨ ɟɟ ɩɨɪɹɞɤɨɜɨɦɭ ɧɨɦɟɪɭ, ɧɚɩɪɢɦɟɪ ɤɨ ɜɬɨɪɨɣ ɨɬɤɪɵɬɨɣ ɪɚɛɨɱɟɣ ɤɧɢɝɟ ɦɨɠɧɨ ɨɛɪɚɬɢɬɶɫɹ ɬɚɤ: App.WorkBooks[2]
68
Ɉɛɪɚɬɢɬɟ ɜɧɢɦɚɧɢɟ ɧɚ ɬɨ, ɱɬɨ ɜ Delphi ɩɪɢ ɢɫɩɨɥɶɡɨɜɚɧɢɢ ɩɨɡɞɧɟɝɨ ɫɜɹɡɵɜɚɧɢɹ ɫɢɧɬɚɤɫɢɫ, ɢɫɩɨɥɶɡɭɟɦɵɣ ɞɥɹ ɨɛɪɚɳɟɧɢɹ ɤ ɱɥɟɧɚɦ ɤɨɥɥɟɤɰɢɣ ɨɛɴɟɤɬɨɜ Excel, ɨɬɥɢɱɟɧ ɨɬ ɫɢɧɬɚɤɫɢɫɚ, ɢɫɩɨɥɶɡɭɟɦɨɝɨ ɞɥɹ ɨɛɪɚɳɟɧɢɹ ɤ ɨɛɴɟɤɬɚɦ Word – ɜ ɫɥɭɱɚɟ Word ɦɵ ɢɫɩɨɥɶɡɨɜɚɥɢ ɦɟɬɨɞ Item, ɚ ɜ ɫɥɭɱɚɟ Excel ɦɵ ɨɛɪɚɳɚɟɦɫɹ ɤ ɱɥɟɧɚɦ ɤɨɥɥɟɤɰɢɢ ɤɚɤ ɤ ɷɥɟɦɟɧɬɚɦ ɦɚɫɫɢɜɚ. ȿɫɥɢ ɠɟ ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ Visual Basic, ɫɢɧɬɚɤɫɢɫ, ɩɪɢɦɟɧɹɟɦɵɣ ɞɥɹ ɨɛɪɚɳɟɧɢɹ ɤ ɱɥɟɧɚɦ ɤɨɥɥɟɤɰɢɣ, ɛɭɞɟɬ ɨɞɢɧɚɤɨɜ ɞɥɹ ɜɫɟɯ ɤɨɥɥɟɤɰɢɣ Microsoft Office. ɋɞɟɥɚɬɶ ɪɚɛɨɱɭɸ ɤɧɢɝɭ ɚɤɬɢɜɧɨɣ ɦɨɠɧɨ ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɚ Activate: App.WorkBooks[2].Activate; Ɋɚɫɫɦɨɬɪɢɦ ɞɚɥɟɟ ɫɨɯɪɚɧɟɧɢɟ ɪɚɛɨɱɢɯ ɤɧɢɝ ɜ ɮɚɣɥɚɯ. 6.5.3. ɋɨɯɪɚɧɟɧɢɟ, ɩɟɱɚɬɶ ɢ ɡɚɤɪɵɬɢɟ ɪɚɛɨɱɢɯ ɤɧɢɝ Microsoft Excel Ɂɚɤɪɵɬɢɟ ɞɨɤɭɦɟɧɬɚ ɦɨɠɟɬ ɛɵɬɶ ɨɫɭɳɟɫɬɜɥɟɧɨ ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɚ Close: App.WorkBooks[2].Close;
ȼɵɜɨɞ ɞɨɤɭɦɟɧɬɚ Excel ɧɚ ɭɫɬɪɨɣɫɬɜɨ ɩɟɱɚɬɢ ɦɨɠɧɨ ɨɫɭɳɟɫɬɜɢɬɶ ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɚ PrintOut ɨɛɴɟɤɬɚ WorkBook, ɧɚɩɪɢɦɟɪ: App.ActiveWorkBook.PrintOut; ȿɫɥɢ ɧɭɠɧɨ ɢɡɦɟɧɢɬɶ ɩɚɪɚɦɟɬɪɵ ɩɟɱɚɬɢ, ɫɥɟɞɭɟɬ ɭɤɚɡɚɬɶ ɡɧɚɱɟɧɢɹ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɯ ɩɚɪɚɦɟɬɪɨɜ ɦɟɬɨɞɚ PrintOut (ɜ ɫɥɭɱɚɟ Excel ɢɯ ɜɨɫɟɦɶ). 6.5.4. Ɉɛɪɚɳɟɧɢɟ ɤ ɥɢɫɬɚɦ ɢ ɹɱɟɣɤɚɦ Ɉɛɪɚɳɟɧɢɟ ɤ ɥɢɫɬɚɦ ɪɚɛɨɱɟɣ ɤɧɢɝɢ ɩɪɨɢɡɜɨɞɢɬɫɹ ɫ ɩɨɦɨɳɶɸ ɤɨɥɥɟɤɰɢɢ WorkSheets ɨɛɴɟɤɬɚ WorkBook. Ʉɚɠɞɵɣ ɱɥɟɧ ɷɬɨɣ ɤɨɥɥɟɤɰɢɢ ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ ɨɛɴɟɤɬ WorkSheet. Ʉ ɱɥɟɧɭ ɷɬɨɣ ɤɨɥɥɟɤɰɢɢ ɦɨɠɧɨ ɨɛɪɚɬɢɬɶɫɹ ɩɨ ɟɝɨ ɩɨɪɹɞɤɨɜɨɦɭ ɧɨɦɟɪɭ, ɧɚɩɪɢɦɟɪ: App.WorkBooks[1].WorkSheets[1].Name := ‘ɋɬɪɚɧɢɰɚ 1’; ɉɪɢɜɟɞɟɧɧɵɣ ɜɵɲɟ ɩɪɢɦɟɪ ɢɥɥɸɫɬɪɢɪɭɟɬ, ɤɚɤ ɦɨɠɧɨ ɢɡɦɟɧɢɬɶ ɢɦɹ ɥɢɫɬɚ ɪɚɛɨɱɟɣ ɤɧɢɝɢ. Ʉ ɥɢɫɬɭ ɪɚɛɨɱɟɣ ɤɧɢɝɢ ɦɨɠɧɨ ɨɛɪɚɬɢɬɶɫɹ ɢ ɩɨ ɢɦɟɧɢ, ɧɚɩɪɢɦɟɪ: App.WorkBooks[1].WorkSheets[‘Sheet1’].Name := ‘ɋɬɪɚɧɢɰɚ 1’;
ɢɥɢ App.ActiveWorkBook.Close; Ɇɟɬɨɞ Close ɢɦɟɟɬ ɧɟɫɤɨɥɶɤɨ ɧɟɨɛɹɡɚɬɟɥɶɧɵɯ (ɜ ɫɥɭɱɚɟ ɩɨɡɞɧɟɝɨ ɫɜɹɡɵɜɚɧɢɹ) ɩɚɪɚɦɟɬɪɨɜ, ɜɥɢɹɸɳɢɯ ɧɚ ɩɪɚɜɢɥɚ ɫɨɯɪɚɧɟɧɢɹ ɪɚɛɨɱɟɣ ɤɧɢɝɢ. ɉɟɪɜɵɣ ɢɡ ɩɚɪɚɦɟɬɪɨɜ ɩɪɢɧɢɦɚɟɬ ɡɧɚɱɟɧɢɹ True ɢɥɢ False ɢ ɜɥɢɹɟɬ ɧɚ ɬɨ, ɫɨɯɪɚɧɹɬɶ ɥɢ ɢɡɦɟɧɟɧɢɹ, ɜɧɟɫɟɧɧɵɟ ɜ ɪɚɛɨɱɭɸ ɤɧɢɝɭ. ȼɬɨɪɨɣ ɩɚɪɚɦɟɬɪ (ɬɢɩɚ Variant) — ɢɦɹ ɮɚɣɥɚ, ɜ ɤɨɬɨɪɨɦ ɧɭɠɧɨ ɫɨɯɪɚɧɢɬɶ ɪɚɛɨɱɭɸ ɤɧɢɝɭ (ɟɫɥɢ ɜ ɧɟɟ ɛɵɥɢ ɜɧɟɫɟɧɵ ɢɡɦɟɧɟɧɢɹ). Ɍɪɟɬɢɣ ɩɚɪɚɦɟɬɪ, ɬɚɤɠɟ ɩɪɢɧɢɦɚɸɳɢɣ ɡɧɚɱɟɧɢɹ True ɢɥɢ False, ɜɥɢɹɟɬ ɧɚ ɬɨ, ɫɥɟɞɭɟɬ ɥɢ ɩɟɪɟɫɵɥɚɬɶ ɞɨɤɭɦɟɧɬ ɫɥɟɞɭɸɳɟɦɭ ɩɨɥɶɡɨɜɚɬɟɥɸ ɩɨ ɷɥɟɤɬɪɨɧɧɨɣ ɩɨɱɬɟ, ɢ ɦɨɠɟɬ ɛɵɬɶ ɩɪɨɢɝɧɨɪɢɪɨɜɚɧ, ɟɫɥɢ ɷɬɚ ɮɭɧɤɰɢɨɧɚɥɶɧɨɫɬɶ ɧɟ ɢɫɩɨɥɶɡɭɟɬɫɹ: App.ActiveWorkBook.Close(True,’C:\MyWorkBook.xls’); ɉɪɨɫɬɨ ɫɨɯɪɚɧɢɬɶ ɪɚɛɨɱɭɸ ɤɧɢɝɭ, ɧɟ ɡɚɤɪɵɜɚɹ ɟɟ, ɦɨɠɧɨ ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɨɜ Save ɢɥɢ SaveAs: App.ActiveWorkBook.Save; ɢɥɢ App.ActiveWorkBook.SaveAs(‘C:\MyWorkBook.xls’); Ɇɟɬɨɞ SaveAs ɢɦɟɟɬ ɛɨɥɟɟ ɞɟɫɹɬɤɚ ɩɚɪɚɦɟɬɪɨɜ, ɜɥɢɹɸɳɢɯ ɧɚ ɬɨ, ɤɚɤ ɢɦɟɧɧɨ ɫɨɯɪɚɧɹɟɬɫɹ ɞɨɤɭɦɟɧɬ (ɩɨɞ ɤɚɤɢɦ ɢɦɟɧɟɦ, ɫ ɩɚɪɨɥɟɦ ɢɥɢ ɛɟɡ ɧɟɝɨ, ɤɚɤɨɜɚ ɤɨɞɨɜɚɹ ɫɬɪɚɧɢɰɚ ɞɥɹ ɫɨɞɟɪɠɚɳɟɝɨɫɹ ɜ ɧɟɣ ɬɟɤɫɬɚ ɢ ɞɪ.). Ɂɚɤɪɵɬɶ ɫɚɦ Excel ɦɨɠɧɨ ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɚ Quit ɨɛɴɟɤɬɚ Excel.Application. ȼ ɫɥɭɱɚɟ Excel ɷɬɨɬ ɦɟɬɨɞ ɩɚɪɚɦɟɬɪɨɜ ɧɟ ɢɦɟɟɬ. 69
Ɉɛɪɚɳɟɧɢɟ ɤ ɨɬɞɟɥɶɧɵɦ ɹɱɟɣɤɚɦ ɥɢɫɬɚ ɩɪɨɢɡɜɨɞɢɬɫɹ ɫ ɩɨɦɨɳɶɸ ɤɨɥɥɟɤɰɢɢ Cells ɨɛɴɟɤɬɚ WorkSheet. ɇɚɩɪɢɦɟɪ, ɞɨɛɚɜɢɬɶ ɞɚɧɧɵɟ ɜ ɹɱɟɣɤɭ B1 ɦɨɠɧɨ ɫɥɟɞɭɸɳɢɦ ɨɛɪɚɡɨɦ: App.WorkBooks[1].WorkSheets[‘Sheet1’].Cells[1,2].Value:=’25’; Ɂɞɟɫɶ ɩɟɪɜɚɹ ɢɡ ɤɨɨɪɞɢɧɚɬ ɹɱɟɣɤɢ ɭɤɚɡɵɜɚɟɬ ɧɚ ɧɨɦɟɪ ɫɬɪɨɤɢ, ɜɬɨɪɚɹ – ɧɚ ɧɨɦɟɪ ɫɬɨɥɛɰɚ. Ⱦɨɛɚɜɥɟɧɢɟ ɮɨɪɦɭɥ ɜ ɹɱɟɣɤɢ ɩɪɨɢɡɜɨɞɢɬɫɹ ɚɧɚɥɨɝɢɱɧɵɦ ɫɩɨɫɨɛɨɦ: App.WorkBooks[1].WorkSheets[‘Sheet1’].Cells[3,2].Value := ‘ =SUM(B1:B2)’; Ɉɱɢɫɬɢɬɶ ɹɱɟɣɤɭ ɦɨɠɧɨ ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɚ ClearContents. Ɏɨɪɦɚɬɢɪɨɜɚɧɢɟ ɬɟɤɫɬɚ ɜ ɹɱɟɣɤɚɯ ɩɪɨɢɡɜɨɞɢɬɫɹ ɫ ɩɨɦɨɳɶɸ ɫɜɨɣɫɬɜ Font ɢ Interior ɨɛɴɟɤɬɚ Cell ɢ ɢɯ ɩɨɞɫɜɨɣɫɬɜ. ɇɚɩɪɢɦɟɪ, ɫɥɟɞɭɸɳɢɣ ɮɪɚɝɦɟɧɬ ɤɨɞɚ ɜɵɜɨɞɢɬ ɬɟɤɫɬ ɜ ɹɱɟɣɤɟ ɤɪɚɫɧɵɦ ɠɢɪɧɵɦ ɲɪɢɮɬɨɦ Courier ɤɟɝɥɹ 16 ɧɚ ɠɟɥɬɨɦ ɮɨɧɟ: App.WorkBooks[1].WorkSheets[1].Cells[3,2].Interior.Color := clYellow; App.WorkBooks[1].WorkSheets[1].Cells[3,2].Font.Color := clRed; App.WorkBooks[1].WorkSheets[1].Cells[3,2].Font.Name := ‘Courier’; App.WorkBooks[1].WorkSheets[1].Cells[3,2].Font.Size := 16; App.WorkBooks[1].WorkSheets[1].Cells[3,2].Font.Bold := True; ȼɦɟɫɬɨ ɫɜɨɣɫɬɜɚ Color ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɫɜɨɣɫɬɜɨ ColorIndex, ɩɪɢɧɢɦɚɸɳɟɟ ɡɧɚɱɟɧɢɹ ɨɬ 1 ɞɨ 56; ɬɚɛɥɢɰɭ ɫɨɨɬɜɟɬɫɬɜɢɣ ɡɧɚɱɟɧɢɣ ɷɬɨɝɨ 70
ɫɜɨɣɫɬɜɚ ɪɟɚɥɶɧɵɦ ɰɜɟɬɚɦ ɦɨɠɧɨ ɧɚɣɬɢ ɜ ɫɩɪɚɜɨɱɧɨɦ ɮɚɣɥɟ VBAXL9.CHM. Ɉɛɪɚɬɢɬɶɫɹ ɤ ɬɟɤɭɳɟɣ ɹɱɟɣɤɟ ɦɨɠɧɨ ɫ ɩɨɦɨɳɶɸ ɫɜɨɣɫɬɜɚ ActiveCell ɨɛɴɟɤɬɚ Excel.Application, ɚ ɭɡɧɚɬɶ ɦɟɫɬɨɩɨɥɨɠɟɧɢɟ ɹɱɟɣɤɢ ɦɨɠɧɨ ɫ ɩɨɦɨɳɶɸ ɫɜɨɣɫɬɜɚ Address ɨɛɴɟɤɬɚ Cell, ɧɚɩɪɢɦɟɪ: ShowMessage(App.ActiveCell.Address); ɉɨɦɢɦɨ ɨɛɪɚɳɟɧɢɹ ɤ ɨɬɞɟɥɶɧɵɦ ɹɱɟɣɤɚɦ, ɦɨɠɧɨ ɦɚɧɢɩɭɥɢɪɨɜɚɬɶ ɩɪɹɦɨɭɝɨɥɶɧɵɦɢ ɨɛɥɚɫɬɹɦɢ ɹɱɟɟɤ ɫ ɩɨɦɨɳɶɸ ɨɛɴɟɤɬɚ Range, ɧɚɩɪɢɦɟɪ: App.WorkBooks[1].WorkSheets[2].Range[‘A1:C5’].Value := ‘Test’; App.WorkBooks[1].WorkSheets[2].Range[‘A1:C5’].Font.Color := clRed; ɉɪɢɜɟɞɟɧɧɵɣ ɜɵɲɟ ɤɨɞ ɩɪɢɜɨɞɢɬ ɤ ɡɚɩɨɥɧɟɧɢɸ ɩɪɹɦɨɭɝɨɥɶɧɨɝɨ ɭɱɚɫɬɤɚ ɬɟɤɫɬɨɦ ɢ ɤ ɢɡɦɟɧɟɧɢɸ ɰɜɟɬɚ ɲɪɢɮɬɚ ɹɱɟɟɤ. Ɉɛɴɟɤɬ Range ɬɚɤɠɟ ɱɚɫɬɨ ɢɫɩɨɥɶɡɭɟɬɫɹ ɞɥɹ ɤɨɩɢɪɨɜɚɧɢɹ ɩɪɹɦɨɭɝɨɥɶɧɵɯ ɨɛɥɚɫɬɟɣ ɱɟɪɟɡ ɛɭɮɟɪ ɨɛɦɟɧɚ. ɇɢɠɟ ɩɪɢɜɟɞɟɧ ɩɪɢɦɟɪ, ɢɥɥɸɫɬɪɢɪɭɸɳɢɣ ɤɨɩɢɪɨɜɚɧɢɟ ɬɚɤɨɣ ɨɛɥɚɫɬɢ: App.WorkBooks[1].WorkSheets[2].Range[‘A1:C5’].Copy; App.WorkBooks[1].WorkSheets[2].Range[‘A11:C15’].Select; App.WorkBooks[1].WorkSheets[2].Paste; Ɉɛɪɚɬɢɬɟ ɜɧɢɦɚɧɢɟ ɧɚ ɬɨ, ɱɬɨ ɞɢɚɩɚɡɨɧ, ɤɭɞɚ ɤɨɩɢɪɭɸɬɫɹ ɞɚɧɧɵɟ, ɩɪɟɞɜɚɪɢɬɟɥɶɧɨ ɜɵɞɟɥɹɟɬɫɹ ɫ ɩɨɦɨɳɶɸ ɦɟɬɨɞɚ Select. Ɉɬɦɟɬɢɦ, ɱɬɨ ɩɪɢɦɟɪɧɨ ɬɚɤɢɦ ɠɟ ɨɛɪɚɡɨɦ ɦɨɠɧɨ ɤɨɩɢɪɨɜɚɬɶ ɞɚɧɧɵɟ ɢ ɢɡ ɞɪɭɝɢɯ ɩɪɢɥɨɠɟɧɢɣ (ɧɚɩɪɢɦɟɪ, ɢɡ Microsoft Word). Ⱦɨɜɨɥɶɧɨ ɱɚɫɬɨ ɩɪɢ ɚɜɬɨɦɚɬɢɡɚɰɢɢ Excel ɢɫɩɨɥɶɡɭɸɬɫɹ ɟɝɨ ɜɨɡɦɨɠɧɨɫɬɢ, ɫɜɹɡɚɧɧɵɟ ɫ ɩɨɫɬɪɨɟɧɢɟɦ ɞɢɚɝɪɚɦɦ. ɇɢɠɟ ɦɵ ɪɚɫɫɦɨɬɪɢɦ, ɤɚɤ ɷɬɨ ɫɞɟɥɚɬɶ. 6.5.5. ɋɨɡɞɚɧɢɟ ɞɢɚɝɪɚɦɦ Ⱦɢɚɝɪɚɦɦɚɦ Excel ɫɨɨɬɜɟɬɫɬɜɭɟɬ ɨɛɴɟɤɬ Chart, ɤɨɬɨɪɵɣ ɦɨɠɟɬ ɪɚɫɩɨɥɚɝɚɬɶɫɹ ɤɚɤ ɧɚ ɨɬɞɟɥɶɧɨɦ ɥɢɫɬɟ, ɬɚɤ ɢ ɧɚ ɥɢɫɬɟ ɫ ɞɚɧɧɵɦɢ. ȿɫɥɢ ɨɛɴɟɤɬ Chart ɪɚɫɩɨɥɚɝɚɟɬɫɹ ɧɚ ɥɢɫɬɟ ɫ ɞɚɧɧɵɦɢ, ɟɦɭ ɫɨɨɬɜɟɬɫɬɜɭɟɬ ɱɥɟɧ ɤɨɥɥɟɤɰɢɢ ChartObjects ɨɛɴɟɤɬɚ WorkSheet ɢ ɫɨɡɞɚɧɢɟ ɞɢɚɝɪɚɦɦɵ ɧɭɠɧɨ ɧɚɱɚɬɶ ɫ ɞɨɛɚɜɥɟɧɢɹ ɷɥɟɦɟɧɬɚ ɜ ɷɬɭ ɤɨɥɥɟɤɰɢɸ: Ch:=App.WorkBooks[1].WorkSheets[2].ChartObjects.Add(10,50,400,400); ɉɚɪɚɦɟɬɪɚɦɢ ɷɬɨɝɨ ɦɟɬɨɞɚ ɹɜɥɹɸɬɫɹ ɤɨɨɪɞɢɧɚɬɵ ɥɟɜɨɝɨ ɜɟɪɯɧɟɝɨ ɭɝɥɚ ɢ ɪɚɡɦɟɪɵ ɞɢɚɝɪɚɦɦɵ ɜ ɩɭɧɤɬɚɯ (1/72 ɞɸɣɦɚ). ȿɫɥɢ ɠɟ ɞɢɚɝɪɚɦɦɚ ɪɚɫɩɨɥɚɝɚɟɬɫɹ ɧɚ ɨɬɞɟɥɶɧɨɦ ɥɢɫɬɟ (ɧɟ ɩɪɟɞɧɚɡɧɚɱɟɧɧɨɦ ɞɥɹ ɯɪɚɧɟɧɢɹ ɞɚɧɧɵɯ), ɬɨ ɫɨɡɞɚɧɢɟ ɞɢɚɝɪɚɦɦɵ ɧɭɠɧɨ ɧɚɱɚɬɶ ɫ ɞɨɛɚɜɥɟɧɢɹ ɷɥɟɦɟɧɬɚ ɜ ɤɨɥɥɟɤɰɢɸ Sheets ɨɛɴɟɤɬɚ Application (ɤɨɬɨɪɚɹ ɨɬ71
ɥɢɱɚɟɬɫɹ ɨɬ ɤɨɥɥɟɤɰɢɢ WorkSheets ɬɟɦ, ɱɬɨ ɫɨɞɟɪɠɢɬ ɥɢɫɬɵ ɜɫɟɯ ɬɢɩɨɜ, ɚ ɧɟ ɬɨɥɶɤɨ ɥɢɫɬɵ ɫ ɞɚɧɧɵɦɢ): App.WorkBooks[1].Sheets.Add(,,1,xlWBATChart); ȼ ɷɬɨɦ ɫɥɭɱɚɟ ɩɟɪɜɵɣ ɩɚɪɚɦɟɬɪ ɦɟɬɨɞɚ Add ɭɤɚɡɵɜɚɟɬ ɩɨɪɹɞɤɨɜɵɣ ɧɨɦɟɪ ɥɢɫɬɚ, ɩɟɪɟɞ ɤɨɬɨɪɵɦ ɧɭɠɧɨ ɩɨɦɟɫɬɢɬɶ ɞɚɧɧɵɣ ɥɢɫɬ (ɢɥɢ ɞɚɧɧɵɟ ɥɢɫɬɵ, ɟɫɥɢ ɢɯ ɧɟɫɤɨɥɶɤɨ), ɜɬɨɪɨɣ ɩɚɪɚɦɟɬɪ – ɩɨɪɹɞɤɨɜɵɣ ɧɨɦɟɪ ɥɢɫɬɚ, ɩɨɫɥɟ ɤɨɬɨɪɨɝɨ ɧɭɠɧɨ ɩɨɦɟɫɬɢɬɶ ɞɚɧɧɵɣ ɥɢɫɬ (ɢɫɩɨɥɶɡɭɟɬɫɹ ɨɛɵɱɧɨ ɨɞɢɧ ɢɡ ɧɢɯ), ɬɪɟɬɢɣ ɩɚɪɚɦɟɬɪ – ɫɤɨɥɶɤɨ ɧɭɠɧɨ ɫɨɡɞɚɬɶ ɥɢɫɬɨɜ, ɚ ɱɟɬɜɟɪɬɵɣ – ɤɚɤɨɝɨ ɬɢɩɚ ɞɨɥɠɟɧ ɛɵɬɶ ɥɢɫɬ. Ɂɧɚɱɟɧɢɹ ɱɟɬɜɟɪɬɨɝɨ ɩɚɪɚɦɟɬɪɚ ɫɨɜɩɚɞɚɸɬ ɫɨ ɡɧɚɱɟɧɢɹɦɢ ɩɟɪɜɨɝɨ ɩɚɪɚɦɟɬɪɚ ɦɟɬɨɞɚ Add ɤɨɥɥɟɤɰɢɢ WorkBooks ɨɛɴɟɤɬɚ Application, ɢ ɩɪɢ ɢɫɩɨɥɶɡɨɜɚɧɢɢ ɢɦɟɧ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɯ ɤɨɧɫɬɚɧɬ ɫɥɟɞɭɟɬ ɨɩɪɟɞɟɥɢɬɶ ɢɯ ɜ ɩɪɢɥɨɠɟɧɢɢ-ɤɨɧɬɪɨɥɥɟɪɟ. ɉɪɨɫɬɟɣɲɢɣ ɫɩɨɫɨɛ ɫɨɡɞɚɬɶ ɞɢɚɝɪɚɦɦɭ, ɫ ɬɨɱɤɢ ɡɪɟɧɢɹ ɩɨɥɶɡɨɜɚɬɟɥɹ, – ɫɨɡɞɚɬɶ ɟɟ ɫ ɩɨɦɨɳɶɸ ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɝɨ ɷɤɫɩɟɪɬɚ ɧɚ ɨɫɧɨɜɟ ɩɪɹɦɨɭɝɨɥɶɧɨɣ ɨɛɥɚɫɬɢ ɫ ɞɚɧɧɵɦɢ. Ɍɨɱɧɨ ɬɚɤ ɠɟ ɦɨɠɧɨ ɫɨɡɞɚɬɶ ɞɢɚɝɪɚɦɦɭ ɢ ɫ ɩɨɦɨɳɶɸ ɤɨɧɬɪɨɥɥɟɪɚ ɚɜɬɨɦɚɬɢɡɚɰɢɢ – ɞɥɹ ɷɬɨɣ ɰɟɥɢ ɭ ɨɛɴɟɤɬɚ Chart, ɹɜɥɹɸɳɟɝɨɫɹ ɫɜɨɣɫɬɜɨɦ ɨɛɴɟɤɬɚ ChartObject (ɱɥɟɧɚ ɤɨɥɥɟɤɰɢɢ ChartObjects), ɢɦɟɟɬɫɹ ɦɟɬɨɞ ChartWizard. ɉɟɪɜɵɦ ɩɚɪɚɦɟɬɪɨɦ ɷɬɨɝɨ ɦɟɬɨɞɚ ɹɜɥɹɟɬɫɹ ɨɛɴɟɤɬ Range, ɫɨɞɟɪɠɚɳɢɣ ɞɢɚɩɚɡɨɧ ɹɱɟɟɤ ɞɥɹ ɩɨɫɬɪɨɟɧɢɹ ɞɢɚɝɪɚɦɦɵ, ɚ ɜɬɨɪɵɦ – ɱɢɫɥɨɜɨɣ ɩɚɪɚɦɟɬɪ, ɭɤɚɡɵɜɚɸɳɢɣ, ɤɚɤɨɝɨ ɬɢɩɚ ɞɨɥɠɧɚ ɛɵɬɶ ɷɬɚ ɞɢɚɝɪɚɦɦɚ: Var Ch: Variant; … Ch.Chart.ChartWizard(App.WorkBooks[1].WorkSheets[2].Range[‘A1:C5’], xl3DColumn); ȼɨɡɦɨɠɧɵɟ ɡɧɚɱɟɧɢɹ ɩɚɪɚɦɟɬɪɚ, ɨɬɜɟɱɚɸɳɟɝɨ ɡɚ ɬɢɩ ɞɢɚɝɪɚɦɦɵ, ɦɨɠɧɨ ɧɚɣɬɢ ɜ ɫɩɪɚɜɨɱɧɨɦ ɮɚɣɥɟ. ɍ ɨɛɴɟɤɬɚ Chart ɢɦɟɟɬɫɹ ɦɧɨɠɟɫɬɜɨ ɫɜɨɣɫɬɜ, ɨɬɜɟɱɚɸɳɢɯ ɡɚ ɜɧɟɲɧɢɣ ɜɢɞ ɞɢɚɝɪɚɦɦɵ, ɫ ɩɨɦɨɳɶɸ ɤɨɬɨɪɵɯ ɦɨɠɧɨ ɢɡɦɟɧɢɬɶ ɟɟ ɬɨɱɧɨ ɬɚɤ ɠɟ, ɤɚɤ ɩɨɥɶɡɨɜɚɬɟɥɢ ɞɟɥɚɸɬ ɷɬɨ ɜɪɭɱɧɭɸ. ɇɢɠɟ ɩɪɢɜɨɞɢɬɫɹ ɩɪɢɦɟɪ ɫɨɡɞɚɧɢɹ ɡɚɝɨɥɨɜɤɚ ɞɢɚɝɪɚɦɦɵ ɢ ɩɨɞɩɢɫɟɣ ɜɞɨɥɶ ɟɟ ɨɫɟɣ (ɨɬɦɟɬɢɦ, ɱɬɨ ɨɫɢ ɟɫɬɶ ɧɟ ɭ ɜɫɟɯ ɬɢɩɨɜ ɞɢɚɝɪɚɦɦ). Ch.Chart.HasTitle :=1; Ch.Chart.HasLegend := False; Ch.Chart.ChartTitle.Text := ‘ɉɪɢɦɟɪ ɞɢɚɝɪɚɦɦɵ Excel ‘; Ch.Chart.Axes(1).HasTitle := True; Ch.Chart.Axes(1).AxisTitle.Text := ‘ɉɨɞɩɢɫɶ ɜɞɨɥɶ ɨɫɢ ɚɛɫɰɢɫɫ’; Ch.Chart.Axes(2).HasTitle := True; Ch.Chart.Axes(2).AxisTitle.Text := ‘ɉɨɞɩɢɫɶ ɜɞɨɥɶ ɨɫɢ ɨɪɞɢɧɚɬ’; ȿɳɟ ɨɞɢɧ ɫɩɨɫɨɛ ɫɨɡɞɚɧɢɹ ɞɢɚɝɪɚɦɦɵ – ɨɩɪɟɞɟɥɢɬɶ ɜɫɟ ɟɟ ɩɚɪɚɦɟɬɪɵ ɫ ɩɨɦɨɳɶɸ ɫɜɨɣɫɬɜ ɨɛɴɟɤɬɚ Chart, ɜɤɥɸɱɚɹ ɢ ɨɩɪɟɞɟɥɟɧɢɟ ɫɟɪɢɣ, ɧɚ ɨɫɧɨ72
ɜɟ ɤɨɬɨɪɵɯ ɨɧɚ ɞɨɥɠɧɚ ɛɵɬɶ ɩɨɫɬɪɨɟɧɚ. Ⱦɚɧɧɵɟ ɞɥɹ ɫɟɪɢɢ ɨɛɵɱɧɨ ɧɚɯɨɞɢɬɫɹ ɜ ɨɛɴɟɤɬɟ Range, ɫɨɞɟɪɠɚɳɟɦ ɫɬɪɨɤɭ ɢɥɢ ɫɬɨɥɛɟɰ ɞɚɧɧɵɯ, ɚ ɞɨɛɚɜɥɟɧɢɟ ɫɟɪɢɢ ɤ ɞɢɚɝɪɚɦɦɟ ɩɪɨɢɡɜɨɞɢɬɫɹ ɩɭɬɟɦ ɞɨɛɚɜɥɟɧɢɹ ɱɥɟɧɚ ɤ ɤɨɥɥɟɤɰɢɢ SeriesCollection, ɧɚɩɪɢɦɟɪ: App.WorkBooks[1].Sheets.Add(,,1,xlWBATChart); App.WorkBooks[1].Sheets[1].ChartType := xl3DPie; Rng:=App.WorkBooks[1].WorkSheets[2].Range[‘B1:B5’]; App.WorkBooks[1].Sheets[1].SeriesCollection.Add(Rng); ȼ ɞɚɧɧɨɦ ɩɪɢɦɟɪɟ ɤ ɞɢɚɝɪɚɦɦɟ, ɫɨɡɞɚɧɧɨɣ ɧɚ ɨɬɞɟɥɶɧɨɦ ɥɢɫɬɟ, ɫɩɟɰɢɚɥɶɧɨ ɩɪɟɞɧɚɡɧɚɱɟɧɧɨɦ ɞɥɹ ɞɢɚɝɪɚɦɦ, ɞɨɛɚɜɥɹɟɬɫɹ ɨɞɧɚ ɫɟɪɢɹ ɧɚ ɨɫɧɨɜɟ ɞɢɚɩɚɡɨɧɚ ɹɱɟɟɤ ɞɪɭɝɨɝɨ ɥɢɫɬɚ. ȼɨɡɦɨɠɧɨɫɬɢ ɚɜɬɨɦɚɬɢɡɚɰɢɢ Microsoft Excel ɢ Word ɞɚɥɟɤɨ ɧɟ ɢɫɱɟɪɩɵɜɚɸɬɫɹ ɩɪɢɜɟɞɟɧɧɵɦɢ ɩɪɢɦɟɪɚɦɢ. ɋɜɟɞɟɧɢɹ ɨ ɧɢɯ ɦɨɠɧɨ ɜɫɟɝɞɚ ɧɚɣɬɢ ɜ ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɦ ɫɩɪɚɜɨɱɧɨɦ ɮɚɣɥɟ.
6.6. ɉɪɢɦɟɪ ɷɤɫɩɨɪɬɚ ɞɚɧɧɵɯ ɜ Word ȼ ɤɚɱɟɫɬɜɟ ɩɪɢɦɟɪɚ ɪɚɫɫɦɨɬɪɢɦ ɡɚɞɚɱɭ ɮɨɪɦɢɪɨɜɚɧɢɹ ɨɬɱɟɬɚ ɞɚɧɧɵɯ ɢɡ ɡɚɞɚɧɧɨɣ ɛɚɡɵ ɞɚɧɧɵɯ ɜ Microsoft Word: ɧɟɨɛɯɨɞɢɦɨ ɫɮɨɪɦɢɪɨɜɚɬɶ ɫɥɨɠɧɵɣ ɞɨɤɭɦɟɧɬ ɫ ɤɨɥɨɧɬɢɬɭɥɚɦɢ, ɬɚɛɥɢɰɚɦɢ ɢ ɡɚɝɨɥɨɜɤɚɦɢ. Ɍɚɤɠɟ ɞɨɤɭɦɟɧɬ ɞɨɥɠɟɧ ɢɦɟɬɶ ɬɢɬɭɥɶɧɭɸ ɫɬɪɚɧɢɰɭ, ɧɚ ɤɨɬɨɪɨɣ ɩɢɲɟɬɫɹ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɣ ɬɟɤɫɬ, ɫɨ ɜɬɨɪɨɣ ɫɬɪɚɧɢɰɵ ɧɚɱɢɧɚɟɬɫɹ ɜɟɪɯɧɢɣ ɤɨɥɨɧɬɢɬɭɥ, ɝɞɟ ɡɚɞɚɟɬɫɹ ɡɚɝɨɥɨɜɨɤ. Ⱦɥɹ ɩɨɞɤɥɸɱɟɧɢɹ ɤ ɛɚɡɟ ɞɚɧɧɵɯ ɛɭɞɭɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɫɬɚɧɞɚɪɬɧɵɟ ɤɨɦɩɨɧɟɧɬɵ Delphi, ɧɚɩɪɢɦɟɪ, TADODataset. ɋɭɳɟɫɬɜɭɟɬ ɞɜɚ ɫɩɨɫɨɛɚ ɪɟɲɟɧɢɹ ɩɨɫɬɚɜɥɟɧɧɨɣ ɡɚɞɚɱɢ: 1. Ȼɟɡ ɢɫɩɨɥɶɡɨɜɚɧɢɹ ɲɚɛɥɨɧɚ: ɫɨɡɞɚɟɬɫɹ ɧɨɜɵɣ ɞɨɤɭɦɟɧɬ, ɝɞɟ ɫɨɡɞɚɸɬɫɹ ɤɨɥɨɧɬɢɬɭɥɵ ɧɚɞɩɢɫɢ ɢ ɬ.ɞ. ɗɬɨɬ ɦɟɬɨɞ ɞɨɫɬɚɬɨɱɧɨ ɫɥɨɠɟɧ, ɬɚɤ ɤɚɤ ɧɟɨɛɯɨɞɢɦɨ ɭɱɢɬɵɜɚɬɶ ɬɨ, ɱɬɨ ɧɚ ɪɚɡɧɵɯ ɤɨɦɩɶɸɬɟɪɚɯ ɦɨɝɭɬ ɢɡɧɚɱɚɥɶɧɨ ɫɬɨɹɬɶ ɪɚɡɧɵɟ ɧɚɫɬɪɨɣɤɢ ɫɬɪɚɧɢɰɵ, ɲɪɢɮɬɨɜ, ɚɛɡɚɰɟɜ. 2. ɋ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ ɲɚɛɥɨɧɚ: ɡɚɪɚɧɟɟ ɫɨɡɞɚɟɬɫɹ ɞɨɤɭɦɟɧɬ, ɤɨɬɨɪɵɣ ɢɫɩɨɥɶɡɭɟɬɫɹ ɤɚɤ ɲɚɛɥɨɧ. ɇɚɩɪɢɦɟɪ, ɜ ɲɚɛɥɨɧ ɜ ɧɭɠɧɨɦ ɦɟɫɬɟ ɜ ɰɟɧɬɪɟ ɩɟɪɜɨɣ ɫɬɪɚɧɢɰɵ ɫɬɚɜɢɬɫɹ ɨɛɴɟɤɬ ɬɢɩɚ ɧɚɞɩɢɫɶ, ɜ ɤɨɬɨɪɨɣ ɜɵɫɬɚɜɥɹɟɬɫɹ ɧɭɠɧɵɣ ɲɪɢɮɬ ɢ ɜɵɪɚɜɧɢɜɚɧɢɟ. ȼ ɤɨɧɰɟ ɩɟɪɜɨɣ ɫɬɪɚɧɢɰɵ ɞɟɥɚɟɬɫɹ ɪɚɡɪɵɜ ɪɚɡɞɟɥɚ. ɇɚ ɜɬɨɪɨɣ ɫɬɪɚɧɢɰɟ ɜ ɜɟɪɯɧɢɣ ɤɨɥɨɧɬɢɬɭɥ ɬɨɠɟ ɜɫɬɚɜɥɹɟɬɫɹ ɨɛɴɟɤɬ ɬɢɩɚ ɧɚɞɩɢɫɶ. ȼ ɧɟɝɨ ɛɭɞɟɬ ɜɫɬɚɜɥɹɬɶɫɹ ɧɚɡɜɚɧɢɟ ɞɨɤɭɦɟɧɬɚ. Ⱦɚɥɟɟ ɡɚɩɢɫɵɜɚɟɬɫɹ ɦɚɤɪɨɫ, ɜ ɤɨɬɨɪɨɦ ɜɵɩɨɥɧɹɸɬɫɹ ɞɟɣɫɬɜɢɹ, ɤɨɬɨɪɵɟ ɧɟɨɛɯɨɞɢɦɨ ɪɟɚɥɢɡɨɜɚɬɶ ɜ Delphi: 1) ɜ ɨɛɴɟɤɬ ɬɢɩɚ ɧɚɞɩɢɫɶ ɧɚ ɩɟɪɜɨɣ ɫɬɪɚɧɢɰɟ ɜɧɟɫɬɢ ɬɟɤɫɬ; 2) ɩɟɪɟɣɬɢ ɜ ɤɨɧɟɰ ɞɨɤɭɦɟɧɬɚ; 3) ɩɟɪɟɣɬɢ ɜ ɜɟɪɯɧɢɣ ɤɨɥɨɧɬɢɬɭɥ; 4) ɜ ɨɛɴɟɤɬ ɬɢɩɚ ɧɚɞɩɢɫɶ ɜ ɤɨɥɨɧɬɢɬɭɥɟ ɜɧɟɫɬɢ ɬɟɤɫɬ; 5) ɜɟɪɧɭɬɶɫɹ ɧɚ ɜɬɨɪɭɸ ɫɬɪɚɧɢɰɭ. 73
ȼ ɪɟɡɭɥɶɬɚɬɟ ɩɨɥɭɱɚɟɬɫɹ ɫɥɟɞɭɸɳɢɣ ɦɚɤɪɨɫ: Sub Ɇɚɤɪɨɫ1() ActiveDocument.Shapes("Text Box 8").Select Selection.TypeText Text:="ɬɟɤɫɬ ɧɚ ɬɢɬɭɥɶɧɨɣ" Selection.EndKey Unit:=wdStory If ActiveWindow.View.SplitSpecial <> wdPaneNone Then ActiveWindow.Panes(2).Close End If If ActiveWindow.ActivePane.View.Type = wdNormalView Or _ ActiveWindow.ActivePane.View.Type = wdOutlineView Then ActiveWindow.ActivePane.View.Type = wdPrintView End If ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader Selection.HeaderFooter.Shapes("Text Box 5").Select Selection.TypeText Text:="Ɍɟɤɫɬ ɜ ɤɨɥɨɧɬɢɬɭɥɟ" ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument End Sub ȿɫɥɢ ɩɪɟɨɛɪɚɡɨɜɚɬɶ ɜ ɤɨɞ Delphi, ɬɨ ɩɨɥɭɱɢɦ: //ɜɵɛɢɪɚɟɬɫɹ ɩɟɪɜɚɹ ɧɚɞɩɢɫɶ Vr := 'Text Box 8'; Word.ActiveDocument.Shapes.Item(vr).Select; //ɩɢɲɟɬɫɹ ɬɟɤɫɬ Word.Selection.TypeText('ɬɟɤɫɬ ɧɚ ɬɢɬɭɥɶɧɨɣ'); //ɩɟɪɟɯɨɞ ɜ ɤɨɧɟɰ ɞɨɤɭɦɟɧɬɚ Vr := wdStory; Word.Selection.EndKey(Vr); //ɨɩɟɪɚɬɨɪɵ if ɡɞɟɫɶ ɧɟ ɧɭɠɧɵ //ɩɟɪɟɯɨɞ ɜ ɜɟɪɯɧɢɣ ɤɨɥɨɧɬɢɬɭɥ Word.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageHeader; Vr := 'Text Box 5'; Word.Selection.HeaderFooter.Shapes.Item(Vr).Select; Word.Selection.TypeText('ɬɟɤɫɬ ɜ ɤɨɥɨɧɬɢɬɭɥɟ'); Word.ActiveWindow.ActivePane.View.SeekView := wdSeekMainDocument;
74
Ⱥɧɚɥɨɝɢɱɧɵɦ ɨɛɪɚɡɨɦ ɦɨɠɧɨ ɩɨɥɭɱɢɬɶ ɤɨɞ ɜɜɨɞɚ ɬɟɤɫɬɚ ɜ ɤɨɥɨɧɬɢɬɭɥɵ, ɧɚɫɬɪɨɣɤɭ ɲɪɢɮɬɨɜ, ɚɛɡɚɰɟɜ ɢ ɬ. ɩ. Ɋɟɲɟɧɢɟ ɩɨɫɬɚɜɥɟɧɧɨɣ ɡɚɞɚɱɢ ɩɪɢɜɟɞɟɧɨ ɜ ɫɥɟɞɭɸɳɟɦ ɥɢɫɬɢɧɝɟ: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, DB, ADODB, ActiveX; type TForm1 = class(TForm) ADOConnection1: TADOConnection; ADODataSet1: TADODataSet; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } procedure TableExport(Word,Document,Selection: Variant; DataSet: TDataSet; Title,FlagText: String); public { Public declarations } end; var Form1: TForm1; implementation uses ComObj; {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var Word: Variant; Document: Variant; Selection: Variant; Vr: OleVariant; begin 75
Screen.Cursor := crHourGlass; //ɫɨɡɞɚɟɬɫɹ ɫɟɪɜɟɪ ɚɜɬɨɦɚɬɢɡɚɰɢɢ Word Word := CreateOleObject('Word.Application'); try //ɫɨɡɞɚɟɬɫɹ ɧɨɜɵɣ ɞɨɤɭɦɟɧɬ ɩɨ ɲɚɛɥɨɧɭ Document := Word.Documents.Add(GetCurrentDir+'\Shablon.doc'); //ɜɵɛɢɪɚɟɬɫɹ ɩɟɪɜɚɹ ɧɚɞɩɢɫɶ Vr := 'Text Box 8'; Document.Shapes.Item(Vr).Select; //ɡɚɩɨɦɢɧɚɟɬɫɹ ɫɫɵɥɤɚ ɧɚ ɨɛɴɟɤɬ Selection Selection := Word.Selection; //ɩɢɲɟɬɫɹ ɬɟɤɫɬ Selection.TypeText('ɬɟɤɫɬ ɧɚ ɬɢɬɭɥɶɧɨɣ'); //ɩɟɪɟɯɨɞ ɜ ɤɨɧɟɰ ɞɨɤɭɦɟɧɬɚ Vr := wdStory; Selection.EndKey(Vr); //ɩɟɪɟɯɨɞ ɜ ɜɟɪɯɧɢɣ ɤɨɥɨɧɬɢɬɭɥ Word.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageHeader; Vr := 'Text Box 5'; Selection.HeaderFooter.Shapes.Item(Vr).Select; Word.Selection.TypeText('ɬɟɤɫɬ ɜ ɤɨɥɨɧɬɢɬɭɥɟ'); Word.ActiveWindow.ActivePane.View.SeekView := wdSeekMainDocument; ADODataSet1.Close; ADODataSet1.CommandText := 'SELECT [Main].[Tel], [Main].[name], '+ '[Main].[House], [Korp].[NameKorp], [Flat].[NameFlat], [Ul].[NameUl] '+ ' FROM Main, Ul, Korp, Flat WHERE ([Main].[IdUl]=[Ul].[IdUl]) And '+ '([Main].[IdKorp]=[Korp].[IdKorp]) And '+ '([Main].[IdFlat]=[Flat].[IdFlat]) '; ADODataSet1.Open; //ɧɚɫɬɪɚɢɜɚɸɬɫɹ ɩɚɪɚɦɟɬɪɵ ɷɤɫɩɨɪɬɚ //ɡɚɝɨɥɨɜɨɤ ADODataSet1.fields[0].DisplayLabel := 'Ɍɟɥɟɮɨɧ'; //ɲɢɪɢɧɚ ɫɬɨɥɛɰɚ ADODataSet1.fields[0].Tag := round(Word.CentimetersToPoints(2)); //ɨɬɨɛɪɚɠɚɬɶ ɩɨɥɟ DataSet1.fields[0].Visible := True; 76
//ɡɚɝɨɥɨɜɨɤ ADODataSet1.fields[1].DisplayLabel := 'ɎɂɈ'; //ɲɢɪɢɧɚ ɫɬɨɥɛɰɚ ADODataSet1.fields[1].Tag := Round(Word.CentimetersToPoints(10)); //ɨɬɨɛɪɚɠɚɬɶ ɩɨɥɟ ADODataSet1.fields[1].Visible := True; //ɡɚɝɨɥɨɜɨɤ ADODataSet1.fields[2].DisplayLabel := 'Ⱦɨɦ'; //ɲɢɪɢɧɚ ɫɬɨɥɛɰɚ ADODataSet1.fields[2].Tag:= Round(Word.CentimetersToPoints(1.5)); //ɨɬɨɛɪɚɠɚɬɶ ɩɨɥɟ ADODataSet1.fields[2].Visible := True; //ɡɚɝɨɥɨɜɨɤ ADODataSet1.fields[3].DisplayLabel := 'Ʉɨɪɩɭɫ'; //ɲɢɪɢɧɚ ɫɬɨɥɛɰɚ ADODataSet1.fields[3].Tag := Round(Word.CentimetersToPoints(1.5)); //ɨɬɨɛɪɚɠɚɬɶ ɩɨɥɟ ADODataSet1.fields[3].Visible := True; //ɡɚɝɨɥɨɜɨɤ ADODataSet1.fields[4].DisplayLabel := 'Ʉɜɚɪɬɢɪɚ'; //ɲɢɪɢɧɚ ɫɬɨɥɛɰɚ ADODataSet1.fields[4].Tag := Round(Word.CentimetersToPoints(1.5)); //ɨɬɨɛɪɚɠɚɬɶ ɩɨɥɟ ADODataSet1.fields[4].Visible := True; //ɧɟ ɨɬɨɛɪɚɠɚɬɶ ɩɨɥɟ ADODataSet1.fields[5].Visible := False; //ɜɵɡɵɜɚɟɬɫɹ ɩɪɨɰɟɞɭɪɚ ɷɤɫɩɨɪɬɚ TableExport(Word,Document,Selection,ADODataSet1,'ɀɢɜɭɳɢɟ ɧɚ ɭɥɢɰɟ Ⱥɯɦɟɬɨɜɚ','ȺɏɆȿɌɈȼȺ'); 77
TableExport(Word,Document,Selection,ADODataSet1,'ɀɢɜɭɳɢɟ ɧɚ ɭɥɢɰɟ Ʌɟɬɱɢɤɨɜ','ɅȿɌɑɂɄɈȼ'); finally //ɨɬɨɛɪɚɠɟɬɫɹ Word Word.Visible := True; //ɧɟ ɡɚɛɭɞɶɬɟ ɭɞɚɥɢɬɶ ɫɫɵɥɤɢ!!! Selection := Null; Document := Null; Word := Null; Screen.Cursor := crDefault; end; end; procedure TForm1.TableExport(Word,Document,Selection: Variant; DataSet: TDataSet; Title,FlagText: String); var I,ColCount: Integer; //ɤɨɥɢɱɟɫɬɜɨ ɤɨɥɨɧɨɤ ɜ ɬɚɛɥɢɰɟ TableBeg: Integer; //ɇɨɦɟɪ ɫɢɦɜɨɥɚ ɜ ɧɚɱɚɥɟ ɬɚɛɥɢɰɵ TableBeg2: Integer; //ɇɨɦɟɪ ɫɢɦɜɨɥɚ ɜ ɧɚɱɚɥɟ ɞɚɧɧɵɯ ɬɚɛɥɢɰɵ Vr1,Vr2: OleVariant; F: Boolean; St: String; function ConvertString(S: String): String; {ɉɪɢ ɮɨɪɦɢɪɨɜɚɧɢɢ ɬɚɛɥɢɰɵ ɜ ɤɚɱɟɫɬɜɟ ɪɚɡɞɟɥɢɬɟɥɹ ɩɨ ɭɦɨɥɱɚɧɢɸ ɢɫɩɨɥɶɡɭɟɬɫɹ "-", ɤɨɬɨɪɵɣ ɧɟɨɛɯɨɞɢɦɨ ɡɚɦɟɧɢɬɶ ɧɚ ɚɧɚɥɨɝɢɱɧɵɣ ɫɢɦɜɨɥ ɫ ɤɨɞɨɦ #173} begin Result := StringReplace(S,'-',#173,[]); end; begin {ɉɪɨɰɟɞɭɪɚ ɷɤɫɩɨɪɬɢɪɭɟɬ ɥɢɲɶ ɬɟ ɡɚɩɢɫɢ, ɭ ɤɨɬɨɪɵɯ ɡɧɚɱɟɧɢɟ ɩɨɫɥɟɞɧɟɝɨ ɩɨɥɹ ɫɨɜɩɚɞɚɟɬ ɫ FlagText. ȿɫɥɢ FlagText=,'' ɬɨ ɷɤɫɩɨɪɬɢɪɭɸɬɫɹ ɜɫɟ ɡɚɩɢɫɢ} Vr1 := wdStory; //ɩɟɪɟɯɨɞ ɜ ɤɨɧɟɰ ɞɨɤɭɦɟɧɬɚ Selection.EndKey(Vr1); //ɜɫɬɚɜɥɹɟɬɫɹ ɡɚɝɨɥɨɜɨɤ ɬɚɛɥɢɰɵ Document.Range.InsertAfter(Title); //ɧɚɫɬɪɨɣɤɢ ɫɬɢɥɹ ɚɛɡɚɰɚ Document.Paragraphs.Item(Document.Paragraphs.Count).Range.Select; 78
Selection.ParagraphFormat.KeepWithNext := -1; Selection.ParagraphFormat.SpaceAfter := 14; //ɡɚɞɚɟɬɫɹ ɲɪɢɮɬ Selection.Font.Size := 15; WSelection.Font.bold := 1; //ɞɨɛɚɜɥɹɟɬɫɹ ɫɬɪɨɤɚ Document.Paragraphs.Add; Document.Paragraphs.Item(Document.Paragraphs.Count).Range.Select; Selection.ParagraphFormat.SpaceAfter := 0; Vr1 := wdStory; //ɩɟɪɟɯɨɞ ɜ ɤɨɧɟɰ ɞɨɤɭɦɟɧɬɚ Selection.EndKey(Vr1); //ɡɚɩɨɦɢɧɚɟɬɫɹ ɩɨɥɨɠɟɧɢɟ ɤɭɪɫɨɪɚ. ɗɬɨ ɧɚɱɚɥɨ ɛɭɞɭɳɟɣ ɬɚɛɥɢɰɵ. //ɡɚɬɟɦ ɜɵɛɢɪɚɟɬɫɹ ɜɟɫɶ ɨɫɬɚɜɲɢɣɫɹ ɬɟɤɫɬ, ɱɬɨɛɵ ɩɪɟɨɛɪɚɡɨɜɚɬɶ //ɟɝɨ ɜ ɬɚɛɥɢɰɭ TableBeg := Selection.End; DataSet.First; //ɜɫɬɚɜɥɹɸɬɫɹ ɡɚɝɨɥɨɜɤɢ ɞɥɹ ɜɫɟɯ ɜɢɞɢɦɵɯ ɩɨɥɟɣ for I := 0 to DataSet.FieldCount-1 do if DataSet.Fields[i].Visible then Document.Range.InsertAfter (ɋonvertString(DataSet.Fields[I]. DisplayLabel)+#9); Selection.EndKey(Vr1); //ɭɛɢɪɚɸɬɫɹ ɩɨɫɥɟɞɧɢɟ ɫɢɦɜɨɥɵ ɬɚɛɭɥɹɰɢɢ {ɋɢɦɜɨɥ ɬɚɛɭɥɹɰɢɢ ɢɫɩɨɥɶɡɭɟɬɫɹ ɜ ɤɚɱɟɫɬɜɟ ɪɚɡɞɟɥɢɬɟɥɹ ɞɥɹ ɫɬɨɥɛɰɨɜ ɬɚɛɥɢɰɵ} Selection.TypeBackspace; //ɩɪɢɦɟɧɹɟɬɫɹ ɲɪɢɮɬ Document.Paragraphs.Item(Document.Paragraphs.Count).Range.Select; Selection.Font.Size := 14; Selection.Font.Italic := 1; Selection.Font.Bold := 0; //ɞɨɛɚɜɥɹɟɬɫɹ ɫɬɪɨɤɚ Document.Paragraphs.Add; //Ɉɩɪɟɞɟɥɟɧɢɟ, ɛɵɥɢ ɥɢ ɜ ɬɚɛɥɢɰɟ ɜɨɨɛɳɟ ɡɚɩɢɫɢ ɞɥɹ ɷɤɫɩɨɪɬɚ F := True; //ɋɬɪɨɤɚ ɞɥɹ ɷɤɫɩɨɪɬɚ ɬɟɤɫɬɚ ɬɚɛɥɢɰɵ St := ''; 79
//ɧɚɱɚɥɨ ɞɚɧɧɵɯ ɜ ɬɚɛɥɢɰɟ TableBeg2 := Selection.End; if Dataset.RecordCount > 0 then begin repeat if (Dataset.fields[DataSet.Fields.Count-1].AsString = FlagText) or (FlagText = '') then begin for I := 0 to DataSet.FieldCount-1 do if DataSet.Fields[I].Visible then //ɱɟɪɟɡ ɬɚɛɭɥɹɰɢɸ ɜɵɜɨɞɹɬɫɹ ɜɫɟ ɜɢɞɢɦɵɟ ɩɨɥɹ St := St+DataSet.Fields[I].AsString+#9; //ɭɛɢɪɚɟɬɫɹ ɩɨɫɥɟɞɧɢɣ ɫɢɦɜɨɥ ɬɚɛɭɥɹɰɢɢ SetLength(St,Length(St)-1); //ɩɟɪɟɧɨɫ ɫɬɪɨɤɢ St := St+#13; F := False; end; Dataset.Next; until Dataset.Eof; //ɩɟɪɟɯɨɞ ɜ ɤɨɧɟɰ ɬɟɤɫɬɚ Selection.EndKey(Vr1); //ɜɫɬɚɜɤɚ ɞɚɧɧɵɯ ɬɚɛɥɢɰɵ Selection.InsertAfter(ɋonvertString(St)); //ɧɚɱɚɥɨ ɞɚɧɧɵɯ ɬɚɛɥɢɰɵ Vr1 := TableBeg2; //ɤɨɧɟɰ ɬɚɛɥɢɰɵ Vr2 := Selection.End; Selection.Font.Size := 12; Selection.Font.Bold := 0; Selection.Font.Italic := 0; end; //ɜ ɬɨɦ ɫɥɭɱɚɟ, ɟɫɥɢ ɧɟ ɷɤɫɩɨɪɬɢɪɨɜɚɥɨɫɶ ɧɢ ɨɞɧɨɣ ɡɚɩɢɫɢ //ɮɨɪɦɢɪɭɟɬɫɹ ɩɭɫɬɚɹ ɫɬɪɨɤɚ if F then begin for I := 0 to DataSet.FieldCount-1 do 80
if DataSet.Fields[I].Visible then Document.Range.InsertAfter(' '+#9); Selection.EndKey(Vr1,EmptyParam);
end; end. ɉɪɢ ɧɚɠɚɬɢɢ ɧɚ ɤɧɨɩɤɭ Button1 ɜɵɩɨɥɧɹɟɬɫɹ ɩɪɨɰɟɞɭɪɚ Button1Click, ɜ ɤɨɬɨɪɨɣ ɫɨɡɞɚɸɬɫɹ ɨɛɴɟɤɬɵ Word ɢ Document, ɡɚɞɚɸɬɫɹ ɬɢɬɭɥɶɧɵɣ ɥɢɫɬ ɢ ɤɨɥɨɧɬɢɬɭɥɵ, ɡɚɬɟɦ ɜɵɡɵɜɚɟɬɫɹ ɩɪɨɰɟɞɭɪɚ TableExport – ɷɤɫɩɨɪɬɚ ɞɚɧɧɵɯ ɜ ɬɚɛɥɢɰɭ Word. ȼ ɞɚɧɧɨɣ ɩɪɨɝɪɚɦɦɟ ɢɫɩɨɥɶɡɨɜɚɥɨɫɶ ɩɨɡɞɧɟɟ ɫɜɹɡɵɜɚɧɢɟ. ɑɬɨɛɵ ɫɨɤɪɚɬɢɬɶ ɤɨɥɢɱɟɫɬɜɨ ɨɛɪɚɳɟɧɢɣ ɤ ɢɧɬɟɪɮɟɣɫɚɦ ɫɫɵɥɤɢ ɧɚ ɨɛɴɟɤɬɵ, Document ɢ Select ɯɪɚɧɹɬɫɹ ɜ ɨɬɞɟɥɶɧɵɯ ɩɟɪɟɦɟɧɧɵɯ. ɉɨ ɨɤɨɧɱɚɧɢɢ ɪɚɛɨɬɵ ɜɫɟ ɫɫɵɥɤɢ ɧɚ ɨɛɴɟɤɬɵ ɫɟɪɜɟɪɚ ɚɜɬɨɦɚɬɢɡɚɰɢɢ ɧɟɨɛɯɨɞɢɦɨ ɭɞɚɥɢɬɶ, ɱɬɨ ɞɨɫɬɢɝɚɟɬɫɹ ɩɪɢɫɜɨɟɧɢɟɦ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɦ ɩɟɪɟɦɟɧɧɵɦ ɡɧɚɱɟɧɢɹ Null.
Selection.TypeBackspace; end; //ɧɚɱɚɥɨ ɛɭɞɭɳɟɣ ɬɚɛɥɢɰɵ Vr1 := TableBeg; //ɤɨɧɟɰ ɛɭɞɭɳɟɣ ɬɚɛɥɢɰɵ Vr2 := Selection.End; //ɜɵɛɢɪɚɟɬɫɹ ɞɢɚɩɚɡɨɧ Document.Range(Vr1,Vr2).Select; //ɩɪɟɨɛɪɚɡɨɜɚɧɢɟ ɜ ɬɚɛɥɢɰɭ Selection.ConvertToTable; ɋolɋount := 1; //ɡɚɞɚɸɬɫɹ ɲɢɪɢɧɵ ɤɨɥɨɧɨɤ for I := 0 to DataSet.FieldCount-1 do if DataSet.Fields[I].Visible then begin Document.Tables.Item(Document.Tables.Count).Columns.Item(ColCount). Width := DataSet.Fields[i].Tag; Inc(ColCount); end; //ɡɚɞɚɸɬɫɹ ɝɪɚɧɢɰɵ Selection.Cells.Borders.Item (wdBorderLeft). LineStyle:=wdLineStyleSingle; Selection.Cells.Borders.Item(wdBorderRight).LineStyle:= wdLineStyleSingle; Selection.Cells.Borders.Item(wdBorderHorizontal).LineStyle:= wdLineStyleSingle; Selection.Cells.Borders.Item(wdBorderTop).LineStyle:= wdLineStyleSingle; Selection.Cells.Borders.Item(wdBorderBottom).LineStyle:= wdLineStyleSingle; Selection.Cells.Borders.Item(wdBorderVertical).LineStyle:= wdLineStyleSingle; Document.Paragraphs.Add; Document.Paragraphs.Item(Document.Paragraphs.Count-1). Range.Select; Selection.ParagraphFormat.KeepWithNext:=0; 81
Ɂɚɞɚɱɢ «Ʉɥɢɟɧɬ-ɋɟɪɜɟɪ» ȼɨ ɜɫɟɯ ɜ ɜɚɪɢɚɧɬɚɯ ɡɚɞɚɱ ɬɪɟɛɭɟɬɫɹ ɫɨɡɞɚɬɶ COM-ɫɟɪɜɟɪ, ɩɪɟɞɨɫɬɚɜɥɹɸɳɢɣ ɭɤɚɡɚɧɧɵɣ ɢɧɬɟɪɮɟɣɫ, ɢ ɪɟɚɥɢɡɨɜɚɬɶ ɩɪɢɥɨɠɟɧɢɟ-ɤɥɢɟɧɬ, ɢɫɩɨɥɶɡɭɸɳɟɟ ɮɭɧɤɰɢɢ ɫɟɪɜɟɪɚ: 1.
ȼɵɱɢɫɥɟɧɢɟ ɬɪɢɝɨɧɨɦɟɬɪɢɱɟɫɤɢɯ ɮɭɧɤɰɢɣ.
2. Ʉɚɥɶɤɭɥɹɬɨɪ (ɩɨɞɞɟɪɠɢɜɚɸɳɢɣ ɫɥɨɠɟɧɢɟ, ɜɵɱɢɬɚɧɢɟ, ɭɦɧɨɠɟɧɢɟ, ɞɟɥɟɧɢɟ). 3. Ɉɛɪɚɛɨɬɤɚ ɮɚɣɥɚ: ɧɚɯɨɠɞɟɧɢɟ ɞɥɢɧɵ ɮɚɣɥɚ (ɜ ɫɢɦɜɨɥɚɯ), ɧɚɯɨɠɞɟɧɢɟ ɫɚɦɨɝɨ ɞɥɢɧɧɨɝɨ ɫɥɨɜɚ. 4. Ɉɛɪɚɛɨɬɤɚ ɮɚɣɥɨɜ: ɫɪɚɜɧɟɧɢɟ ɞɜɭɯ ɮɚɣɥɨɜ, ɧɚɯɨɠɞɟɧɢɟ ɨɞɢɧɚɤɨɜɵɯ ɫɥɨɜ ɜ ɮɚɣɥɚɯ, ɨɛɴɟɞɢɧɟɧɢɟ ɮɚɣɥɨɜ. 5.
Ɋɟɚɥɢɡɚɰɢɹ ɮɚɣɥɨɜɵɯ ɨɩɟɪɚɰɢɣ (ɨɬɤɪɵɬɢɟ, ɱɬɟɧɢɟ, ɡɚɩɢɫɶ ɢ ɬ. ɞ.).
6.
ɋɨɪɬɢɪɨɜɤɚ ɦɚɫɫɢɜɚ (ɥɸɛɵɦ ɫɩɨɫɨɛɨɦ).
7. Ɉɛɪɚɛɨɬɤɚ ɢɡɨɛɪɚɠɟɧɢɹ (ɨɫɜɟɬɥɟɧɢɟ, ɡɚɬɟɦɧɟɧɢɟ, ɭɫɬɚɧɨɜɤɚ ɩɪɨɡɪɚɱɧɨɫɬɢ). 8. Ʉɨɞɢɪɨɜɚɧɢɟ ɢɡɨɛɪɚɠɟɧɢɹ ɩɨ ɡɚɞɚɧɧɨɦɭ ɤɥɸɱɭ (ɧɚɩɪɢɦɟɪ, ɨɩɟɪɚɰɢɟɣ XOR). 9.
ɒɢɮɪɨɜɚɧɢɟ ɬɟɤɫɬɚ ɦɟɬɨɞɨɦ XOR.
10. Ɋɚɛɨɬɚ ɫɨ ɫɬɪɨɤɨɣ ɬɟɤɫɬɚ: ɜɵɱɢɫɥɟɧɢɟ ɞɥɢɧɵ, ɧɚɯɨɠɞɟɧɢɟ ɩɨɞɫɬɪɨɤɢ, ɧɚɯɨɠɞɟɧɢɟ ɤɨɥɢɱɟɫɬɜɚ ɜɯɨɠɞɟɧɢɣ ɩɨɞɫɬɪɨɤɢ. 11. Ɉɛɪɚɛɨɬɤɚ ɫɬɪɨɤɢ: ɩɟɪɟɜɨɞ ɫɬɪɨɤɢ ɜ ɜɟɪɯɧɢɣ ɢɥɢ ɧɢɠɧɢɣ ɪɟɝɢɫɬɪ. 12. ɉɟɪɟɜɨɞ ɱɢɫɟɥ ɜ ɬɟɤɫɬɨɜɨɟ ɩɪɟɞɫɬɚɜɥɟɧɢɟ (ɧɚɩɪɢɦɟɪ, 111 ĺ 'ɫɬɨ ɨɞɢɧɧɚɞɰɚɬɶ'). ɑɢɫɥɚ ɦɨɝɭɬ ɛɵɬɶ ɞɨ 1000000. 82
13. ɉɟɪɟɜɨɞ ɞɚɬ ɜ ɪɚɡɥɢɱɧɵɟ ɬɟɤɫɬɨɜɵɟ ɩɪɟɞɫɬɚɜɥɟɧɢɹ. 14. Ⱥɧɚɥɢɡ html-ɞɨɤɭɦɟɧɬɨɜ. ɇɟɨɛɯɨɞɢɦɨ ɨɛɟɫɩɟɱɢɬɶ ɨɩɟɪɚɰɢɢ ɢɡɜɥɟɱɟɧɢɹ ɫɩɢɫɤɚ ɚɞɪɟɫɨɜ (url) ɜɫɟɯ ɢɡɨɛɪɚɠɟɧɢɣ, ɫɨɞɟɪɠɚɳɢɯɫɹ ɜ ɞɨɤɭɦɟɧɬɟ, ɜɫɟɯ ɫɫɵɥɨɤ ɢ ɜɫɟɯ ɮɨɪɦ, ɚ ɬɚɤɠɟ ɢɡɜɥɟɱɟɧɢɟ ɬɟɤɫɬɨɜɨɝɨ ɫɨɞɟɪɠɚɧɢɹ ɞɨɤɭɦɟɧɬɚ (ɬ. ɟ. ɭɞɚɥɟɧɢɟ ɜɫɟɯ ɬɟɝɨɜ). 15. ɉɨɢɫɤ ɤɪɚɬɱɚɣɲɟɝɨ ɩɭɬɢ ɧɚ ɝɪɚɮɟ. ɉɪɟɞɭɫɦɨɬɪɟɬɶ ɜɨɡɦɨɠɧɨɫɬɶ ɡɚɝɪɭɡɤɢ ɝɪɚɮɚ ɢɡ ɬɟɤɫɬɨɜɨɝɨ ɩɪɟɞɫɬɚɜɥɟɧɢɹ/ɮɚɣɥɚ (ɬ. ɟ. ɬɚɤɠɟ ɬɪɟɛɭɟɬɫɹ ɪɚɡɪɚɛɨɬɚɬɶ ɮɨɪɦɚɬ ɬɟɤɫɬɨɜɨɝɨ ɩɪɟɞɫɬɚɜɥɟɧɢɹ). 16. ɉɟɪɟɜɨɞ ɬɟɤɫɬɚ. ɉɨɞ ɩɟɪɟɜɨɞɨɦ ɩɨɧɢɦɚɟɬɫɹ ɩɪɨɫɬɚɹ ɡɚɦɟɧɚ ɫɥɨɜ. Ɍɚɤɠɟ ɧɟɨɛɯɨɞɢɦɨ ɪɟɚɥɢɡɨɜɚɬɶ ɮɭɧɤɰɢɢ ɡɚɝɪɭɡɤɢ ɫɥɨɜɚɪɟɣ. 17. ȼɵɱɢɫɥɟɧɢɟ ɡɧɚɱɟɧɢɣ ɮɭɧɤɰɢɣ ɨɞɧɨɣ ɩɟɪɟɦɟɧɧɨɣ, ɡɚɞɚɧɧɵɯ ɜ ɬɟɤɫɬɨɜɨɣ ɮɨɪɦɟ, ɧɚɩɪɢɦɟɪ '=x^2+3'. ɂɧɬɟɪɩɪɟɬɚɬɨɪ ɞɨɥɠɟɧ ɩɨɞɞɟɪɠɢɜɚɬɶ ɤɚɤ ɩɪɨɫɬɵɟ ɦɚɬɟɦɚɬɢɱɟɫɤɢɟ ɨɩɟɪɚɰɢɢ (+, –, *, /, ^), ɬɚɤ ɢ ɧɟɤɨɬɨɪɵɣ ɧɚɛɨɪ ɮɭɧɤɰɢɣ (Cos, Sin ɢ ɬ. ɞ.). 18. ȼɵɩɨɥɧɢɬɶ ɩɪɟɞɵɞɭɳɭɸ ɡɚɞɚɱɭ ɞɥɹ ɮɭɧɤɰɢɣ ɧɟɫɤɨɥɶɤɢɯ ɩɟɪɟɦɟɧɧɵɯ. 19. ɏɟɲ-ɬɚɛɥɢɰɚ (ɞɥɹ ɯɪɚɧɟɧɢɹ ɱɢɫɟɥ, ɡɚɩɢɫɟɣ, ɫɬɪɨɤ ɢ ɬ. ɩ.). 20. Ɂɚɝɪɭɡɤɚ ɚɜɬɨɦɨɛɢɥɟɣ. ȿɫɬɶ ɫɩɢɫɨɤ ɚɜɬɨɦɨɛɢɥɟɣ ɢ ɫɩɢɫɨɤ ɝɪɭɡɨɜ, ɬɪɟɛɭɟɬɫɹ ɪɚɫɩɪɟɞɟɥɢɬɶ ɝɪɭɡ ɦɟɠɞɭ ɚɜɬɨɦɨɛɢɥɹɦɢ ɢ ɜ ɤɚɱɟɫɬɜɟ ɪɟɡɭɥɶɬɚɬɚ ɜɵɞɚɬɶ ɫɩɢɫɨɤ ɝɪɭɡɨɜ ɞɥɹ ɤɚɠɞɨɝɨ ɚɜɬɨɦɨɛɢɥɹ.
83
ɋɩɢɫɨɤ ɥɢɬɟɪɚɬɭɪɵ 1. Ⱥɪɯɚɧɝɟɥɶɫɤɢɣ Ⱥ. ə. ɉɪɨɝɪɚɦɦɢɪɨɜɚɧɢɟ ɜ Delphi 6 / Ⱥ. ə. Ⱥɪɯɚɧɝɟɥɶɫɤɢɣ. – Ɇ. : ȻɂɇɈɆ, 2002. – 1120 ɫ. 2. Ȼɨɤɫ Ⱦ. ɋɭɳɧɨɫɬɶ ɬɟɯɧɨɥɨɝɢɢ COM. Ȼɢɛɥɢɨɬɟɤɚ ɩɪɨɝɪɚɦɦɢɫɬɚ / Ⱦ. Ȼɨɤɫ. – ɋɉɛ. : ɉɢɬɟɪ, 2001. – 400 ɫ. 3. Ʌɟɣɧɟɤɟɪ Ɋ. COM+. ɗɧɰɢɤɥɨɩɟɞɢɹ ɩɪɨɝɪɚɦɦɢɫɬɚ / Ɋ. Ʌɟɣɧɟɤɟɪ. – Ɇ. : Ⱦɢɚɋɨɮɬ, 2002. – 656 ɫ. 4. Ɋɨɞɠɟɪɫɨɧ Ⱦ. Ɉɫɧɨɜɵ COM / Ⱦ. Ɋɨɞɠɟɪɫɨɧ ; ɩɟɪ. ɫ ɚɧɝɥ. – 2-ɟ ɢɡɞ., ɢɫɩɪ. ɢ ɞɨɩ. – Ɇ. : Ɋɭɫɫɤɚɹ Ɋɟɞɚɤɰɢɹ, 2000. – 400 ɫ. 5. Ɋɨɮɷɣɥ ɗ. COM ɢ COM+. ɉɨɥɧɨɟ ɪɭɤɨɜɨɞɫɬɜɨ / ɗ. Ɋɨɮɷɣɥ, ə. ɒɨɯɚɭɞ ; ɩɟɪ. ɫ ɚɧɝɥ. – Ʉɢɟɜ : ȼȿɄ+, Ɇ. : ɗɧɬɪɨɩ, 2000. – 560 ɫ. 6. Ɍɟɣɤɫɟɣɪɚ ɋ. Delphi 4. Ɋɭɤɨɜɨɞɫɬɜɨ ɪɚɡɪɚɛɨɬɱɢɤɚ / ɋ. Ɍɟɣɤɫɟɣɪɚ, Ʉ. ɉɚɱɟɤɨ ; ɩɟɪ. ɫ ɚɧɝɥ. – ɋɩɛ. : ȼɢɥɶɹɦɫ, 1999. – 912 ɫ. 7. ɏɚɪɦɨɧ ɗ. Ɋɚɡɪɚɛɨɬɤɚ COM-ɩɪɢɥɨɠɟɧɢɣ ɜ ɫɪɟɞɟ Delphi : ɭɱɟɛ. ɩɨɫɨɛɢɟ / ɗ. ɏɚɪɦɨɧ. – Ɇ. : ȼɢɥɶɹɦɫ, 2000. – 464 ɫ.
ɋɫɵɥɤɢ ɜ Internet 1. MSDN Library. Microsoft Corporation. – http://msdn.microsoft.com/library/ en-us/fileio/base/file_management_functions.asp 2. Ƚɥɭɳɟɧɤɨ ɘ. ɗɤɫɩɨɪɬ ɢɡ ȻȾ ɜ Word. – http://www.delphimaster.ru/ articles/dbtoword/index.html. 3. ȿɥɦɚɧɨɜɚ ɇ. Ⱥɜɬɨɦɚɬɢɡɚɰɢɹ ɩɪɢɥɨɠɟɧɢɣ Microsoft Office ɜ ɩɪɢɦɟɪɚɯ ɑɚɫɬɶ 1. Microsoft Word ɢ Microsoft Excel. – http://www.compress.ru/ Archive/CP/2000/11/25/. 4. Ʉɨɪɨɥɟɜɫɬɜɨ Delphi. ȼɢɪɬɭɚɥɶɧɵɣ ɤɥɭɛ ɩɪɨɝɪɚɦɦɢɫɬɨɜ. http://www.delphikingdom.ru/ 5. ɋɬɚɬɶɢ ɩɨ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɸ. – http://www.wasm.ru 6. Ɍɟɯɧɨɥɨɝɢɢ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ. – http://www.HiProg.com 7. ɗɥɟɤɬɪɨɧɧɵɟ ɤɧɢɝɢ ɢ ɭɱɟɛɧɢɤɢ ɩɨ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɸ. http://www.cnt.ru/~wh/x/books_n_manuals.html 8. ɗɥɟɤɬɪɨɧɧɵɣ ɤɚɬɚɥɨɝ ɇɚɭɱɧɨɣ ɛɢɛɥɢɨɬɟɤɢ ȼȽɍ. – http://www.lib.vsu.ru
84
ɍɱɟɛɧɨɟ ɢɡɞɚɧɢɟ
Ⱥɪɬɟɦɨɜ Ɇɢɯɚɢɥ Ⱥɧɚɬɨɥɶɟɜɢɱ, ȼɚɯɬɢɧ Ⱥɥɟɤɫɟɣ Ⱥɥɟɤɫɚɧɞɪɨɜɢɱ, ȼɨɳɢɧɫɤɚɹ Ƚɢɥɶɞɚ ɗɞɝɚɪɨɜɧɚ, Ɋɭɞɚɥɟɜ ȼɚɥɟɪɢɣ Ƚɟɧɧɚɞɶɟɜɢɱ
Ɉɫɧɨɜɵ ɋɈɆ-ɬɟɯɧɨɥɨɝɢɣ ɍɱɟɛɧɨ-ɦɟɬɨɞɢɱɟɫɤɨɟ ɩɨɫɨɛɢɟ ɞɥɹ ɜɭɡɨɜ
ɉɨɞɩɢɫɚɧɨ ɜ ɩɟɱɚɬɶ 20.08.07. Ɏɨɪɦɚɬ 60×84/16. ɍɫɥ. ɩɟɱ. ɥ. 5. Ɍɢɪɚɠ 50 ɷɤɡ. Ɂɚɤɚɡ 1765. ɂɡɞɚɬɟɥɶɫɤɨ-ɩɨɥɢɝɪɚɮɢɱɟɫɤɢɣ ɰɟɧɬɪ ȼɨɪɨɧɟɠɫɤɨɝɨ ɝɨɫɭɞɚɪɫɬɜɟɧɧɨɝɨ ɭɧɢɜɟɪɫɢɬɟɬɚ. 394000, ɝ. ȼɨɪɨɧɟɠ, ɩɥ. ɢɦ. Ʌɟɧɢɧɚ, 10. Ɍɟɥ. 208-298, 598-026 (ɮɚɤɫ) http://www.ppc.vsu.ru; e-mail: [email protected] Ɉɬɩɟɱɚɬɚɧɨ ɜ ɬɢɩɨɝɪɚɮɢɢ ɂɡɞɚɬɟɥɶɫɤɨ-ɩɨɥɢɝɪɚɮɢɱɟɫɤɨɝɨ ɰɟɧɬɪɚ ȼɨɪɨɧɟɠɫɤɨɝɨ ɝɨɫɭɞɚɪɫɬɜɟɧɧɨɝɨ ɭɧɢɜɟɪɫɢɬɟɬɚ. 394000, ɝ. ȼɨɪɨɧɟɠ, ɭɥ. ɉɭɲɤɢɧɫɤɚɹ, 3. Ɍɟɥ. 204-133. 85