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 7647979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:35:48+00:00 2026-05-31T10:35:48+00:00

We have a legacy COM DLL with the following simplified IDL for some method:

  • 0

We have a legacy COM DLL with the following simplified IDL for some method:

AllocateMemory( [in] LONG lSize, [out] LONG *pAddr );

Implementation of this method contains the following:

BYTE *pArr = (BYTE*)CoTaskMemAlloc( lSize );  
*pAddr = &pArr;  

The address of newly allocated memory is returned as a long value back to the caller.

C++ clients utilizing this COM object can invoke that method and then access the memory as follows:

//---- C++ ----------------   
long lSize = 10;  
long lAddr;  
pCOMObj->AllocateMemory( lSize, &lAddr );  
byte **bArray = (byte**)lAddr;  
for (int i = 0; i < iSize; i++)  
{  
    printf( "array[%d] = %d\n", i, (*bArray)[i] );  
}  
// Now deallocate memory.  
CoTaskMemFree( *bArray ); 
//--------------------

All works well from C++ COM client.

The same COM DLL was added as a reference to a Visual Studio 2010 project.
Our goal is to be able to do the same from C# client using Interop:

//---- C# ----------------  
int iSize = 10;  
int iAddr = 0;  
objCOM.AllocateMemory( iSize, ref iAddr );  
...    

// ??? *UNKNOWN*  
// At this point we need to be able to access allocated memory pointed to by iAddr.  


// The following does NOT work, i.e., no errors occur but array contents are totally
// different from what has been initialized inside AllocateMemory() method, implying 
// that totally different memory is being accessed/copied:    
byte [] bArray = new byte [iSize];  
IntPtr rAddr = new IntPtr( iAddr );  

// Marshal the array from an unmanaged to a managed heap.  
Marshal.Copy( rAddr, bArray, 0, iSize );  
for (int i=0; i < iSize; i++)  
   Console.WriteLine( bArray[i] );  

// Release the unmanaged array.  
Marshal.FreeCoTaskMem( rAddr );  
//--------------------

Does anyone know of a potential solution?

  • 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-31T10:35:49+00:00Added an answer on May 31, 2026 at 10:35 am
    IntPtr bAddr = new IntPtr( iAddr );  // bAddr = (byte**)iAddr
    IntPtr rAddr = new IntPtr(Marshal.ReadInt32(bAddr)); //rAddr = (*bAddr);
    
    byte [] bArray = new byte [iSize];   
    // Marshal the array from an unmanaged to a managed heap.   
    Marshal.Copy( rAddr, bArray, 0, iSize );   
    for (int i=0; i < iSize; i++)   
       Console.WriteLine( bArray[i] );   
    
    // Release the unmanaged array.   
    Marshal.FreeCoTaskMem( rAddr );  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a legacy 3rd party COM .dll that is used to provide an
I have a project that uses some legacy COM components. When I included them
I have a legacy COM-component with an interface declaring a property like this (IDL
We have some legacy code that needs to identify in the Page_Load which event
Windows 7, C++, VS2008 I have a COM DLL that needs to be registered
I have a legacy delphi dll. I created a class library that imports the
I have a .NET v2.0 Dll that exposes a few classes to COM. The
I have the following COM object installed on one of our servers that I
I have legacy COM component, and my mission is to write web service that
I have to develop extension for legacy application that relies on com objects. My

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.