none
COM Interop problem: dynamic property access vs InvokeMember RRS feed

  • Question

  • Hi!

    I've got a bit of code that looks ideally suited to dynamic (COM interop) but somehow doesn't. This is the code:

     

    using System;
    using System.Reflection;
    namespace AcrobatTest
    	{
    	class Program
    		{
    		[STAThread]
    		static void Main(string[] args)
    			{
    			Acrobat.AcroAVDoc D = new Acrobat.AcroAVDoc();
    			D.Open(@"C:\tmp\AcrobatTest\AcrobatTest\d3183wgs01.pdf","XXX");
    			Acrobat.CAcroPDDoc pdDoc = (Acrobat.CAcroPDDoc)D.GetPDDoc();
    
    			// This works
    				Object jsObj = pdDoc.GetJSObject();
    				Type T = jsObj.GetType();
    				double nPages = (double)T.InvokeMember(
    							 "numPages",
    							 BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance,
    							 null, jsObj, null);
    			// This doesn't
    				dynamic JSO = pdDoc.GetJSObject();
    				object O = JSO.numPages;
    			}
    		}
    	}
    
    

     

    The dynamic bit throws this exception:

     

    System.ArgumentException was unhandled
      Message=Value does not fall within the expected range.
      Source=mscorlib
      StackTrace:
           at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
           at System.Dynamic.ComRuntimeHelpers.GetITypeInfoFromIDispatch(IDispatch dispatch, Boolean throwIfMissingExpectedTypeInfo)
           at System.Dynamic.IDispatchComObject.EnsureScanDefinedMethods()
           at System.Dynamic.IDispatchComObject.System.Dynamic.IDynamicMetaObjectProvider.GetMetaObject(Expression parameter)
           at System.Dynamic.DynamicMetaObject.Create(Object value, Expression expression)
           at System.Dynamic.DynamicMetaObjectBinder.Bind(Object[] args, ReadOnlyCollection`1 parameters, LabelTarget returnLabel)
           at System.Runtime.CompilerServices.CallSiteBinder.BindCore[T](CallSite`1 site, Object[] args)
           at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
           at CallSite.Target(Closure , CallSite , Object )
           at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
           at AcrobatTest.Program.Main(String[] args) in C:\tmp\AcrobatTest\AcrobatTest\Program.cs:line 23
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: 
    
    

     


    There is no inner exception.

    Does anybody know what happens here?

    Can I make dynamic work in such a context at all?

    There's no change if I try to assign to a double variable, try to cast, or call JSO.numPages.ToString().

    Lots of Greetings!

     


    Volker
    • Edited by Hetzi Friday, September 2, 2011 3:42 PM
    Friday, September 2, 2011 3:41 PM

All replies