When dispinterface has a retval attribute in the Interface Definition Language (IDL) file of the
Microsoft Component Object Model (COM) server, and when you use the Add
Reference tool to import the COM server that you want to use from Microsoft
Visual C++ .NET 2003 managed code, you may receive the following
exception:
An unhandled exception of type
'System.Reflection.TargetParameterCountException' occurred in mscorlib.dll
Additional information:
When you use the Add Reference tool in Visual C++ .NET
2003, the Type Library Importer (Tlbimp.exe) internally generates an interop
assembly. Tlbimp.exe does not run with the /transform:dispret option.
To work around this problem, create an interop assembly
manually, and then import the newly created assembly in your code. To create an
interop assembly, run Tlbimp.exe with the /transform:dispret command-line option.
To do this, follow these steps:
Remove the reference to the MyServer.DLL COM
component.
Open a Microsoft Visual Studio .NET command
prompt.
Open a Microsoft Visual Studio .NET command
prompt.
Change the folder to the MyServer folder.
To create a COM DLL, type the following command at the
command prompt:
cl /LD MyServer.cpp
A COM DLL that is named MyServer.DLL is
created.
To register the COM DLL, type the following command at the
command prompt:
regsvr32 MyServer.dll
Access COM DLL from Managed Code
Start Visual Studio .NET 2003.
On File menu, point to
New, and then click Project.
Under Project Types, click Visual
C++ Projects. Under Templates, click Console
Application (.NET). In the Name box, type
ManagedClient, and then click
OK.
In Solution Explorer, right-click
Reference, and then click Add
Reference.
On the COM tab, locate MyServer
1.0 Type Library, click Select, and then click
OK.
Copy the following code to
ManagedClient.cpp:
// This is the main project file for the Visual C++ .NET application project.
// The file was generated by using an Application Wizard.
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
using namespace Interop;
int _tmain()
{
MyServer::IMyServerObj *mysrv = new MyServer::CMyServerObjClass();
int i = 12;
// Call the method from the COM DLL.
mysrv->GetOffsetNum(100,&i);
Console::WriteLine(S"The new value of i is {0}",i.ToString());
}
On Debug menu, click
Start. You receive the TargetParameterCountException exception
that is described in the "Symptoms" section of this article.