Solved

C++ COM Method with Optional Arguments

Posted on 2004-10-14
8
425 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

816 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

8 Experts available now in Live!

Get 1:1 Help Now