Solved

C++ COM Method with Optional Arguments

Posted on 2004-10-14
8
430 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

756 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