TempFileCOM.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "TempFileCOM.h"
  2. #include <shlwapi.h>
  3. enum
  4. {
  5. DISP_TEMPFILE_WRITE_STRING,
  6. DISP_TEMPFILE_CLOSE,
  7. DISP_TEMPFILE_GET_FILENAME,
  8. DISP_TEMPFILE_DELETE,
  9. };
  10. TempFileCOM::TempFileCOM(const wchar_t *ext)
  11. {
  12. ref=1;
  13. wchar_t tempPath[MAX_PATH-14] = {0};
  14. GetTempPathW(MAX_PATH-14, tempPath);
  15. GetTempFileNameW(tempPath, L"tfc", 0, filename);
  16. if (ext)
  17. {
  18. PathRemoveExtensionW(filename);
  19. PathAddExtensionW(filename, ext);
  20. }
  21. hFile = CreateFileW(filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, 0);
  22. wchar_t BOM = 0xFEFF;
  23. DWORD written = 0;
  24. WriteFile(hFile, &BOM, 2, &written, NULL);
  25. }
  26. #define CHECK_ID(str, id)\
  27. if (CSTR_EQUAL == CompareStringW(lcid, NORM_IGNORECASE, rgszNames[i], -1, L##str, -1))\
  28. { rgdispid[i] = id; continue; }
  29. HRESULT TempFileCOM::GetIDsOfNames(REFIID riid, OLECHAR FAR* FAR* rgszNames, unsigned int cNames, LCID lcid, DISPID FAR* rgdispid)
  30. {
  31. bool unknowns = false;
  32. for (unsigned int i = 0;i != cNames;i++)
  33. {
  34. CHECK_ID("WriteString", DISP_TEMPFILE_WRITE_STRING);
  35. CHECK_ID("Close", DISP_TEMPFILE_CLOSE);
  36. CHECK_ID("GetFilename", DISP_TEMPFILE_GET_FILENAME);
  37. CHECK_ID("Delete", DISP_TEMPFILE_DELETE);
  38. rgdispid[i] = DISPID_UNKNOWN;
  39. unknowns = true;
  40. }
  41. if (unknowns)
  42. return DISP_E_UNKNOWNNAME;
  43. else
  44. return S_OK;
  45. }
  46. HRESULT TempFileCOM::GetTypeInfo(unsigned int itinfo, LCID lcid, ITypeInfo FAR* FAR* pptinfo)
  47. {
  48. return E_NOTIMPL;
  49. }
  50. HRESULT TempFileCOM::GetTypeInfoCount(unsigned int FAR * pctinfo)
  51. {
  52. return E_NOTIMPL;
  53. }
  54. HRESULT TempFileCOM::Invoke(DISPID dispid, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, EXCEPINFO FAR * pexecinfo, unsigned int FAR *puArgErr)
  55. {
  56. switch (dispid)
  57. {
  58. case DISP_TEMPFILE_WRITE_STRING:
  59. {
  60. const wchar_t *str = pdispparams->rgvarg[0].bstrVal;
  61. DWORD written=0;
  62. WriteFile(hFile, str, (DWORD)wcslen(str)*sizeof(wchar_t), &written, NULL);
  63. return S_OK;
  64. }
  65. case DISP_TEMPFILE_CLOSE:
  66. CloseHandle(hFile);
  67. hFile=INVALID_HANDLE_VALUE;
  68. return S_OK;
  69. case DISP_TEMPFILE_GET_FILENAME:
  70. {
  71. BSTR tag = SysAllocString(filename);
  72. VariantInit(pvarResult);
  73. V_VT(pvarResult) = VT_BSTR;
  74. V_BSTR(pvarResult) = tag;
  75. return S_OK;
  76. }
  77. case DISP_TEMPFILE_DELETE:
  78. DeleteFileW(filename);
  79. return S_OK;
  80. }
  81. return DISP_E_MEMBERNOTFOUND;
  82. }
  83. STDMETHODIMP TempFileCOM::QueryInterface(REFIID riid, PVOID *ppvObject)
  84. {
  85. if (!ppvObject)
  86. return E_POINTER;
  87. else if (IsEqualIID(riid, IID_IDispatch))
  88. *ppvObject = (IDispatch *)this;
  89. else if (IsEqualIID(riid, IID_IUnknown))
  90. *ppvObject = this;
  91. else
  92. {
  93. *ppvObject = NULL;
  94. return E_NOINTERFACE;
  95. }
  96. AddRef();
  97. return S_OK;
  98. }
  99. ULONG TempFileCOM::AddRef(void)
  100. {
  101. return (ULONG)++ref;
  102. }
  103. ULONG TempFileCOM::Release(void)
  104. {
  105. if (--ref == 0)
  106. delete this;
  107. return (ULONG)ref;
  108. }