Good day.

You must pass a pointer to the IntPtr structure lpCmdData to the external library:

[DllImport("xxx.dll", SetLastError = true)] public static extern IntPtr Start(IntPtr hService, uint dwCommand, IntPtr lpCmdData, uint dwTimeOut, ref _wfs_result lppResult); 

The code for the structure itself is as follows:

  [StructLayout(LayoutKind.Explicit)] public struct my_struct { [FieldOffset(0)] public ushort a; [FieldOffset(2)] public ushort b; [FieldOffset(4)] public uint c; [FieldOffset(8)] public bool d; [FieldOffset(9)] public IntPtr e; }; 

Getting a pointer to the structure is done through Marshal.StructureToPtr:

  int iSizeOfStruct = Marshal.SizeOf(typeof(my_struct)); IntPtr ptrDispense = Marshal.AllocHGlobal(iSizeOfStruct); Marshal.StructureToPtr(Struct1, ptrDispense, true); 

I get the pointer when I pass it to the DLL, then I get an error that the pointer is not correct .. After two days I come to the conclusion that the resulting structure address is available only within .NET, therefore it is not a physical memory address, but a logical address. Is it possible to get a real physical memory address that will be available outside the application on .NET? Or can you create a structure outside a protected area?

PS picking the code, I noticed the following, when the DLL returns a memory pointer to me, then it consists of 10 numbers - example 1255246569. When a pointer is received in .NET, it consists of 7 numbers - 1542952

  • one
    The latter is not clear, because 1. memory alignment is standard on the boundaries of 4 bytes, i.e. the address must be a multiple of 4. And 2 AllocHGlobal, equivalent to GlobalAlloc, must give the correct address. - nick_n_a
  • Yes, I also do not understand ... When one of the functions returns a pointer, it is a ten-digit one and I calmly read the structure by the pointer structure. But when I create a pointer to my own structure, it is seven-digit ... Additionally, I write on .NET2.0 under winxp - Alex

1 answer 1

Converting there by your example

 my_struct cmddata = new cmddata(); //public static extern IntPtr Start(IntPtr hService, uint dwCommand, IntPtr lpCmdData, uint dwTimeOut, ref _wfs_result lppResult); Start(hService, dwCommand, Marshal.UnsafeAddrOfPinnedArr‌​ayElement(cmddata,0), Timeout, lppResult); 

Transform back

 IntPtr data = ...; my_struct r = (my_struct)Marshal.PtrToStructure(data, typeof(bootrec)); 
  • Marshal.UnsafeAddrOfPinnedArr‌ ayElement requires the first parameter of type System.Array, which does not correspond to my structure - Alex