471,337 Members | 794 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Best practice needed to marshal STL data from managed code to unmanaged code & vice versa

Best practice needed to marshal STL data from managed code to unmanaged code & vice vers

managed code is written in managed C++ & accesses the unmanaged code (i.e. lives in a mixed mode DLL
unmanaged code is written in unmanaged C++ & resides in an unmanaged DL
Product used is VS.NET 200

Please tell me what is the best practice for getting and setting data fro
unmanaged to managed part when I have a class with references to STL objects as member
function parameters
For instance if I have the following class in an unmanaged Dll

class UNMAN1_API CUnman1

int GetStdString(std::string & strText)
int GetStdVector(std::vector<std::string>& vectText)

int SetStdString(const std::string & strText_p)
int SetStdVector(const std::vector<std::string>& vectText)

I can create the class in a managed C++ mixed mode Dll without problems. I searched the Web (Internet) and have found a solution for setting the standard string, whic
looks like follows

int Form2::TestSTL2(void

CUnman1 cUnman1
std::string stdstrText

String *pStrTmp = S"This is a string with more than 15 characters.\r\n"
IntPtr intPtr = Marshal::StringToHGlobalAnsi(pStrTmp);
char *pcTmp = static_cast<char*>(intPtr.ToPointer());

stdstrText = pcTmp


return 0
But it looks strange because when you assign the unmanaged allocated character array to the standar
string, should there not be a new allocation in the standard string class?. Is this kind of processing correct

For the other three function
- int GetStdVector(std::vector<std::string>& vectText)
- int GetStdString(std::string & strText)
- int SetStdVector(const std::vector<std::string>& vectText)
I have not found anything, neither in MSDN nor on the Web

Is there a generic approach how to pass STL objects between both worlds in the mixed mode DLL

Any help / hints would be apreciated

Regards & thanks in advanc
Johannes Unfrie

D.I. Johannes Unfrie
Senior Engineer SpeechMagi
Philips Speech Processing
Speech Recognition System
Triesterstrasse 64, P.O. Box 217, A-1101 Vienna, Austri
Tel. +43 1 60101 280
Fax +43 1 60101 414
mail: jo**************@philips.co
web: www.speechrecognition.philips.co

Nov 17 '05 #1
0 2551

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Ney André de Mello Zunino | last post: by
1 post views Thread by Christian Schmidt | last post: by
41 posts views Thread by =?Utf-8?B?VGltIE1hcnNkZW4=?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.