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

ID: Q216334


The information in this article applies to:


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.


MORE INFORMATION

Steps to Reproduce the Problem

In Visual Studio:
  1. From the File menu, click New Project.


  2. Choose MFC AppWizard(dll) as the project type and give the project a name, such as Testdfv.


  3. Click OK. Then click Automation, and click Finish.


  4. Add a New Class using Class Wizard. Choose the base class as CCmdTarget and name the new class, Cdfvbug. Then click Automation.


  5. Add a method to interface Idfvbug:
    
    short Test(short iVal) 


  6. Go to the .odl file and change the method definition to:
    
    [id(1)] short Test([in, defaultvalue(99)] short iVal); 


  7. 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). 


  8. Implement Test Method of Cdfvbug class to simply return iVal.


  9. Compile and build the project to get Testdfv.dll.


  10. Using regsvr32 utility, register testdfv.dll.

    The Automation server thus built, can be tested using a Visual Basic Client.


  11. 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  


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


REFERENCES

For additional information about a method to replace the default implementation of any MFC automation server, please see the following article in the Microsoft Knowledge Base:

Q140616 SAMPLE: MFCDISP: Replacing MFC IDispatch Implementation

Additional query words: defaultvalue dispinterface


Keywords          : kbdocfix kbMFC kbVS600 kbVS600sp2fix kbVS600SP1fix 
Version           : WINDOWS:6.0,6.0 SP1,6.0 SP2
Platform          : WINDOWS 
Issue type        : 

Last Reviewed: May 20, 1999