📄 embedole.cpp
字号:
/****************************************************************************
Program: EMBEDOLE
File: EMBEDOLE.CPP
Syntax: embedole <dbname> [<server>]
Description:
This program illustrates embedding an OLE object in a rich text item.
A new document is created with a "Title" and "Body" fields. The
body contains rich text and an embedded OLE object.
This is a new version of EMBEDOLE which has been restructured and
enhanced to store an appropriate bitmap representation of the OLE
object.
****************************************************************************/
#include <ole2.h>
#include <lncppapi.h>
#include <iostream>
using namespace std;
#define ERR_BUF_SIZE 512 // Size of error buffer
char CommandBuf[80];
//
// CFailure - Application failure class
//
class CFailure
{
public:
CFailure (void) { ; }
~CFailure (void) { ; }
};
//
// Function declarations
//
// Embed the OLE object into Notes
void EmbedInNotes (
LNNotesSession & Session,
char * pDocPath,
char * pDbFilename,
char * pDbServer,
char * pOleUserType,
char * pFilePath,
LNOLEGUID * pApiGuid,
char * pOleProgID,
LNRichText * pGraphicContainer
);
// Get the file name (if any) for an OLE object, and
// convert the name from UNICODE to multi-byte characters.
void GetMultiByteTempFileName (
LPMALLOC lpMalloc,
LPSTORAGE lpStorage,
char * pBuffer,
int bufferSize
);
// Copy the contents of an OLE object into
// the destination IStorage.
void CopyObjectToIStorage (
LPOLEOBJECT lpOleObject,
LPSTORAGE lpDestStorage
);
// Try to get the graphic representation
// of the OLE object.
void GetGraphic (
LPOLEOBJECT lpOleObject,
LNRichText * pGraphicContainer
);
// Convert a Windows bitmap to a Notes LNGraphic
void ConvertBitmapToLNGraphic (
STGMEDIUM * pStgMedium,
LNRichText * pGraphicContainer
);
// Microsoft routines to store bitmap in a file
extern "C" PBITMAPINFO CreateBitmapInfoStruct (
HBITMAP hBmp
);
extern "C" BOOL CreateBMPFile (
LPTSTR pszFile,
PBITMAPINFO pbi,
HBITMAP hBMP,
HDC hDC
);
//
// Main program
//
int main(int argc, char *argv[])
{
LNNotesSession Session;
// Cleanup controls
BOOL IsNotesInitialized = FALSE;
BOOL IsOleInitialized = FALSE;
// Argument pointers
char * DocPath = NULL;
char * pFilePart;
char * DbFilename;
char * DbServer = NULL;
CLSID oleClsid;
LNOLEGUID * pApiGuid = (LNOLEGUID *) (&oleClsid);
// Deal with the structure conversion issue
// Some name strings must be converted from UNICODE to multi-byte (native)
// characters, or vice-versa.
char FullPathName [MAXPATH] = "";
WCHAR widePathName [MAXPATH_OLE];
LPOLESTR lpProgID = NULL; // Program ID string (UNICODE); must be freed using lpMalloc
char oleProgID [MAXPATH_OLE]; // Buffer for multi-byte copy of name
LPOLESTR lpUserType = NULL; // OLE user type string (UNICODE); must be freed using lpMalloc
char oleUserType [MAXPATH_OLE]; // Buffer for multi-byte copy of string
// OLE interface pointers
LPMALLOC lpMalloc = NULL; // Pointer to IMalloc interface
LPSTORAGE lpRootStorage = NULL; // Pointer to root IStorage
LPSTORAGE lpSubStorage = NULL; // Pointer to content IStorage
LPOLEOBJECT lpOleObject = NULL; // IOleObject interface
char tempFilePath [MAXPATH_OLE]; // Name of OLE's temporary file
// Check the command line.
if (argc < 3 || argc > 4)
{
cout << "\nUsage:\n\t" << argv[0]
<< " <docfile> <filename> [<server>] \n"
<< endl;
return(1);
}
// Get info from command line.
DocPath = argv[1];
DbFilename = argv[2];
if (argc == 4)
DbServer = argv[3];
#ifdef W32
// On Win32 systems, the OS can expand the pathname for us.
GetFullPathName (DocPath, MAXPATH, FullPathName, &pFilePart);
#else
// Everybody else must pass in the fully-qualified pathname
strcpy (FullPathName, DocPath);
#endif
try
{
// Initialize the Notes C++ API
if (LNNOERROR != Session.Init(argc, argv))
throw ("Unable to initialize the C++ API");
IsNotesInitialized = TRUE;
// Initialize the OLE interface
if (FAILED(OleInitialize(NULL)))
throw ("Cannot initialize OLE");
IsOleInitialized = TRUE;
// Get the current OLE memory allocation interface
CoGetMalloc(MEMCTX_TASK, &lpMalloc);
// Convert the file's pathname to UNICODE
MultiByteToWideChar (
CP_ACP,
0,
FullPathName,
-1,
widePathName,
MAXPATH_OLE);
// Find the OLE class ID for the file
if(FAILED(GetClassFile(widePathName, &oleClsid)))
{
cout << "No CLSID found for " << DocPath << endl;
throw (CFailure ());
}
// Next, get the ProgID for the OLE server
if(FAILED(ProgIDFromCLSID(oleClsid, &lpProgID)))
{
cout << "Cannot convert CLSID to ProgID for " << DocPath << endl;
throw (CFailure ());
}
// Convert ProgID from UNICODE to platform characters
WideCharToMultiByte (
CP_ACP,
0,
lpProgID,
-1,
oleProgID,
MAXPATH_OLE,
NULL,
NULL);
// Get the OLE user type for the OLE server
if(FAILED(OleRegGetUserType(oleClsid, USERCLASSTYPE_FULL, &lpUserType)))
{
cout << "Cannot obtain OLE user type for " << DocPath << endl;
throw (CFailure ());
}
// Convert UserType from UNICODE to platform characters
WideCharToMultiByte (
CP_ACP,
0,
lpUserType,
-1,
oleUserType,
MAXPATH_OLE,
NULL,
NULL);
// Create a temporary OLE storage file
if (FAILED(StgCreateDocfile (
NULL,
STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_CREATE | STGM_DELETEONRELEASE,
0,
&lpRootStorage)))
{
throw ("Cannot create temporary storage file");
}
// Create the sub-storage for the object data
if (FAILED(lpRootStorage->CreateStorage(
lpProgID, // Needs the UNICODE version!
STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
0,
0,
&lpSubStorage)))
{
throw ("Cannot create object data sub-storage");
}
// Create an OLE object based on the input file
if (FAILED(OleCreateFromFile (
CLSID_NULL, // We do what the OLE documentation says . . .
widePathName, // Pathname
IID_IOleObject, // We'll need the IOleObject interface
OLERENDER_DRAW,
NULL,
NULL,
lpSubStorage, // Create the content in the substorage
(void FAR * FAR *) &lpOleObject)))
{
throw ("Failed to create OLE object from file");
}
// Store the contents of the OLE object into the substorage
CopyObjectToIStorage (lpOleObject, lpSubStorage);
// Get the name of the OLE temporary file
GetMultiByteTempFileName (lpMalloc, lpRootStorage, tempFilePath, MAXPATH_OLE);
// Get the graphical representation of the OLE object
LNRichText graphicContainer;
GetGraphic (lpOleObject, &graphicContainer);
// Everything is ready - store into Notes!
EmbedInNotes (
Session,
DocPath,
DbFilename,
DbServer,
oleUserType,
tempFilePath,
pApiGuid,
oleProgID,
&graphicContainer);
}
catch (char * pMessage)
{
cout << "*** Error: " << pMessage << endl;
}
catch (CFailure)
{
;
}
/////////////////////////////////
// Clean up and free resources //
/////////////////////////////////
// Release the IOleObject interface
if (((LPOLEOBJECT) NULL) != lpOleObject)
{
lpOleObject->Release ();
lpOleObject = NULL;
}
// Release the sub-storage
if (((LPSTORAGE) NULL) != lpSubStorage)
{
lpSubStorage->Release ();
lpSubStorage = NULL;
}
// Release the root storage
if (((LPSTORAGE) NULL) != lpRootStorage)
{
lpRootStorage->Release ();
lpRootStorage = NULL;
}
// Free the memory for the user type string
if (((LPOLESTR) NULL) != lpUserType)
lpMalloc->Free (lpUserType);
// Free the memory for the ProgID
if (((LPOLESTR) NULL) != lpProgID)
lpMalloc->Free (lpProgID);
// Release the IMalloc interface pointer
if (((LPMALLOC) NULL) != lpMalloc)
{
lpMalloc->Release ();
lpMalloc = NULL;
}
// Close the OLE session
if (IsOleInitialized)
OleUninitialize ();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -