When activating a linked or embedded object for editing—for example, a user double-clicking on an object—the client DLL starts the server application using the /Embedding command-line option.
The server application uses this option to determine whether the object has been opened directly by a user or as part of an editing session for linked and embedded objects. Server applications should check for both /Embedding and -Embedding whenever they check for the command-line options.
The /Embedding command line option determines how the server application should initialize itself when starting:
If neither /Embedding nor /Embedding filename is present, the server application should register itself by calling the OleRegisterServer function. The absence of the /Embedding flag indicates that the server was not started by a client application. If the server application normally opens an untitled document when it starts, it should call the OleRegisterServerDoc function, specifying an untitled document to register.
If the /Embedding option is present, the server application should not register the document or display a window. The absence of filename after the /Embedding flag indicates that the object is embedded, but not linked. The application should register itself with the OleRegisterServer function and then enter a message-dispatch loop, waiting for callbacks from the server DLL. When the server DLL requires the server application to be visible, it will call the Show or DoVerb callback function in the OLEOBJECTVTBL structure.
If the /Embedding filename option is present, it indicates that the object to be processed is a linked object and filename references the linked file. In this case, the server application should register itself with OleRegisterServer, process the filename string and call the OleRegisterServerDoc function to register the document. The server application should not create a new document or display a window. After registering the document, the application should enter a message-dispatch loop, waiting for callbacks from the server DLL. When the DLL requires the server application to be visible, it will call the Show or DoVerb callback function in the OLEOBJECTVTBL structure.
In the SRVDEMO.EXE program, the ProcessCmdLine function parses the command line to determine how the application was started, setting a flag to indicate whether it was opened directly by a user (the fEmbedding flag will be FALSE), or whether it is part of an editing session for linked and embedded objects. Linked objects have a filename following the /Embedded command-line option, embedded objects do not.
extern BOOL fEmbedding;
.
.
.
/* WinMain
*
* Standard windows entry point
*/
int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
if (!hPrevInstance)
{
fFirstInstance = TRUE;
if (!InitApplication(hInstance))
return FALSE;
}
else
fFirstInstance = FALSE;
msg.wParam = FALSE;
if (!InitInstance(hInstance, fFirstInstance))
goto errRtn;
if (!InitServer (hwndMain, hInstance))
goto errRtn;
if (!ProcessCmdLine (lpCmdLine, nCmdShow, hwndMain))
{
ExitApplication(FALSE);
goto errRtn;
}
static BOOL ProcessCmdLine (LPSTR lpszLine, int nCmdShow,
HWND hwndMain)
{
char szBuf[cchFilenameMax];
BOOL fEmbedding = FALSE; // Is "-Embedding" on the
// command line?
int i=0;
OFSTRUCT of;
if (!*lpszLine) // No filename or options, so start a
// fresh document.
{
return CreateUntitledDoc(nCmdShow);
}
SkipBlanks (&lpszLine);
// Check for "-Embedding" or "/Embedding" and set
// fEmbedding.
if(*lpszLine == '-' || *lpszLine == '/')
{
lpszLine++;
GetWord (&lpszLine, szBuf);
fEmbedding = !lstrcmp(szBuf, szEmbeddingFlag);
}
SkipBlanks (&lpszLine);
if (*lpszLine) // if there is a filename
{
// Put filename into szBuf.
GetWord (&lpszLine, szBuf);
if (-1 == OpenFile(szBuf, &of, OF_READ | OF_EXIST))
{
// File not found
if (fEmbedding)
return FALSE;
else
{
char sz[100];
wsprintf (sz, "File %s not found.", (LPSTR) szBuf);
ErrorBox (sz);
return CreateUntitledDoc(nCmdShow);
}
}
//Create the document and register it with OLE library
if (!CreateDocFromFile (szBuf, NULL, doctypeFromFile))
{
// File is not in proper format.
if (fEmbedding)
return FALSE;
else
{
char sz[100];
wsprintf (sz, "File %s not in proper
format.", (LPSTR) szBuf);
ErrorBox (sz);
return CreateUntitledDoc(nCmdShow);
}
}
}
if (fEmbedding)
{
//Do not show window until told to do so by client.
ShowWindow(hwndMain, SW_HIDE);
}
else
{
ShowWindow(hwndMain, nCmdShow);
UpdateWindow(hwndMain);
}
return TRUE;
}