-1

I've inherited a project that includes a COM DLL. I'm sort of new to COM but something doesn't seem right. The interface defined in the IDL only uses the VARIANT type for all properties and method returns/parameters. Is there any possible justification for this? I have a feeling the previous developer was just winging some things, but I want to be sure.

Here's what my IDL looks like:

interface IMyComInterface : IDispatch
{
    [id(1), helpstring("method CheckMessage")] HRESULT CheckMessage([in] VARIANT vMsg);
    [id(2), helpstring("method CheckFolder")] HRESULT CheckFolder([in] VARIANT Folder, [out] VARIANT *pCount, [out, retval] VARIANT *pErrorCount);
    [propget, id(3), helpstring("property Flags")] HRESULT Flags([out, retval] VARIANT *pVal);
    [propput, id(3), helpstring("property Flags")] HRESULT Flags([in] VARIANT newVal);
    [propget, id(4), helpstring("property MessageStore")] HRESULT MessageStore([out, retval] VARIANT *pVal);
    [propput, id(4), helpstring("property MessageStore")] HRESULT MessageStore([in] VARIANT newVal);
    [propget, id(5), helpstring("property Directory")] HRESULT Directory([out, retval] VARIANT *pVal);
    [propput, id(5), helpstring("property Directory")] HRESULT Directory([in] VARIANT newVal);
    [propget, id(6), helpstring("property MessageCount")] HRESULT MessageCount([out, retval] VARIANT *pVal);
};

Much thanks.

EDIT:

To make things clear, all of these VARIANTs could be replaced by explicit types.

Frank Weindel
  • 1,212
  • 1
  • 13
  • 31
  • looks like you inherited my project. sorry :p so VARIANTs are basically generic types that can be anything. They're like Object in c#. The idea is that these types can change at runtime. This is so that your interface can be compatible with runtime type binding languages (like VB, C#, etc.). – thang Feb 19 '13 at 17:33
  • Well, check the error handling in the code first. Nice if it generates a friendly error message through IErrorInfo. If you leave it up to the IDispatch stock implementation then the DISP_E_BADVARTYPE, "Bad variable type" you'd get when a variable of the wrong type is passed is not the greatest. Get a better answer by describing the scripting language that's most commonly used with this server. – Hans Passant Feb 20 '13 at 01:34
  • Only a C++ application uses this COM server. – Frank Weindel Feb 20 '13 at 15:34

3 Answers3

1

If you need performance or simplification, you change VARIANT type for appropriate values, more closely to type used. Remember, it is an interface break.
View valid values to use :
COM Data Types

lsalamon
  • 7,998
  • 6
  • 50
  • 63
1

Your interface derives from IDispatch which means it's a dual interface - you can call the methods directly from the interface, or you can call them through iDispatch::Invoke. The parameters passed to Invoke must all be of type VARIANTARG which is just another name for VARIANT, see http://msdn.microsoft.com/en-us/library/ms891678.aspx.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • So to have the capability of being a dual interface all of the parameters in the interface have to be of type `VARIANT`? `Invoke` doesn't handle the marshaling from `VARIANT` to the types described in the interface? – Frank Weindel Feb 20 '13 at 01:03
  • IDispatch::Invoke() is supposed to do all the marshaling. – Luke Feb 20 '13 at 17:54
0

For certain types of dual interfaces that suppose to work with VB and scripting languages it is better to use VARIANT that direct type. For example it is better to use VARIANT over almost all kind of SAFEARRAY and interface pointers. But you may test and see.

BigBoss
  • 6,904
  • 2
  • 23
  • 38