Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 921895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:56:26+00:00 2026-05-15T18:56:26+00:00

I have a COM interface method definition in IDL as follows: [id(8)] HRESULT GetBinData([in,out,size_is(dataLen)]BYTE

  • 0
  • I have a COM interface method definition in IDL as follows:

    [id(8)]
    HRESULT GetBinData([in,out,size_is(dataLen)]BYTE data[], [in]LONG dataLen);
    
  • It is mapped automatically to this .NET IL code (note that no MarshallAs LPArray is used on data):

    .method /*06000021*/ public hidebysig newslot virtual 
       instance void GetBinData([in][out] uint8& Data,
                                [in] int32 dataLen) runtime managed internalcall
    // SIG: 20 02 01 10 05 08
    {
      .custom /*0C000052:0A000009*/ instance void
         [mscorlib/*23000001*/]
         System.Runtime.InteropServices.DispIdAttribute/*0100000F*/::.ctor(int32)
           /* 0A000009 */ = ( 01 00 08 00 00 00 00 00 ) 
      .override test.ISomething/*02000002*/::GetBinData/*02000002::06000008*/ 
    } // end of method SomethingClass::GetBinData
    
  • That code looks in C#:

    [MethodImpl(MethodImplOptions.InternalCall,
        MethodCodeType=MethodCodeType.Runtime), DispId(8)]
    public virtual extern void GetBinData
        ([In, Out] ref byte Data, [In] int dataLen);
    
  • My code (that seems to work ok) uses it as shown next:

    byte[] b = new byte[1024];
    someObject.GetBinData(ref b[0], b.Length);
    
  • Now my question is NOT how to do this better (I know that) but:
    (a) why my code above works at all?
    (b) are there cases when such code may not work (eg., memory moved inside CLR while calling GetBinData, etc).
    • 1 1 Answer
    • 0 Views
    • 0 Followers
    • 0
    Share
    • Facebook
    • Report

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Forgot Password?

    Need An Account, Sign Up Here

    1 Answer

    • Voted
    • Oldest
    • Recent
    • Random
    1. Editorial Team
      Editorial Team
      2026-05-15T18:56:27+00:00Added an answer on May 15, 2026 at 6:56 pm

      Garbage collection should not concern you, since you object is pinned automatically as soon as you pass it to the method (source: MSDN):

      When the runtime marshaler sees that
      your code is passing to native code a
      reference to a managed reference
      object, it automatically pins the
      object.

      In case that your native method saves the reference (pointer) for some later async work, you must pin it manually:

      byte[] b = new byte[1024];
      GCHandle pinHandle = GCHandle.Alloc(b, GCHandleType.Pinned);
      try
      {
         someObject.GetBinData(ref b[0], b.length);
      }
      finally
      {
         pinHandle.Free();
      }
      

      Otherwise, there is no reason why it shouldn’t work. You are allocating the memory before calling the method, CLR pins your object until the method is executed, and your native code should take care that array length is taken into account.

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
        • Report

    Sidebar

    Related Questions

    I have a COM interface with a following method definition (IDL notation): SCODE GetText( [in,
    We have a COM component who’s implementation and interface definition exist in managed code
    I have the following interface which I'm trying to make COM-visible. When I try
    We have a COM object implemented with C++/ATL that includes a method which will
    I have a composite component: <composite:interface> <composite:attribute name=actionMethod method-signature=java.lang.String action() required=true /> </composite:interface> <composite:implementation>
    I have a COM object written using the MS ATL library. I have declared
    I have a COM dll written in vb6. When I try to create a
    I have a COM interop assembly, and I would like to check from a
    I have a COM SDK written in C++ and I'd like to create documentation
    I have a VB6 COM component which I need to call from my .Net

    Explore

    • Home
    • Add group
    • Groups page
    • Communities
    • Questions
      • New Questions
      • Trending Questions
      • Must read Questions
      • Hot Questions
    • Polls
    • Tags
    • Badges
    • Users
    • Help
    • SEARCH

    Footer

    © 2021 The Archive Base. All Rights Reserved
    With Love by The Archive Base

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.