c# Marshalling unmanaged type char[xx]
-
So here is my problem : i have a structure used as parameter (struct * )in a dll (VC++ 6.00): typedef struct { CHAR chararray1[10]; DWORD Data1; DWORD Data2; CHAR chararray2[4]; CHAR chararray3[2]; BYTE Data3; BYTE Data4; BYTE Data5; BYTE Data6; BYTE Data7; BYTE Data8; } mystruct; It should be converted like this - i think ;-)) - [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi)] unsafe public struct mystruct { [MarshalAs(UnmanagedType.ByValTStr,SizeConst=10)] public string chararray1; public Int32 Data1; public Int32 Data2; [MarshalAs(UnmanagedType.ByValTStr,SizeConst=4)] public string chararray2; [MarshalAs(UnmanagedType.ByValTStr,SizeConst=2)] public string chararray3; public byte Data4; public byte Data5; ... } Expected Value for string fields are "ABC : 0.00", "1234", "56" When executing with size defined identically in both structures, i get this strange result "ABC :0.0","123", "5" if i increase(+1) the size in C# structure , hoping to get my missing char, i get "ABC : 0.00", "1234", "6" So am i doing a mistake somewhere ? or is it a bug ?????? Thanks by Advance Gaetan
-
So here is my problem : i have a structure used as parameter (struct * )in a dll (VC++ 6.00): typedef struct { CHAR chararray1[10]; DWORD Data1; DWORD Data2; CHAR chararray2[4]; CHAR chararray3[2]; BYTE Data3; BYTE Data4; BYTE Data5; BYTE Data6; BYTE Data7; BYTE Data8; } mystruct; It should be converted like this - i think ;-)) - [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi)] unsafe public struct mystruct { [MarshalAs(UnmanagedType.ByValTStr,SizeConst=10)] public string chararray1; public Int32 Data1; public Int32 Data2; [MarshalAs(UnmanagedType.ByValTStr,SizeConst=4)] public string chararray2; [MarshalAs(UnmanagedType.ByValTStr,SizeConst=2)] public string chararray3; public byte Data4; public byte Data5; ... } Expected Value for string fields are "ABC : 0.00", "1234", "56" When executing with size defined identically in both structures, i get this strange result "ABC :0.0","123", "5" if i increase(+1) the size in C# structure , hoping to get my missing char, i get "ABC : 0.00", "1234", "6" So am i doing a mistake somewhere ? or is it a bug ?????? Thanks by Advance Gaetan
I have the same problem. I have one char[256] unmanaged parameter and I'm only getting the first character each time. What does your function prototype look like and how are you passing your struct? Please post a solution if you find one and I will do the same. Tym!
-
I have the same problem. I have one char[256] unmanaged parameter and I'm only getting the first character each time. What does your function prototype look like and how are you passing your struct? Please post a solution if you find one and I will do the same. Tym!
Fucntion declared in the dll like this INT Function(DWORD param1, mystruct *psConfig); import with c# [DllImport(@"C:\DLL\MyDll.dll",EntryPoint="Function")] unsafe public static extern int MyFunction(uint param1,ref mystruct psConfig); Calling function : private void button10_Click(object sender, System.EventArgs e) { mystruct sConfig = new mystruct(); int res; unsafe { res = MyFunction(hCom,ref sConfig); } MessageBox.Show( res.ToString() ); }
-
So here is my problem : i have a structure used as parameter (struct * )in a dll (VC++ 6.00): typedef struct { CHAR chararray1[10]; DWORD Data1; DWORD Data2; CHAR chararray2[4]; CHAR chararray3[2]; BYTE Data3; BYTE Data4; BYTE Data5; BYTE Data6; BYTE Data7; BYTE Data8; } mystruct; It should be converted like this - i think ;-)) - [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi)] unsafe public struct mystruct { [MarshalAs(UnmanagedType.ByValTStr,SizeConst=10)] public string chararray1; public Int32 Data1; public Int32 Data2; [MarshalAs(UnmanagedType.ByValTStr,SizeConst=4)] public string chararray2; [MarshalAs(UnmanagedType.ByValTStr,SizeConst=2)] public string chararray3; public byte Data4; public byte Data5; ... } Expected Value for string fields are "ABC : 0.00", "1234", "56" When executing with size defined identically in both structures, i get this strange result "ABC :0.0","123", "5" if i increase(+1) the size in C# structure , hoping to get my missing char, i get "ABC : 0.00", "1234", "6" So am i doing a mistake somewhere ? or is it a bug ?????? Thanks by Advance Gaetan
gmar wrote: So am i doing a mistake somewhere ? or is it a bug ?????? The structs mapping is incorrect! Where's Data3? You should alos set the imported functions CharSet, not the struct. Also all those unsafe sections are unnecessary. They are only need when using pointers, whicj you are obviously not. Lastly a string should be null terminated, that could be causing printing problems... leppie::AllocCPArticle("Zee blog");
Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it. -
gmar wrote: So am i doing a mistake somewhere ? or is it a bug ?????? The structs mapping is incorrect! Where's Data3? You should alos set the imported functions CharSet, not the struct. Also all those unsafe sections are unnecessary. They are only need when using pointers, whicj you are obviously not. Lastly a string should be null terminated, that could be causing printing problems... leppie::AllocCPArticle("Zee blog");
Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.hello, i already tried to set CharSet=CharSet.Ansi, but it didn't change anything. Unsafe or not shouldn't be a problem, but i will remove it just to try. and as you have seen i'm not talking about a problem on Data3, my problem is to get back the content of each char array coming from my dll. so i will try to get it back as an array of byte , if it's possible. So if you have a real idea about how to solve this problem, you are welcome.
-
I have the same problem. I have one char[256] unmanaged parameter and I'm only getting the first character each time. What does your function prototype look like and how are you passing your struct? Please post a solution if you find one and I will do the same. Tym!