472,353 Members | 1,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 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 11538
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...
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...
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...
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++...
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...
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...
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...
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...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

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.