INPROC Sample: Demonstrates an In-Process Automation Server Application
The INPROC sample is an in-process Automation (formerly OLE Automation) server. Unlike the other MFC automation server samples, INPROC can be loaded as a dynamic-link library (DLL) in the client's address space. In-process servers are usually more efficient than servers implemented as separate EXEs because a remote procedure call (RPC) is not necessary to invoke methods on the objects implemented by the server.
Note
Some samples, such as this one, have not been modified to reflect the changes in the Visual C++ wizards, libraries, and compiler, but still demonstrate how to complete your desired task.
Security Note: |
---|
This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended. |
To get samples and instructions for installing them:
On the Visual Studio Help menu, click Samples.
For more information, see Visual Studio Samples.
The most recent version and complete list of samples is available online from the Visual Studio 2008 Samples page.
You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied into a folder under \Program Files\Visual Studio 9.0\Samples\. For Express editions of Visual Studio, all samples are located online.
Building and Running the Sample
To build and run the INPROC sample
Open the solution inproc.sln.
On the Build menu, click Build.
Register INPROC's DLL using the project's .reg file, that is, either run regedit INPROC.REG, or use REGSVR from the REGSVR sample.
If you use REGEDIT, be sure that Inproc.dll is on the system path. Alternately, you can modify the .reg file to refer to the path of Inproc.dll explicitly.
A DLL cannot be run stand alone, as an EXE can; therefore, to test INPROC, you must "drive" it from a client application. INPROC can be driven from Visual Basic or from Visual C++. See the IPDRIVE sample for an example of driving INPROC from Visual C++.
Note
You can build an EXE version or a DLL version of INPROC. Set the EXE variant in the sample's project settings. An EXE version of INPROC only registers itself in the Windows registry.
INPROC Classes
The CVariantMapclass implements a VARIANT to VARIANT map. This allows any VARIANT to be mapped to any other VARIANT. Although this is probably not useful to Visual C++ programmers (who would probably use CMap directly), it does bring the power of MFC's collection classes to Visual Basic users. The CVariantMap class is accessed by the name mfc.inproc.varmap. This is how the object is registered in the Windows registry. CVariantMap implements the standard collection methods and properties as well as the _NewEnum method.
The CStringCollect class implements an array of strings. Its implementation is simpler than the CVariantMap class, even though it implements many of the same automation features as CVariantMap. The CStringCollect object is accessed by the name mfc.inproc.strcoll. It implements most of the standard collection methods and properties and is a good example for how to implement your own collections. Of particular interest is its implementation of the _NewEnum method, which allows Visual Basic users to use the For Each... In syntax when enumerating the contents of a collection. CStringCollect uses CEnumVariant to implement this functionality (it implements IEnumVARIANT using the MFC interface maps). You may find CEnumVariant useful in your own applications.
Finally, the sample implements a few dummy properties, which are used simply to compare the performance of in-process servers and LocalServer servers. These are the properties tested by IPDRIVE's Test1 and Test2 buttons.
Keywords
This sample demonstrates the following keywords:
AfxIsValidAddress; AfxMessageBox; AfxOleInit; AfxOleLockApp; AfxOleUnlockApp; AfxThrowMemoryException; AfxThrowOleException; CCmdTarget::EnableAutomation; CCmdTarget::GetIDispatch; CCmdTarget::OnFinalRelease; CString::AllocSysString; CWinApp::InitInstance; CWinApp::RunAutomated; CWinApp::RunEmbedded; DllCanUnloadNow; DllGetClassObject; min