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

  • Home
  • SEARCH
  • 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 8219511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:11:29+00:00 2026-06-07T13:11:29+00:00

We have a C#/.Net COM server (COM-visible assembly) with the following interface & class

  • 0

We have a C#/.Net COM server (COM-visible assembly) with the following interface & class declarations:

[ComVisible(true)]
[Guid( "..." )]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ITest
{
    void TestMethod( [In] int nNumElements, 
                     [In, Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0)] byte [] bArray );
}

[ComVisible(true)]
[Guid( "..." )]
[ClassInterface( ClassInterfaceType.None )]
[ProgId( "CSCOMServer.CSTest" )]
public class CSTest : ITest
{
    public void TestMethod( int nNumElements, byte [] bArray )
    {
        for (int i = 0; i < bArray.Length; i++)
        {
            bArray [i] = (byte)i;
        }
    }
}

The resulting IDL/typelib is:

...
interface ITest : IUnknown {
    HRESULT _stdcall TestMethod(
                    [in] long nNumElements, 
                    [in, out] unsigned char* bArray);
};

...
coclass CSTest {
    interface _Object;
    [default] interface ITest;
};

The goal is to be able to utilize this from a C++ COM client, which works fine using the following:

int iSize = 10;
ITest *test;
byte *buf  = (byte*)CoTaskMemAlloc( iSize * sizeof(byte) );
CoCreateInstance( CLSID_CSTest, NULL, CLSCTX_INPROC_SERVER, IID_ITest, (void **) &test );
test->TestMethod( iSize, buf );

The buffer is allocated by C++ and passed to C# for population.
All works well and upon the completion of TestMethod() the C++ array (buf) contains correct values set by the C# method.

The question is about efficiency:
Does the Interop wrapper perform array copying during marshaling (“In” phase) and then back (“Out” phase) OR does the C# method operate directly on the passed in memory (which may be pinned)?

  • 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-06-07T13:11:30+00:00Added an answer on June 7, 2026 at 1:11 pm

    No, a copy is definitely required here since you ask for a structural change. The CLR must create a managed array object to meet the byte[] argument type requirement. And the source array isn’t in any way usable as-is since it is nothing but a raw chunk of memory. The other way around (byte[] to unsigned char*) can work but that’s not the case here.

    The normal COM automation type for arrays is SAFEARRAY btw, no speed advantage but there are a lot more COM clients that could use your server.

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

Sidebar

Related Questions

I have the following interface defined to expose a .NET class to COM: [InterfaceType(ComInterfaceType.InterfaceIsDual)]
I have a c# .NET assembly that gets called from a C++ COM client.
I have trouble to expose a .NET assembly in COM. It seems that I
I have a asp.net site www.example.com on a Microsoft server. I also have a
I have a .Net COM server that has a method that takes a message
If I want to make a .NET assembly usable as a COM server I
We've created a C# class library assembly and made it COM visible to be
I have a .NET user control registered as COM. I use regasm to register
I have CruiseControl.net running Visual Studio (2005/2008 - using devenv.com) as we need to
Here's the core problem: I have a .NET application that is using COM interop

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.