473,573 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

marshal string data from unmanaged to managed

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[PATH_SIZE];
TCHAR szSSFile[PATH_SIZE];
TCHAR szExeFile[PATH_SIZE];
TCHAR szOpLibIFile[PATH_SIZE];
TCHAR szOpLibMFile[PATH_SIZE];
TCHAR szDefaultIFile[PATH_SIZE];
TCHAR szDefaultMFile[PATH_SIZE];
TCHAR szMiscFile[PATH_SIZE];
TCHAR szAuxFile[PATH_SIZE];
TCHAR szSuffix[PATH_SIZE];
} files;
Nov 16 '05 #1
2 11627
zDog,

You would want to do this:

[StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Auto)]
public struct <StructureNam e>
{
public short snOperParams;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=PATH_ SIZE)]
public string szNFPath;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=PATH_ SIZE)]
public string szSSFile;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=PATH_ SIZE)]
public string szExeFile;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=PATH_ SIZE)]
public string szOpLibIFile;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=PATH_ SIZE)]
public string szOpLibMFile;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=PATH_ SIZE)]
public string szDefaultIFile;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=PATH_ SIZE)]
public string szDefaultMFile;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=PATH_ SIZE)]
public string szMiscFile;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=PATH_ SIZE)]
public string szAuxFile;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=PATH_ SIZE)]
public string szSuffix;
}

Just make sure that you define <StructureNam e> and define a constant for
PATH_SIZE.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"zDog" <zD**@discussio ns.microsoft.co m> wrote in message
news:E3******** *************** ***********@mic rosoft.com...
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[PATH_SIZE];
TCHAR szSSFile[PATH_SIZE];
TCHAR szExeFile[PATH_SIZE];
TCHAR szOpLibIFile[PATH_SIZE];
TCHAR szOpLibMFile[PATH_SIZE];
TCHAR szDefaultIFile[PATH_SIZE];
TCHAR szDefaultMFile[PATH_SIZE];
TCHAR szMiscFile[PATH_SIZE];
TCHAR szAuxFile[PATH_SIZE];
TCHAR szSuffix[PATH_SIZE];
} files;

Nov 16 '05 #2
Nicholas,

Thanks for the response!
That is what I was trying without success.

If I initialize the strings, that data gets over to the unmanaged code side
OK.
But the caller gets nothing but garbage back! ;(
(I really do not need to pass anything to the callee, I just need to
retrieve loads of data back to the caller)
Sample -->

/// *** C++ code -->>

//-----------------------------------------------------------------------------------
// Retrieve the File Path Settings (from unmanaged DLL
//-----------------------------------------------------------------------------------
extern "C" __declspec(dlle xport) bool get_file_paths (files_path_str uct
&file_paths)
{

// 'files_path_str uct' is the matching (local) C++ struct of TCHAR
<name>[260]
// Need to copy those strings into the (file_paths) struct/class passed in
from C#

strncpy(file_pa ths.szNFPath, the_file_paths. szNFPath, 260);
strncpy(file_pa ths.szSSPath, the_file_paths. szSSPath, 260);

return true;
}
/// *** C# code -->>

[StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Auto)]
public class file_paths
{
[MarshalAs(Unman agedType.ByValT Str, SizeConst=260)]
public string szNFPath;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=260)]
public string szSSFile;
} fpaths;
[DllImport("x.dl l")]
public static extern bool get_file_paths(
[In, Out, MarshalAs(Unman agedType.LPStru ct)]
file_paths test_file_paths
);

------
zDog
------
"Nicholas Paldino [.NET/C# MVP]" wrote:
zDog,

You would want to do this:

[StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Auto)]
public struct <StructureNam e>
{
public short snOperParams;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=PATH_ SIZE)]
public string szNFPath;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=PATH_ SIZE)]
public string szSSFile;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=PATH_ SIZE)]
public string szExeFile;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=PATH_ SIZE)]
public string szOpLibIFile;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=PATH_ SIZE)]
public string szOpLibMFile;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=PATH_ SIZE)]
public string szDefaultIFile;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=PATH_ SIZE)]
public string szDefaultMFile;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=PATH_ SIZE)]
public string szMiscFile;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=PATH_ SIZE)]
public string szAuxFile;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=PATH_ SIZE)]
public string szSuffix;
}

Just make sure that you define <StructureNam e> and define a constant for
PATH_SIZE.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"zDog" <zD**@discussio ns.microsoft.co m> wrote in message
news:E3******** *************** ***********@mic rosoft.com...
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[PATH_SIZE];
TCHAR szSSFile[PATH_SIZE];
TCHAR szExeFile[PATH_SIZE];
TCHAR szOpLibIFile[PATH_SIZE];
TCHAR szOpLibMFile[PATH_SIZE];
TCHAR szDefaultIFile[PATH_SIZE];
TCHAR szDefaultMFile[PATH_SIZE];
TCHAR szMiscFile[PATH_SIZE];
TCHAR szAuxFile[PATH_SIZE];
TCHAR szSuffix[PATH_SIZE];
} files;


Nov 16 '05 #3

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

Similar topics

5
1485
by: wenmang | last post by:
Hi, I am wondering whether string class's member function data() returns a string of chars without '\0'? Thanks.
10
49571
by: joel.brewster | last post by:
We have a VB6 application using ADO version 2.5 and I am receiving a " CLI0109E String data right truncation. SQLSTATE=22001" error when I execute the rs.UpdateBatch method. I have determined that the error is caused by an extended ascii character 164 (ñ). Has anyone else encountered this problem and found a solution. TIA Joel...
6
5749
by: John Aldrin | last post by:
Hi, I'm looking for info that explains the format of a string data type when written to a stream using a BinaryWriter. I've looked all over MSDN and Internet and I cannot seem to find it. I did some simple testing and it seems that the string data is prefixed w/a variable number of bytes that indicate the length. 1 byte if length <=...
0
3893
by: Frank Lopez | last post by:
Does anyone know if Microsoft generated a whitepaper on this topic? Does anyone know what the solution is? (meaning, eliminate the leak problem -- I am seeing three memory leaks from dllmodul.cpp(102) similar to what is mentioned below)... I am calling MFC as part of unmanaged code used by the managed code. +--------
0
2682
by: Johannes Unfried | last post by:
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...
7
1727
by: Matik | last post by:
Hi to everyone, My problem is, that I'm not so quite sure, which way should I go. The user is inputing by second part application a long string (let's say 128 characters), which are separated by semiclon. Example: A20;BU;AC40;MA50;E;E;IC;GREEN
3
3247
by: psbasha | last post by:
Hi , When ever we read any data from file ,we read as a single line string ,and we convert the respective field data available in that string based on the data type ( say int,float ). Please suggest me which is the best way of handling the file data. I- Method: ---------------- Store as single line string data's(upto end of file...
0
1051
by: tskmjk | last post by:
Hi all, I am developing an application which reads an excel file which has the following records and inserts into a table in SQL SERVER 2005 database . Data: Product Version CustType ------------- ------------- -------------- Norton SystemWorks Basic Edition 2009 ...
1
2013
by: star01daisy | last post by:
This is what i have, but when i run the program the only thing that comes out is the inro line "This program opens...". What do i have to do for it to run smoothly.The program is supposed to sort the data either in ascending or descending order depending on the user’s choice. Finally, the sorted data is stored in a data file as well as shown on...
0
7784
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8031
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8204
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
7795
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
8073
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5600
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5294
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...
1
2223
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
0
1044
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.