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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:59:34+00:00 2026-06-15T12:59:34+00:00

I have a managed function with the following declaration (both interface and implementation): [return:

  • 0

I have a managed function with the following declaration (both interface and implementation):

[return: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BSTR)]
String[] ManagedFunction()
{
    String[] foo = new String[1];
    foo[0] = "bar";
    return foo;
}

There is also a native C++ interface with the same methods as the managed interface, inside of that interface, this method has the following declaration:

void ManagedFunction(SAFEARRAY* foo); 

This function is called by native code in the following way:

void NativeFunction(ManagedBinding binding)
{
    CComSafeArray<BSTR> cComSafeArray;
    cComSafeArray.Create(); 
    LPSAFEARRAY safeArray = cComSafeArray.Detach();
    binding.comObject->ManagedFunction(safeArray); 
}

I’m not sure what I’m doing wrong but after my managed function has been called, safeArray appears to have garbage values, somethings going wrong while marshalling the return value back to native code. Could someone with more experience than me with .Net interop please shed some light on this? Also, It might be relevant to mention that I haven’t had problems with returning ValueTypes from my managed function (boolean if you’re curious), something about returning a String array is messing things up. Thanks!

  • 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-15T12:59:36+00:00Added an answer on June 15, 2026 at 12:59 pm

    Well I finally got it to work. I created a managed representation of SAFEARRAY called ManagedSafeArray (stolen from here : http://social.msdn.microsoft.com/Forums/en-US/clr/thread/6641abfc-3a9c-4976-a523-43890b2b79a2/):

    [StructLayout(LayoutKind.Sequential)]
    struct ManagedSafeArray
    {
        public ushort   dimensions;     // Count of dimensions in the SAFEARRAY
        public ushort   features;       // Flags to describe SAFEARRAY usage
        public uint     elementSize;    // Size of an array element
        public uint     locks;          // Number of times locked without unlocking
        public IntPtr   dataPtr;        // Pointer to the array data
        public uint     elementCount;   // Element count for first (only) dimension
        public int      lowerBound;     // Lower bound for first (only) dimension
    }
    

    I changed the signature of my method to:

    void ManagedMethod(ref ManagedSafeArray foo);
    

    Inside my method, I manually updated the dataPtr field by calling Marshal.AllocCoTaskMem(...) and then copied over the strings I wanted the SAFEARRAY to contain.

    I have no idea as to why the CLR wasn’t able to automatically marshal the parameters to and from native code and I’d still appreciate it if someone could try to explain that.

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

Sidebar

Related Questions

We have a COM component who’s implementation and interface definition exist in managed code
I have the following code: function doSomething(str){ return str+=a; } function anotherFunction(str){ return str+=b;
I have managed to create a javascript dropdown using the following fiddle: (function() {
I have the following code: $(document).ready(function() { // Manage sidebar category display jQuery(#categories >
I'm a newbie to jquery, but have managed to make a show-function and hover
I have a view function which needs to be manually transaction managed, but when
I have managed to confuse myself whether I should return E_NOTIMPL or E_NOINTERFACE from
I have managed to get my click event working with the following code. When
I have a website where I want the following page to function - it
I have the following C++ function and C# p/invoke decleration: //C# [DllImport(capture.dll, EntryPoint =

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.