- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have been attempting to pass a string array contained in a variant to the AutoCAD method AcadPlot.SetLayoutsToPlot. I am using late binding without a Interop assembly because we have several versions of AutoCAD in this company and I would like to support all of them from the same code base.
The method wants a variant that contains a string array passed byref. The Microsoft documentation on COM interop from VB.NET makes it sound like the compiler will marshal it correctly by default, but it always throws a COMException (Exception from HRESULT: 0x80070057 (E_INVALIDARG)).
The stack trace shows it is trying to anyway:
StackTrace:
at Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateCall(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack, Boolean IgnoreReturn)
at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
But no matter how I code it I cannot get this variant array passed to AutoCAD.
I have tried:
Imports System.Runtime.InteropServices
' VB default marshalling
Dim layoutarr = New Object() {"Layout1"}
doc.Plot.SetLayoutsToPlot(layoutarr)
' using a VariantWrapper
Dim layoutarr = New Object() {"Layout1"}
Dim wrapper As New System.Runtime.InteropServices.VariantWrapper(layoutarr)
doc.Plot.SetLayoutsToPlot(wrapper)
'making a method and using a custom marshaling attribute
Dim layoutarr = New Object() {"Layout1"}
doc.Plot.SetLayoutsToPlot(MarshalVariantStringArray(layoutarr))
Public Function MarshalVariantStringArray(ByVal ObjIn As Object) As <MarshalAsAttribute(UnmanagedType.SafeArray, SafeArraySubType:=VarEnum.VT_BSTR)> Object
Return ObjIn
End Function
'using reflection to invoke the method
'(not sure why that would help, but it was worth a try)
Dim layoutarr = New Object() {"Layout1"}
Dim argarr(0 To 0) As Object
argarr(0) = layoutarr
doc.Plot.GetType().InvokeMember("SetLayoutsToPlot", Reflection.BindingFlags.InvokeMethod, Nothing, doc.Plot, argarr)
Does anyone know what to do in this situation? As intensively as AutoCAD ActiveX uses variant arrays, I know this problem is going to come up again and again for me as I try to migrate code over to .NET.
Thanks.
Solved! Go to Solution.