views:

43

answers:

0

Hi all,

(I am coding in C#)

I want to pass a pointer to structure through an API. Say structure is,

 [StructLayout(LayoutKind.Sequential,CharSet= CharSet.Ansi)]
      public struct PassThisStruct
        {
           public Int32 Param1;
           public Int32 Param2;
          [MarshalAs(UnmanagedType.ByValArray, SizeConst=13)]
           public Char[] OutputData;

        };

Now the API declaration is:-

unsafe public delegate Int32 MyAPI(Int16 Something, PassThisStruct* StructPtr);

i.e. I want to pass Structure pointer to Structure.

I made a delegate for this API and then retrived address from DLL and stored it in Function Pointer. But When I try to compile, it gives me error at the delegate declaration that, "Cannot take address of, get size of, or declare pointer to managed space".

So how should pass the pointer? I do know that I can use "ref" but this API is written in C++ and it wants a structure pointer. If I use ref I can't pass Null and I am also getting error as "AccessViolationException" (That's one more big problem for me!). So I would use ref only if I can get rid of AccessViolationException.

One more question, Can I pass Null in this case?? If Yes, How?

Regards!