Solved

C++ COM Method with Optional Arguments

Posted on 2004-10-14
8
422 Views
Last Modified: 2013-11-25
Hi,

I am trying to produce a COM object in Visual C++, where one of the methods accepts optional arguments. Below are the interface declaration, the class implementing the interface, and the implementation of the method taking an optional argument. As it stands, it compiles. But when I try to call the function from an ASP page using VBS with the supposedly optional parameter missing, I get the following error


Microsoft VBScript runtime error '800a01c2'
Wrong number of arguments or invalid property assignment: 'CopyFile'


So the optional attribute seems to be completely ignored.

Any help would be much appreciated.

Here is the code

[uuid("0ab5a3d0-e5b6-11d0-abf5-00a0c90fffc0"),dual]
__interface IFileSystem : IDispatch
{
      [id(0x4b2)] HRESULT __stdcall CopyFile (
        [in] BSTR Source,
        [in] BSTR Destination,
        [in, optional] VARIANT OverWriteFiles );
};



[
  coclass,
  threading("apartment"),
  vi_progid("Scripting.FileSystemObject"),
  progid("Scripting.FileSystemObject.1"),
  version(1.0),
  uuid("4FC4AA66-B04C-406A-B319-3F946FE491B1"),
  helpstring("FileSystemObject Class")
]
class ATL_NO_VTABLE CFileSystemObject :
  public IFileSystem, // Interface to original FileSystemObject
  public IObjectControl
{
public:
  CFileSystemObject() : m_piFSO(NULL), m_bIsAdmin(false), m_bsCurrentPath(NULL), m_hrSave(-1)
  {}

  DECLARE_PROTECT_FINAL_CONSTRUCT()

  HRESULT FinalConstruct()
  {
    return S_OK;
  }

  void FinalRelease()
  {
  }

  // IObjectControl Methods
public:
  STDMETHOD(Activate)();
  STDMETHOD_(void, Deactivate)();
  STDMETHOD_(BOOL, CanBePooled)();

  // Implementation of IFileSystem

  virtual HRESULT __stdcall CopyFile (
    /*[in]*/ BSTR Source,
    /*[in]*/ BSTR Destination,
    /*[in]*/ VARIANT OverWriteFiles );

};




HRESULT __stdcall CFileSystemObject::CopyFile (
/*[in]*/ BSTR Source,
/*[in]*/ BSTR Destination,
/*[in]*/ [in, optional] VARIANT OverWriteFiles )
{
  return S_OK;
}
0
Comment
Question by:snp
  • 3
  • 3
8 Comments
 
LVL 16

Expert Comment

by:nonubik
ID: 12306273
I think your problem is related to this: http://support.microsoft.com/default.aspx?scid=kb;en-us;269035 and is a MS bug
0
 
LVL 16

Expert Comment

by:nonubik
ID: 12306279
But if you'll call that method from C++ it will work without the optional parameter.
0
 

Author Comment

by:snp
ID: 12307535
Thanks, that sounds like it is the problem. Trouble here is that I am trying to create a security wrapper around the FileSystemObject, which must be undetectable to the caller. That means that any parameters that are optional on the original FileSystemObject must also be optional on the wrapper. If this bug still has not been fixed after two years I suppose the chances are it will never be fixed. I am not sure that the description in the article is completely accurate, though. It suggests that the calling environment is responsible (here ASP) for not having access to the type info. However, I have written a similar COM component in VB6, with a method having an optional parameter, and in this case calls from ASP work with or without the optional parameter.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 16

Expert Comment

by:nonubik
ID: 12307598
Yes, I think too the calling environment is responsible for not having access to the type info.
0
 

Author Comment

by:snp
ID: 12316443
Its possible, I suppose. But if so, I don't know why optional parameters should work on a COM component written in VB6 when it is called from the same ASP page as the C++ com component.
0
 

Author Comment

by:snp
ID: 12337243
using VARIANT * as the optional parameter type seems to work.
0
 

Accepted Solution

by:
RomMod earned 0 total points
ID: 12389175
The question has been PAQ'd and the 100 points have been refunded.
RomMod
Community Support Moderator
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now