0

I write an Ole Db Provider on c# (COM tehnology).

Ole Db Provider is .NET DLL.

There is a problem with InvalidCastException: Additional information: Unable to cast "MyOleDbProvider.RowsetObject" to type "IRowset".

This problem occurs when execution a 'GetRowset' method from Ole Db Client. In msdn:

HRESULT GetRowset (IUnknown       *pUnkOuter,
                   REFGUID         rguidSchema,
                   ULONG           cRestrictions,
                   const VARIANT   rgRestrictions[],
                   REFIID          riid,
                   ULONG           cPropertySets,
                   DBPROPSET       rgPropertySets[],
                   IUnknown      **ppRowset);

I converted the 'GetRowset' such as

[PreserveSig]
HRESULT GetRowset(
            [In] IntPtr pUnkOuter,
            [In] ref Guid rguidSchema,
            [In] int cRestrictions,
            [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] object[] rgRestrictions,
            [In] ref Guid riid,
            [In] int cProperties,
            [In] IntPtr rgPropertySets,
            [Out, MarshalAs(UnmanagedType.Interface, IidParameterIndex = 4)] out object ppRowset);

Implementation:

HRESULT IDBSchemaRowset.GetRowset(IntPtr pUnkOuter, ref Guid rguidSchema, int cRestrictions, object[] rgRestrictions,
            ref Guid riid, int cProperties, IntPtr rgPropertySets, out object ppRowset)
        {
            // not significantly begin
               ...
            // not significantly end

            var rowsetObject = new RowsetObject(rguidSchema);

            if (pUnkOuter == IntPtr.Zero)
            {               
                // always here
                ppRowset = rowsetObject;
                return HRESULT.S_OK;
            }
            else
            {                    
                ppRowset = rowsetObject.AggregateIn(pUnkOuter);
            }

            return HRESULT.S_OK;
        }

When i pass ppRowset InvalidCastException occurs.

public class RowsetObject : IRowset
{
   ...
}

[ComImport]
[Guid("0C733A7C-2A1C-11CE-ADE5-00AA0044773D")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IRowset
{
    ...
}

This problem occurs only when client is .NET client. The .NET client uses System.Data.OleDb namespace. If client is native client there is no problem.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
plexusilnur
  • 61
  • 1
  • 5
  • [Out, MarshalAs(UnmanagedType.Interface)] out object ppRowset OR [Out, MarshalAs(UnmanagedType.Interface)] out IRowset ppRowset doesn't work – plexusilnur Nov 19 '15 at 11:16
  • Why MyOleDbProvider.RowsetObject directly casts to client's IRowset? (i think without Marshal) – plexusilnur Nov 19 '15 at 11:18

0 Answers0