By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,958 Members | 1,156 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,958 IT Pros & Developers. It's quick & easy.

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

P: n/a
Problem
Best practice needed to marshal STL data from managed code to unmanaged code & vice vers

Details
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

public
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

cUnman1.SetStdString(stdstrText)
Marshal::FreeHGlobal(intPtr)

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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.