PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/CppAutomatePowerPoint/CppAutomatePowerPoint.cpp

#
C++ | 106 lines | 37 code | 12 blank | 57 comment | 8 complexity | 6c6d0ab89ebbaacb41ca16e9b55b8391 MD5 | raw file
  1. /****************************** Module Header ******************************\
  2. * Module Name: CppAutomatePowerPoint.cpp
  3. * Project: CppAutomatePowerPoint
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * The CppAutomatePowerPoint example demonstrates how to write VC++ code to
  7. * create a Microsoft PowerPoint instance, create a new presentation, insert a
  8. * new slide, add some texts to the slide, save the presentation, quit
  9. * PowerPoint and then clean up unmanaged COM resources.
  10. *
  11. * There are three basic ways you can write VC++ automation codes:
  12. *
  13. * 1. Automating PowerPoint using the #import directive and smart pointers
  14. * (Solution1.h/cpp)
  15. * 2. Automating PowerPoint using C++ and the COM APIs (Solution2.h/cpp)
  16. * 3. Automating PowerPoint using MFC (This is not covered in this sample)
  17. *
  18. * This source is subject to the Microsoft Public License.
  19. * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  20. * All other rights reserved.
  21. *
  22. * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  23. * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  24. * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  25. \***************************************************************************/
  26. #pragma region Includes
  27. #include <stdio.h>
  28. #include <windows.h>
  29. #include "Solution1.h" // The example of using the #import directive
  30. // and smart pointers to automate PowerPoint
  31. #include "Solution2.h" // The example of using the raw COM API to
  32. // automate PowerPoint
  33. #pragma endregion
  34. int wmain(int argc, wchar_t* argv[])
  35. {
  36. HANDLE hThread;
  37. // Demonstrate automating PowerPoint using the #import directive and
  38. // smart pointers in a separate thread.
  39. hThread = CreateThread(NULL, 0, AutomatePowerPointByImport, NULL, 0, NULL);
  40. WaitForSingleObject(hThread, INFINITE);
  41. CloseHandle(hThread);
  42. _putws(L"");
  43. // Demonstrate automating PowerPoint using C++ and the COM APIs in a
  44. // separate thread.
  45. hThread = CreateThread(NULL, 0, AutomatePowerPointByCOMAPI, NULL, 0, NULL);
  46. WaitForSingleObject(hThread, INFINITE);
  47. CloseHandle(hThread);
  48. return 0;
  49. }
  50. //
  51. // FUNCTION: GetModuleDirectory(LPWSTR, DWORD);
  52. //
  53. // PURPOSE: This is a helper function in this sample. It retrieves the
  54. // fully-qualified path for the directory that contains the executable
  55. // file of the current process. For example, "D:\Samples\".
  56. //
  57. // PARAMETERS:
  58. // * pszDir - A pointer to a buffer that receives the fully-qualified
  59. // path for the directory taht contains the executable file of the
  60. // current process. If the length of the path is less than the size that
  61. // the nSize parameter specifies, the function succeeds and the path is
  62. // returned as a null-terminated string.
  63. // * nSize - The size of the lpFilename buffer, in characters.
  64. //
  65. // RETURN VALUE: If the function succeeds, the return value is the length
  66. // of the string that is copied to the buffer, in characters, not
  67. // including the terminating null character. If the buffer is too small
  68. // to hold the directory name, the function returns 0 and sets the last
  69. // error to ERROR_INSUFFICIENT_BUFFER. If the function fails, the return
  70. // value is 0 (zero). To get extended error information, call
  71. // GetLastError.
  72. //
  73. DWORD GetModuleDirectory(LPWSTR pszDir, DWORD nSize)
  74. {
  75. // Retrieve the path of the executable file of the current process.
  76. nSize = GetModuleFileName(NULL, pszDir, nSize);
  77. if (!nSize || GetLastError() == ERROR_INSUFFICIENT_BUFFER)
  78. {
  79. *pszDir = L'\0'; // Ensure it's NULL terminated
  80. return 0;
  81. }
  82. // Run through looking for the last slash in the file path.
  83. // When we find it, NULL it to truncate the following filename part.
  84. for (int i = nSize - 1; i >= 0; i--)
  85. {
  86. if (pszDir[i] == L'\\' || pszDir[i] == L'/')
  87. {
  88. pszDir[i + 1] = L'\0';
  89. nSize = i + 1;
  90. break;
  91. }
  92. }
  93. return nSize;
  94. }