0

I have an issue that I'm having trouble debugging. I'm looking for suggestions on how to track this issue down. Here is my interop definition:

[DispId(1610743816)]
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
public virtual void GetOptions(
    [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BSTR), In] ref Array OptionNames, 
    [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_RECORD)]   out Array Types, 
    [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BOOL)]     out Array IsPortable, 
    [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BOOL)]     out Array IsStartupOnly, 
    [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BSTR)]     out Array Values, 
    [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_I4)]       out Array ErrorIndices, 
    [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_RECORD)]   out Array ErrorCodes, 
    [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BSTR)]     out Array ErrorMsgs);

Here's the call:

Array arrOptionNames;
arrOptionNames = new String[] { "USER" };
System.Array arrTypes;
System.Array arrIsPortable;
System.Array arrIsStartupOnly;
System.Array arrValues;
System.Array arrErrorIndices;
System.Array arrErrorCodes;
System.Array arrErrorMsgs;

opServ.GetOptions(
    ref arrOptionNames,
    out arrTypes,
    out arrIsPortable,
    out arrIsStartupOnly,
    out arrValues,
    out arrErrorIndices,
    out arrErrorCodes,
    out arrErrorMsgs);

Here is the error and stack trace

An Exception Was Encountered - Details Below
Source:  mscorlib
Description:  Specified array was not of the expected type.
Stack Trace:  
   at System.StubHelpers.MngdSafeArrayMarshaler.ConvertSpaceToManaged(IntPtr pMarshalState, Object& pManagedHome, IntPtr pNativeHome)
   at ABC.OptionService.GetOptions(Array& OptionNames, Array& Types, Array& IsPortable, Array& IsStartupOnly, Array& Values, Array& ErrorIndices, Array& ErrorCodes, Array& ErrorMsgs)
   at ABC.Library.ServerTests.SASWorkspaceServerTest.TestServer() in c:\v940m5\ItconfigDotNet\itconfig.library\servertests\ServerTest.cs:line 218

So the StubHelper.MngdSafeArrayMarshaler.ConvertSpaceToManaged has decided that one of my System.Array's (SafeArrays) is of the wrong type. Since there is only one array initialized and it appears to be of the correct type I'm puzzled. Anyone have any ideas about what is going on, or how to debug deeper (ConvertSpaceToManaged appears to be unmanaged).

Dweeberly
  • 4,668
  • 2
  • 22
  • 41
  • All of them, use typed arrays. VT_BSTR should be string[], etc. VT_RECORD might well be a hangup, a matching entry in a type library is required. – Hans Passant Mar 10 '17 at 19:21
  • Perhaps you should think about to change the underlying API to make it more easy to use. For example it might be better to enumerate through a list of objects (containg all those info inside the arrays as Properties), then to have many lists with separated information about the same object instance? – David J Mar 11 '17 at 10:20
  • @DavidJ I would absolutely love to, unfortunately this is an existing(rather old) API that I don't control. – Dweeberly Mar 11 '17 at 16:33
  • In which language it is written? Perhaps you can encapsulale this component into another one with simplfied interface? You can also try to follow the advice looking for a working type lib or changing the Marshalling Code and be some more specific about the underlying unmanaged structures. – David J Mar 11 '17 at 17:11
  • You wrote: **"Since there is only one array initialized and it appears to be of the correct type I'm puzzled."** Which is the Array initialized by the Code? And what happen to the other uninitialized Arrays? Are they returned as null pointer or as empty Arrays? – David J Mar 14 '17 at 08:06

0 Answers0