Microsoft KB Archive/216334

= DOC: defaultvalue Attribute Doesn't Work for MFC dispinterface Server and VB Client =

Article ID: 216334

Article Last Modified on 8/15/2005

-

APPLIES TO


 * Microsoft Visual Studio 6.0 Enterprise Edition
 * Microsoft Visual Studio 6.0 Service Pack 1
 * Microsoft Visual Studio 6.0 Service Pack 2
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q216334



SUMMARY
The defaultvalue attribute is only supported for named parameters or VARIANT parameters in MFC automation server.

The MFC documentation does not clearly state that the defaultvalue attribute is not supported for parameters of VARTYPE when using dispinterface (or the default implementation of MFC automation server).

Actually, the typelibrary generated by MFC always contains the optional attribute together with the defaultvalue attribute, while the optional attribute is suppose to work only with VARIANT, but defaultvalue can also be present without the optional attribute, and defaultvalue should work for any VARTYPE. This article clarifies this situation.



Steps to Reproduce the Problem
In Visual Studio:

 From the File menu, click New Project. Choose MFC AppWizard(dll) as the project type and give the project a name, such as Testdfv. Click OK. Then click Automation, and click Finish. Add a New Class using Class Wizard. Choose the base class as CCmdTarget and name the new class, Cdfvbug. Then click Automation.  Add a method to interface Idfvbug: short Test(short iVal)   Go to the .odl file and change the method definition to:

[id(1)] short Test([in, defaultvalue(99)] short iVal);   Adding Class Factory Support for class Cdfvbug by adding the following lines of code in the appropriate files:

DECLARE_OLECREATE(Cdfvbug) //Add this line to Dfvbug.h.

IMPLEMENT_OLECREATE(Cdfvbug, _T("testdfv.dfvbug"), 0x63b80a5c,0xb7c8, 0x11d2, 0xb7, 0xb, 0x0, 0xc0, 0x4f,  0x96, 0xc7, 0xb5) //Add this line to Dfvbug.cpp (using your own guid). </li> Implement Test Method of Cdfvbug class to simply return iVal.</li> Compile and build the project to get Testdfv.dll.</li> Using regsvr32 utility, register testdfv.dll.

The Automation server thus built, can be tested using a Visual Basic Client.</li>  Add the following code to the Visual Basic client application:

Private Sub Form_Load Dim obj As Object Set obj = CreateObject("Testdfv.dfvbug") Text2.Text = obj.Test End Sub </li></ol>

RESULTS: The Visual Basic client, when executing the above code returns, Run Time Error "449".

<div class="references_section">