DictCtrl.cpp
上传用户:biuytresa
上传日期:2007-12-07
资源大小:721k
文件大小:3k
源码类别:

DNA

开发平台:

Visual C++

  1. // DictCtrl.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "windows.h"
  5. #include <stdio.h>
  6. #include "IDictionary.h"
  7. #include "ISpellCheck.h"
  8. // {54BF6567-1007-11D1-B0AA-444553540000}
  9. extern "C" const GUID CLSID_Dictionary = 
  10. { 0x54bf6567, 0x1007, 0x11d1,
  11. { 0xb0, 0xaa, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00} } ;
  12. extern "C" const GUID IID_Dictionary = 
  13. { 0x54bf6568, 0x1007, 0x11d1,
  14. { 0xb0, 0xaa, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00} } ;
  15. extern "C" const GUID IID_SpellCheck = 
  16. { 0x54bf6569, 0x1007, 0x11d1,
  17. { 0xb0, 0xaa, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00} } ;
  18. int main(int argc, char* argv[])
  19. {
  20. IUnknown *pUnknown;
  21. IDictionary *pDictionary;
  22. ISpellCheck *pSpellCheck;
  23. WCHAR  stringResult[32];
  24. HRESULT hResult;
  25. if (CoInitialize(NULL) != S_OK) {
  26. printf("Initialize COM library failed!n");
  27. CoUninitialize();
  28. return -1;
  29. }
  30. GUID dictionaryCLSID;
  31. hResult = ::CLSIDFromProgID(L"Dictionary.Object", &dictionaryCLSID);
  32. if (FAILED(hResult)) 
  33. {
  34. printf("Can't find the dictionary CLSID!n");
  35. CoUninitialize();
  36. return -2;
  37. }
  38. hResult = CoCreateInstance(dictionaryCLSID, NULL, 
  39. CLSCTX_LOCAL_SERVER, IID_IUnknown, (void **)&pUnknown);
  40. if (FAILED(hResult)) 
  41. {
  42. printf("Create object failed!n");
  43. CoUninitialize();
  44. return -2;
  45. }
  46. hResult = pUnknown->QueryInterface(IID_Dictionary, (void **)&pDictionary);
  47. if (FAILED(hResult)) {
  48. pUnknown->Release();
  49. printf("QueryInterface IDictionary failed!n");
  50. CoUninitialize();
  51. return -3;
  52. }
  53. char strDirectory[256];
  54. ::GetCurrentDirectory(200, strDirectory);
  55. strcat(strDirectory, "\animal.dict");
  56. WCHAR pLibFileName[256];
  57. mbstowcs(pLibFileName, strDirectory, 256);
  58. hResult = pDictionary->LoadLibrary(pLibFileName);
  59. if (SUCCEEDED(hResult)) {
  60. hResult = pDictionary->LookupWord(L"tiger", stringResult);
  61. if (SUCCEEDED(hResult)) {
  62. char *pTiger = new char[32];
  63. wcstombs(pTiger, stringResult, 32) ;
  64. printf("find the word "tiger" -- %sn", pTiger);
  65. delete pTiger;
  66. }
  67. pDictionary->InsertWord(L"elephant", L"象");
  68. hResult = pDictionary->LookupWord(L"elephant", stringResult);
  69. if (SUCCEEDED(hResult)) {
  70. pDictionary->RestoreLibrary(L"animal1.dict");
  71. }
  72. } else {
  73. printf("Load Library "%s" Failed!n", strDirectory);
  74. pDictionary->Release();
  75. pUnknown->Release();
  76. CoUninitialize();
  77. return -4;
  78. }
  79. hResult = pDictionary->QueryInterface(IID_SpellCheck, (void **)&pSpellCheck);
  80. pDictionary->Release();
  81. if (FAILED(hResult)) {
  82. pUnknown->Release();
  83. printf("QueryInterface IDictionary failed!n");
  84. CoUninitialize();
  85. return -5;
  86. }
  87. hResult = pSpellCheck->CheckWord(L"lion", stringResult);
  88. if (SUCCEEDED(hResult)) {
  89. printf("Word "lion" spelling right.n");
  90. } else {
  91. char *pLion = new char[32];
  92. wcstombs(pLion, stringResult, 32) ;
  93. printf("Word "lion" spelling is wrong. Maybe it is %s.n", pLion);
  94. delete pLion;
  95. }
  96. hResult = pSpellCheck->CheckWord(L"dot", stringResult);
  97. if (SUCCEEDED(hResult)) {
  98. printf("Word "dot" spelling right.n");
  99. } else {
  100. char *pDot = new char[32];
  101. wcstombs(pDot, stringResult, 32) ;
  102. printf("Word "dot" spelling is wrong. Maybe it is %s.n", pDot);
  103. delete pDot;
  104. }
  105. pSpellCheck->Release();
  106. if (pUnknown->Release()== 0) 
  107. printf("The reference count of dictionary object is zero.");
  108. CoUninitialize();
  109. return 0;
  110. }