473,395 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

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
0 2658

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: zDog | last post by:
I found a lot of information on passing data from C# to a C++ dll What I cannot find is a way to return C++ structs of TCHAR string data back to the C# managed code! typedef struct // C++ data...
5
by: Ney André de Mello Zunino | last post by:
Hello. I am developing a project in managed C++ for the first time. Things were going fine up until the point I had the need to mix managed and non-managed code. More specifically, what I am...
0
by: Ken Varn | last post by:
I have a managed C++ assembly in which I need to interact with some 'C' APIs that take fixed size 'C' data blocks. I need to wrap these data blocks into a managed object. It seems like a lot of...
1
by: nicewenyan | last post by:
I want to pass a managed c# byte (8 bit) array into a unmanaged c++ function: extern "C" void AddData(unsigned int* data); I use P/Invoke on managed side to do the marshaling as following: ...
1
by: Christian Schmidt | last post by:
Hi all, I need to implement an unmanaged function that gets a SafeArray and hands it over to a managed function having the managed array-type. Using MarshalAs I can call unmanaged functions having...
8
by: Edson Manoel | last post by:
I have some C++ unmanaged code that takes std::string& arguments (as reference), and fills them (possibly growing the string). I want to call this code through PInvoke (DllImport), possibly using...
41
by: =?Utf-8?B?VGltIE1hcnNkZW4=?= | last post by:
Hi, I am after suggestions on the best practice declaring and destroying objects. some example code: Private Sub MySub Dim frmMyForm As MyForm Try
2
by: O.B. | last post by:
I have operation within a class that marshals the data into a byte array. Below are three different ways that work. Are there any downsides to using one over the the other? public virtual byte...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.