473,394 Members | 1,946 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,394 software developers and data experts.

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 11617
zDog,

You would want to do this:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct <StructureName>
{
public short snOperParams;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=PATH_SIZE)]
public string szNFPath;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=PATH_SIZE)]
public string szSSFile;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=PATH_SIZE)]
public string szExeFile;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=PATH_SIZE)]
public string szOpLibIFile;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=PATH_SIZE)]
public string szOpLibMFile;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=PATH_SIZE)]
public string szDefaultIFile;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=PATH_SIZE)]
public string szDefaultMFile;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=PATH_SIZE)]
public string szMiscFile;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=PATH_SIZE)]
public string szAuxFile;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=PATH_SIZE)]
public string szSuffix;
}

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

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"zDog" <zD**@discussions.microsoft.com> wrote in message
news:E3**********************************@microsof t.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(dllexport) bool get_file_paths (files_path_struct
&file_paths)
{

// 'files_path_struct' 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_paths.szNFPath, the_file_paths.szNFPath, 260);
strncpy(file_paths.szSSPath, the_file_paths.szSSPath, 260);

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

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public class file_paths
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)]
public string szNFPath;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)]
public string szSSFile;
} fpaths;
[DllImport("x.dll")]
public static extern bool get_file_paths(
[In, Out, MarshalAs(UnmanagedType.LPStruct)]
file_paths test_file_paths
);

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

You would want to do this:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct <StructureName>
{
public short snOperParams;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=PATH_SIZE)]
public string szNFPath;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=PATH_SIZE)]
public string szSSFile;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=PATH_SIZE)]
public string szExeFile;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=PATH_SIZE)]
public string szOpLibIFile;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=PATH_SIZE)]
public string szOpLibMFile;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=PATH_SIZE)]
public string szDefaultIFile;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=PATH_SIZE)]
public string szDefaultMFile;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=PATH_SIZE)]
public string szMiscFile;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=PATH_SIZE)]
public string szAuxFile;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=PATH_SIZE)]
public string szSuffix;
}

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

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"zDog" <zD**@discussions.microsoft.com> wrote in message
news:E3**********************************@microsof t.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
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
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...
6
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...
0
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...
0
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...
7
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...
3
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 ). ...
0
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 ...
1
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
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.