Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C++ COM Method with Optional Arguments

Posted on 2004-10-14
8
Medium Priority
?
476 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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

963 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