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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T13:52:45+00:00 2026-05-16T13:52:45+00:00

I need to marshal an array of String^ to call a unmanaged function that

  • 0

I need to marshal an array of String^ to call a unmanaged function that expects an array of BSTRs.

On MSDN I found the article

How to: Marshal COM Strings Using C++ Interop

with this code sample:

// MarshalBSTR1.cpp
// compile with: /clr
#define WINVER 0x0502
#define _AFXDLL
#include <afxwin.h>

#include <iostream>
using namespace std;

using namespace System;
using namespace System::Runtime::InteropServices;

#pragma unmanaged

void NativeTakesAString(BSTR bstr) {
   printf_s("%S", bstr);
}

#pragma managed

int main() {
   String^ s = "test string";

   IntPtr ip = Marshal::StringToBSTR(s);
   BSTR bs = static_cast<BSTR>(ip.ToPointer());
   pin_ptr<BSTR> b = &bs;

   NativeTakesAString( bs );
   Marshal::FreeBSTR(ip);
}

So I created a new BSTRs’ array and called the Marshal::StringToBSTR() for every String of the array.
Then I created a managed pin_ptr array.

array<pin_ptr<BSTR> >^ gcDummyParameters = gcnew array<pin_ptr<BSTR> >(asParameters->Length);

but I receved the error:

Error   2   error C2691: 'cli::pin_ptr<Type>' : a managed array cannot have this element type

I tried also with a native array:

pin_ptr<BSTR> dummyParameters[100000];

but even in this case I got an error:

Error   1   error C2728: 'cli::pin_ptr<Type>' : a native array cannot contain this managed type 

What else can I do?

  • 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-16T13:52:46+00:00Added an answer on May 16, 2026 at 1:52 pm

    Microsoft sample looks strange: there is no need to pin BSTR type because it is unmanaged. Just create BSTR array and fill every member using Marshal::StringToBSTR. Don’t use pin_ptr.

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

Sidebar

Related Questions

My .NET library has to marshal strings to a C library that expects text
I'm using Marshal.dump to serialize an array of objects, I need to get the
I have a C function with prototype, void* VoidPointer(void*); Now I need to marshal
I need to read unmanaged memory into a managed byte array. For this I
I'm implementing an unmanaged array class in C# which I need for some OpenGL
I need a recommendation for a pythonic library that can marshall python objects to
Need a map reduce function by mongo in php This my mongo structure [_id]
Need help, function getFamily() { FB.api('/me/family', function(response) { alert(JSON.stringify(response)); }); } With the above
Need help with a query that I wrote: I have three tables Company id
I need to use Jackson to marshal and unmarshal json on arbitrary objects, and

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.