[C#] PtrToStructure problem

看板C_Sharp (C#)作者時間20年前 (2005/03/11 20:01), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
Hi First of all please let me know if there is some other, more suitable group for this news - TIA :-) Now, my problem. I'm writing an C# application in which I'm using some functions from DLL. This dll has been written in C. One of these functions returns as a result a structure, in example: [StructLayout(LayoutKind.Sequential, Pack=2)] internal class myStruct { public short someNumber; public IntPtr handle; } OK. Now in this handle there is a pointer to a structure with actuall results. [StructLayout(LayoutKind.Sequential, Pack=2)] internal class myStruct2 { public uint count; //number of values return from function public uint current; //current value [MarshalAs(UnmanagedType.ByValArray, SizeConst=1)] public short[] items; //array of results } OK. Hope you're still with me :D Now, as you can see there is another array in myStruct2. This array contains result which I want to get :D In this function description I have something like this: <description> The items value is simply a placeholder for the start of the actual array, which must be allocated when the container is allocated. </description> In my application I have something like this: <code> [DllImport("some.dll", EntryPoint="#1")] private static extern short getValues([In, Out] myStruct cap); myStruct ms = new myStruct(); getValues(ms); myStruct2 ms2 = new myStruct2(); ms2 = (myStruct2)Marshal.PtrToStructure(ms.handle, typeof(myStruct2)); </code> In return I get some really strange values in ms2.count and ms2.current. Not mentioning that I can't get the values from the items :-( What am I doing wrong? Meybe there is something missing? best regards Mateusz [PEYN] Adamus
文章代碼(AID): #12COY300 (C_Sharp)
文章代碼(AID): #12COY300 (C_Sharp)