Populating an array<System::UInt32>^ from an unmanaged array in the new C++/CLI syntax

B

Bern McCarty

In the old MEC++ syntax I can do this:

// compile in VS 2005 shell with cl -clr:blush:ldsyntax -LD ArrayCopyOldSyntax.cpp
#using <mscorlib.dll>

public __gc class CopyTest
{
private: System::UInt32 m_indeces __gc[];

public: CopyTest(unsigned int indeces __nogc[], unsigned int arrayLength)
{
m_indeces = __gc new System::UInt32[arrayLength];
System::Runtime::InteropServices::Marshal::Copy(System::IntPtr(indeces),
m_indeces, 0, arrayLength);
}
};


And there is a little magic there in that there is no Marshal::Copy overload
that takes an UInt32 __gc[] as an argument for the destination of the copy,
but the compiler happily accepts it and it simply invokes the Copy overload
that takes an Int32 __gc[] as the destination. In other words, it more or
less does an implied reinterpret_cast for you.

But the equivalent class in the new syntax will not compile:

// compile in VS 2005 shell with cl -clr -LD ArrayCopyNewSyntax.cpp
public ref class CopyTest
{
private: array<System::UInt32>^ m_indeces;

public: CopyTest(unsigned int indeces[], unsigned int arrayLength)
{
m_indeces = gcnew array<System::UInt32>(arrayLength);
System::Runtime::InteropServices::Marshal::Copy(System::IntPtr(indeces),
m_indeces, 0, arrayLength);
}
};

ArrayCopyNewSyntax.cpp(11) : error C2665: 'System::Runtime::InteropServices::Marshal::Copy'
: none of the 16 overloads could convert all the argument types

It appears that I can reinterpret_cast m_indeces to array<Int32>^ explicitly
and it will work, and of course I could always write my own loop to do the
copy, but is there a more elegant way to do this in the new syntax? The
Marshal.Copy overloads are very handy for interop code, but getting into
the habit of doing reinterpret casts on object handles doesn't seem like
a good idea.

-Bern McCarty
 
G

Gary Chang[MSFT]

Hi Dave,

Currently we will perform some research on this issue, I will update you as
soon as possible.

Thanks!

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Gary Chang[MSFT]

Hi Bern,

I have already consulted this issue with our product team, there is no more
elegant way to achieve this in the new C++/CLI compiler. The VC2005
complier is more strict than VC2003's, I am afraid some MEC++'s approaches
may not work under VC2005.

Thanks!

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top