473,574 Members | 3,176 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(st d::string & strText)
int GetStdVector(st d::vector<std:: string>& vectText)

int SetStdString(co nst std::string & strText_p)
int SetStdVector(co nst 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::String ToHGlobalAnsi(p StrTmp);
char *pcTmp = static_cast<cha r*>(intPtr.ToPo inter());

stdstrText = pcTmp

cUnman1.SetStdS tring(stdstrTex t)
Marshal::FreeHG lobal(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(st d::vector<std:: string>& vectText)
- int GetStdString(st d::string & strText)
- int SetStdVector(co nst 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 2684

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

Similar topics

2
11628
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 that needs to be returned to the caller (C#) { short snOperParams; TCHAR szNFPath; TCHAR szSSFile; TCHAR szExeFile;
5
1576
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 trying to have is a MVC-like scheme, where the view is implemented using Windows Forms and the model is composed of a bunch of previously-written...
0
1515
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 redundancy and extra code to have to marshal .net data from managed to unmanaged for this. Is there some built-in method in managed C++ that can...
1
6301
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: public static extern int AddData(IntPtr data);
1
2824
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 SafeArray from managed code. But here I need this vice-versa... I assume it is doable with MarshalAs magic, but how? Thanks Christian
8
13734
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 wrapper layers in unmanaged C++ and C#. I've thought about two approaches: 1) To pass a StringBuilder, this is converted to a char* in C++,...
41
1865
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
15920
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 ToRaw1() { byte byteArray = new byte; IntPtr pointer = Marshal.AllocHGlobal(Size); Marshal.StructureToPtr(this, pointer, false);...
0
7764
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8273
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7862
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5336
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3775
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3794
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1375
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1102
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.