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

Marshalling a char array in a structure from C to .NET

When marshalling a char array in a structure from C to .NET, I get the
following error:
System.ArgumentException: Type could not be marshaled because the
length of an embedded array instance does not match the declared
length in the layout.

namespace RemoteClientNew
{
[StructLayout(LayoutKind.Sequential)]
public struct contacts
{
[MarshalAs UnmanagedType.ByValArray,
ArraySubType=UnmanagedType.AnsiBStr, SizeConst=10)] public byte[]
name;
public int age;
public int id;
}
}
The name field in C code also has a size of 10.
Trying using UnmanagedType as LPArray or ByValTStr with CharSet.Ansi
gives the following error:

System.TypeLoadException: Can not marshal field name of type
RemoteClientNew.con
tacts: This type can not be marshaled as a structure field.
Nov 16 '05 #1
1 6073
Hi Betty,

One thing that I notice is that your ArraySubType is AnsiBstr which is
incorrect. It should be :
UnmanagedType.U1

HTH,
Tom T.

"Betty" wrote:
When marshalling a char array in a structure from C to .NET, I get the
following error:
System.ArgumentException: Type could not be marshaled because the
length of an embedded array instance does not match the declared
length in the layout.

namespace RemoteClientNew
{
[StructLayout(LayoutKind.Sequential)]
public struct contacts
{
[MarshalAs UnmanagedType.ByValArray,
ArraySubType=UnmanagedType.AnsiBStr, SizeConst=10)] public byte[]
name;
public int age;
public int id;
}
}
The name field in C code also has a size of 10.
Trying using UnmanagedType as LPArray or ByValTStr with CharSet.Ansi
gives the following error:

System.TypeLoadException: Can not marshal field name of type
RemoteClientNew.con
tacts: This type can not be marshaled as a structure field.

Nov 16 '05 #2

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

Similar topics

7
by: Mick | last post by:
Does anyone know how to pass an array of structures to a DLL? Here's what I'm doing... the VB.Net error is at the end. *** C++ Structure Declaration: typedef struct _SOME_STRUCT { char...
4
by: Animesh | last post by:
Hi All, I don't know whethher this is possible or not. This is the result of a bad design problem. Here I go; I have a structure like this: typedef struct _s_index_entry { char *doc_id;...
1
by: Reynardine | last post by:
I am calling a C/C++ DLL from C# and I am marshalling the parameters to the API call by doing a type conversion for each parameter. For example, here is my C++ API method : short int XENO_API...
1
by: Me | last post by:
I'm trying to get a structure into a byte array. I can't seem to figure out how to get a non-fixed length null-terminated string into the array (without rolling my own logic). For example, a...
2
by: RJ Lohan | last post by:
Howdy, I have a legacy DLL for which I have a problem marshalling a parameter type of char**. The function header (C++) is as so; extern "C" __declspec(dllexport) int __stdcall...
1
by: peary | last post by:
Hi, everyone, I'm writing a program to discover wireless network using Windows Native Wifi API & VB.net. I have to declare the windows API in my VB.net program. The original windows...
11
by: Daniel Bass | last post by:
Greetings! I'm trying to call this method in a c# app... SNAPIDLL_API int __stdcall SNAPI_SetCapabilitiesBuffer(HANDLE DeviceHandle, unsigned char *pData, long max_length); So far I've got...
2
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I...
1
by: Zoser | last post by:
When marshalling structure from C to .NET, I get the following error: System.ArgumentException: Type could not be marshaled because the length of an embedded array instance does not match the...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.