Custom COM interfaces that must be used cross-process or
cross-apartment require marshaling support. This article explains how to set up
a Visual C++
project to build a self-registering standard marshaler from an IDL script that
describes your custom interface.
To generate a standard marshaler, write an IDL file that
describes your custom interface. The Microsoft Interface Definition Language
(MIDL) compiler is used to generate source files for the marshaler DLL from the
IDL file. Once these files are generated, you compile and link the code to
produce the marshaler DLL.
Versions of Visual C++ prior to 5.0 do not
provide built-in rules for compiling IDL files using the MIDL compiler.
However, you can use the custom build feature to add the necessary rules to
your project. Beginning with version 6.0, the build engine automatically calls
MIDL.exe to compile IDL files. The Project/Settings dialog box has a MIDL tab
to control the settings.
To generate a standard marshaler for the
- Create a new empty Dynamic-Link Library project using
Developer Studio. This can either be a new project workspace or a subproject of
an existing workspace.
Note The project name is also used as the name of the marshaler DLL.
For example, PS<interface>.
- Create a new file called IMyInterface.IDL, and enter the
description of your new interface.
- Insert the file IMyInterface.IDL into the
- Go to the project Build Settings dialog box and select
IMyInterface.IDL in all of the project's configurations.
Note Make sure that you have selected the file in all of your
- For Versions of Visual C++ prior to 6.0, click the Custom
Build tab and type the following entries, and then close the Build Settings
For version 6.0 and later, set the appropriate MIDL settings for
IMyinterface.IDL in the MIDL section of either the Project/Settings dialog box
or the Properties dialog box.
Compiling Interface Definition
midl.exe /ms_ext /char unsigned /c_ext /out "$(InputDir)"
- Add the file RPCHelp.c to your project by typing the
Note The file Rpcdce4.dll is no longer included with Microsoft
operating systems. Its functions have been incorporated into the other DLLs. If
you receive a link error on Rpcdce4.lib, remove its comment pragma from the
#pragma comment(lib, "rpcndr.lib")
#pragma comment(lib, "rpcdce4.lib")
#pragma comment(lib, "rpcns4.lib")
#pragma comment(lib, "rpcrt4.lib")
- Add the following three files to your project:
dlldata.c Since the files don't exist yet, you need to type the names in
the File Name field on the Insert File dialog box. When you get the warning
message that the file doesn't exist, click Yes.
In Visual C++ .NET,
the easiest way to do this is to click Add New Item on the Project menu for
each file. In the Add New Item dialog box, click to select the Text File (.txt)
template, and then type one of the names that is listed (make sure that you
include the '.c' extension).
- Use the following PSSample.def file as a model to create a
DEF file for your project:
Note Make sure you change the library name to match the name of your
DESCRIPTION 'Sample custom interface marshaler'
- Add your new DEF file to the project.
- Open the project Build Settings dialog box (or Properties
dialog box) and select your project (make sure that you select all
configurations). Click the C++ tab and add the value REGISTER_PROXY_DLL to the
Preprocessor Definitions field.
In Visual C++ .NET, you must also add
a Preprocessor definition that identifies the target operating system. The
following definition will compile for Windows NT 4.0, Windows 2000, and later:
_WIN32_WINNT=0x400 For more information, see the "Targeting Stubs for Specific
32-bit or 64-bit Platforms" topic in the Platform SDK Books Online.
- In the Custom Build Step section of the project properties
(or settings), type the following values, and then close the Build Settings
Registering Custom Interface Marshaler
regsvr32 /s /c "$(TargetPath)"
echo regsvr32 exe. time > "$(OutDir)\regsvr32.trg"
Your project is now configured to properly build and register
the custom interface marshaler.
Visual C++ Help file; Topic: "Specifying Custom Build
Visual C++ InfoViewer; Topic: "SDKs\Win32
Article ID: 164149 - Last Review: February 24, 2014 - Revision: 6.0
- Microsoft Visual C++ 4.0 Standard Edition
- Microsoft Visual C++ 5.0 Enterprise Edition
- Microsoft Visual C++ 6.0 Enterprise Edition
- Microsoft Visual C++ 5.0 Professional Edition
- Microsoft Visual C++ 6.0 Professional Edition
- Microsoft Visual C++, 32-bit Learning Edition 6.0
- Microsoft Visual C++ .NET 2002 Standard Edition
- Microsoft Visual C++ .NET 2003 Standard Edition
- Microsoft Visual C++ 2005 Express Edition
|kbnosurvey kbarchive kbhowto kbide KB164149|