PageRenderTime 42ms CodeModel.GetById 23ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 1ms

/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
 27#pragma region Includes
 28#include <stdio.h>
 29#include <windows.h>
 30
 31#include "Solution1.h"		// The example of using the #import directive 
 32							// and smart pointers to automate PowerPoint
 33#include "Solution2.h"		// The example of using the raw COM API to  
 34							// automate PowerPoint
 35#pragma endregion
 36
 37
 38int wmain(int argc, wchar_t* argv[])
 39{
 40	HANDLE hThread;
 41
 42	// Demonstrate automating PowerPoint using the #import directive and 
 43	// smart pointers in a separate thread.
 44	hThread = CreateThread(NULL, 0, AutomatePowerPointByImport, NULL, 0, NULL);
 45	WaitForSingleObject(hThread, INFINITE);
 46	CloseHandle(hThread);
 47
 48	_putws(L"");
 49
 50	// Demonstrate automating PowerPoint using C++ and the COM APIs in a 
 51	// separate thread.
 52	hThread = CreateThread(NULL, 0, AutomatePowerPointByCOMAPI, NULL, 0, NULL);
 53	WaitForSingleObject(hThread, INFINITE);
 54	CloseHandle(hThread);
 55
 56	return 0;
 57}
 58
 59
 60//
 61//   FUNCTION: GetModuleDirectory(LPWSTR, DWORD);
 62//
 63//   PURPOSE: This is a helper function in this sample. It retrieves the 
 64//      fully-qualified path for the directory that contains the executable 
 65//      file of the current process. For example, "D:\Samples\".
 66//
 67//   PARAMETERS:
 68//      * pszDir - A pointer to a buffer that receives the fully-qualified 
 69//      path for the directory taht contains the executable file of the 
 70//      current process. If the length of the path is less than the size that 
 71//      the nSize parameter specifies, the function succeeds and the path is 
 72//      returned as a null-terminated string.
 73//      * nSize - The size of the lpFilename buffer, in characters.
 74//
 75//   RETURN VALUE: If the function succeeds, the return value is the length 
 76//      of the string that is copied to the buffer, in characters, not 
 77//      including the terminating null character. If the buffer is too small 
 78//      to hold the directory name, the function returns 0 and sets the last 
 79//      error to ERROR_INSUFFICIENT_BUFFER. If the function fails, the return 
 80//      value is 0 (zero). To get extended error information, call 
 81//      GetLastError.
 82//
 83DWORD GetModuleDirectory(LPWSTR pszDir, DWORD nSize)
 84{
 85	// Retrieve the path of the executable file of the current process.
 86	nSize = GetModuleFileName(NULL, pszDir, nSize);
 87	if (!nSize || GetLastError() == ERROR_INSUFFICIENT_BUFFER)
 88	{
 89		*pszDir = L'\0'; // Ensure it's NULL terminated
 90		return 0;
 91	}
 92
 93	// Run through looking for the last slash in the file path.
 94    // When we find it, NULL it to truncate the following filename part.
 95
 96    for (int i = nSize - 1; i >= 0; i--)
 97	{
 98        if (pszDir[i] == L'\\' || pszDir[i] == L'/')
 99		{
100			pszDir[i + 1] = L'\0';
101			nSize = i + 1;
102            break;
103		}
104    }
105	return nSize;
106}