Quantcast
Channel: Common Language Runtime Internals and Architecture forum
Viewing all articles
Browse latest Browse all 1710

“Value does not fall within the expected range.” when trying to call a method on a COM object using c#

$
0
0

I get an exception when trying to call a method on a COM object using c#.

This only happens when I try to call the method without using the COM runtime callable wrapper (RCW) interop assembly generated by tlbimp.exe.

When strictly using the COM interop assembly, everything works perfectly.

The reason I don't want to use the COM interop assembly is because the CLSIDs and ProgIDs may be different on the target machine than when the interop assembly is created, but the method names, definitions, and structures are the same.

In this test, however, the COM server is registered using the same CLSIDs and ProgIDs as those defined in the runtime callable wrapper (RCW) which was directly generated from the running COM server assembly.

Running on Windows 7 SP1 64-bit.
C++ COM server is 32-bit
C# assembly is built using "Any CPU" and targets .NET Framework 4

C++ COM server IDL code snippet:

[object,
uuid(MyComObjectInterfaceGuid),
dual,
helpstring("MyComObject Interface"),
pointer_default(unique)]interfaceIMyComObject:IDispatch{#import "MyCustomStruct.h"[id(110), helpstring("Method110")] HRESULT Method110([out] MY_CUSTOM_STRUCT *pMyCustomStruct);}[
uuid(MyComObjectGuid),
helpstring("MyComObject")]
coclass MyComObject{[default]interfaceIMyComObject;};

MyCustomStruct.h:

typedefstruct MY_CUSTOM_STRUCT{
    bool            property1;longunsigned   property2;wchar_t         property3[42];int             property4;} MY_CUSTOM_STRUCT;

When the COM server DLL is registered, the rgs file inserts the ProgId registry values for example.

The runtime callable wrapper produces the following structure:

publicstruct MY_CUSTOM_STRUCT{publicbyte property1;publicuint property2;publicushort[] property3;publicint property4;}

C# code snippet that works:

using MyComObjectInteropNamespace;MyComObject myComObject =newMyComObject();

MY_CUSTOM_STRUCT myCustomStruct =new MY_CUSTOM_STRUCT();
myComObject.Method110(out myCustomStruct);// myCustomStruct contains the expected values

C# code snippet that throws an exception:

using MyComObjectInteropNamespace;Type myComObjectType =Type.GetTypeFromProgID("Company.Product.MyComObject",true);object myComObject =Activator.CreateInstance(myComObjectType);

MY_CUSTOM_STRUCT myCustomStruct =new MY_CUSTOM_STRUCT();object[] args ={ myCustomStruct };ParameterModifier parameterModifier =newParameterModifier(1);
parameterModifier[0]=true;ParameterModifier[] parameterModifiers ={ parameterModifier };

myComObjectType.InvokeMember(// exception is thrown here"Method110",BindingFlags.InvokeMethod,null,
    myComObject,
    args,
    parameterModifiers,null,null);// code does not get this far
myCustomStruct =(MY_CUSTOM_STRUCT)args[0];

Exception:

System.ArgumentException occurredHResult=-2147024809Message=Value does not fall within the expected range.Source=mscorlibStackTrace:
       at System.RuntimeType.InvokeDispMethod(String name,BindingFlags invokeAttr,Object target,Object[] args,Boolean[] byrefModifiers,Int32 culture,String[] namedParameters)
       at System.RuntimeType.InvokeMember(String name,BindingFlags bindingFlags,Binder binder,Object target,Object[] providedArgs,ParameterModifier[] modifiers,CultureInfo culture,String[] namedParams)InnerException:

C# code snippet that also throws the same exception:

using MyComObjectInteropNamespace;Type myComObjectType =Type.GetTypeFromProgID("Company.Product.MyComObject",true);object myComObject =Activator.CreateInstance(myComObjectType);object[] args ={newUnknownWrapper(null)};ParameterModifier parameterModifier =newParameterModifier(1);
parameterModifier[0]=true;ParameterModifier[] parameterModifiers ={ parameterModifier };

myComObjectType.InvokeMember(// exception is thrown here"Method110",BindingFlags.InvokeMethod,null,
    myComObject,
    args,
    parameterModifiers,null,null);// code does not get this far
MY_CUSTOM_STRUCT myCustomStruct =(MY_CUSTOM_STRUCT)args[0];

C# code snippet that also throws an exception:

dynamic myComObject =Activator.CreateInstance(Type.GetTypeFromProgID("Company.Product.MyComObject",true));

MY_CUSTOM_STRUCT myCustomStruct =new MY_CUSTOM_STRUCT();
myComObject.Method110(out myCustomStruct);// exception is thrown here

Exception:

System.ArgumentException occurredHResult=-2147024809Message=Value does not fall within the expected range.Source=mscorlibStackTrace:
       at System.Runtime.InteropServices.Marshal.GetNativeVariantForObject(Object obj,IntPtr pDstNativeVariant)
       at System.Dynamic.UnsafeMethods.InitVariantForObject(Object obj,Variant& variant)InnerException:null

Also posted on stackoverflow here:

http://stackoverflow.com/questions/23074846/value-does-not-fall-within-the-expected-range-when-trying-to-call-a-method-on


Viewing all articles
Browse latest Browse all 1710

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>