0

After resolving one issue with IMetaDataImport, I'm dealing with another for quite a long time. It's with EnumGenericParams method.

The method throws AccessViolationExpcetion, but this happens only sometimes. In the other cases the method returns from calling without any problems, but its parameters are the same as the parameters when it throws expcetion. I can't find out why only sometimes.

Also, the AccessViolationExpcetion can't be catch by catch statement. When debugging in VS 2010, the exception TargetInvocationException is first caught with InnerException set to AccessViolationExpcetion.

The definition of IMetaDataImport2 and EnumGenericParams:

[ComImport]
[Guid("<valid GUID>")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[CLSCompliant(false)]
public interface IMetaDataImport2 : IMetaDataImport {
....
    void EnumGenericParams(
     [ComAliasName("HCORENUM*"), In, Out] ref IntPtr phEnum,
     [ComAliasName("mdToken")] mdToken tk,
     [ComAliasName("mdGenericParam*"), Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex=3)] mdToken[] rGenericParams,
     [ComAliasName("ULONG")] uint cGenericParams,
     [ComAliasName("ULONG*"), Out] out uint pcGenericParams
     );
    ....

The method calling:

metadataImport2.EnumGenericParams(ref pEnum, memberDef, null, 0, out genParamCount);

(Even calling with third parameter not null and fourth > 0 leads to exception.)

The problem occured when I moved app from .NET 3.5 to .NET 4.

Thank you for any help!

Community
  • 1
  • 1
Paulie
  • 81
  • 7
  • After moving the app back to .NET 3.5 (ClientProfile), the method still throws AccessViolationExpcetion, but it can be catch by catch statement. – Paulie Jan 19 '12 at 19:21
  • Was pEnum set to IntPtr.Zero before the first call? If you pass pEnum to CloseEnum (and plan to re-use the field) then you will need to zero it again. EnumGenericParams uses the field pass state from the first call to subsequent calls. Also, what is the definition of mdToken you are using in your C# code? if it's not a 4-byte value type then that could also cause issues. – Brian Reichle Mar 27 '14 at 11:14

1 Answers1

0

Uff (for the second time :-) ), it seems that setting SecurityPermissions high enough can get rid of the problem:

[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.AllFlags)]
void EnumGenericParams(
  [ComAliasName("HCORENUM*"), In, Out] ref IntPtr phEnum,
  [ComAliasName("mdToken")] mdToken tk,
  [ComAliasName("mdGenericParam*"), Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex=3)] mdToken[] rGenericParams,
  [ComAliasName("ULONG")] uint cGenericParams,
  [ComAliasName("ULONG*"), Out] out uint pcGenericParams
  );

Also, assembly with the IMetaDataInterface2 should be under .NET 3.5 (or possibly lower).

Paulie
  • 81
  • 7
  • An access violation happens when the process tries to access a virtual address it wasn't supposed to and indicates a coding error, not a security rights error (think of it as like a more general NullReferenceException). I would be _very_ concerned if incorrect security rights caused that. I think it's more likely that changing the rights changed the environment enough to make the error less likely to happen. – Brian Reichle Mar 27 '14 at 11:30