So here is the method to get data correctly [MarshalAs(UnmanagedType.ByValArray,SizeConst=10)] public byte [] mystring1; instead of [MarshalAs(UnmanagedType.ByValStr,SizeConst=10)] public string mystring1; i guess this will be ok in your application
gmar
Posts
-
c# Marshalling unmanaged type char[xx] -
c# Marshalling unmanaged type char[xx]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.
-
c# Marshalling unmanaged type char[xx]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() ); }
-
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
-
DLL import and parameters;)Hello, i'm using a dll with VC++ 6.0 i would like to use it with C#. My problem is that some type of data are not usable with c#. For exemple i can use char *xx as parameter , but when i want to use a pointer on a structure, it seems impossible to have it working. MyStruct { string field1; int field2; } [DllImport(@"C:\DLL\MyDll.dll",EntryPoint="myfunction",CharSet=CharSet.Ansi)] unsafe public static extern int myfunction(uint param1,short short param2, MyStruct *pointeronStruct); It is not working, [DllImport(@"C:\DLL\MyDll.dll",EntryPoint="ReadCard",CharSet=CharSet.Ansi)] unsafe public static extern int SecondFunction(uint param1,short param2, byte *field1, int *field2); so i need to split the structure, when there is only 2 fields, it's not a problem, but when you have 10 fields, it's not the same. As i cannot modify the dll, i decided to include this dll in a dll where is solve thoses kind of problem and then use the new dll with .NET. Does someone know how to solve this ? Thanks by advance