1

Hi I am trying to convert the C/C++ Strcut to C# and how to fill the structure member with address of another structure in C#?

C/C++ Struct looks like:

         typedef struct _NDISUIO_QUERY_OID
         {
           NDIS_OID        Oid;
           PTCHAR          ptcDeviceName;  
           UCHAR           Data[sizeof(ULONG)];
         } NDISUIO_QUERY_OID, *PNDISUIO_QUERY_OID;

         typedef struct My_Struct
         {
           //leT's have 2 variables...  
            UINT    a;
            UINT    b;
           //sTRUCT may have many no.of variables depending upon the requirement
         }My_STATS, *PMy_STATS;

         PNDISUIO_QUERY_OID     pQueryOid = NULL;

          pQueryOid = (PNDISUIO_QUERY_OID)malloc(sizeof(NDISUIO_QUERY_OID)+ sizeof(My_STATS)) ;

          PMy_STATS   Statistics;
          pQueryOid->Oid = ulOIDCode;//Required OID
      pQueryOid->ptcDeviceName = AUB_NAME;//REquired STRING

          memcpy(pQueryOid->Data, Statistics, sizeof(My_STATS));

          IoctlResult = DeviceIoControl(
                                       handle,
                                       IOCTL_NDISUIO_QUERY_OID_VALUE,
                                       pQueryOid,
                                       sizeof(NDISUIO_QUERY_OID)+ sizeof(My_STATS),
                                       pQueryOid,
                                       sizeof(NDISUIO_QUERY_OID)+ sizeof(My_STATS),
                                       &dwReturnedBytes,
                                       NULL);

Getting IoctlResult Success in C++;

My C# Struct is:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]           
    public struct _NDISUIO_QUERY_OID
    {
        public uint        Oid;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string          ptcDeviceName;
        [MarshalAs(UnmanagedType.ByValArray,SizeConst = sizeof(uint))]
        public byte[] Data;
    };

     My_STATS Sta_Conn_Info = new My_STATS();
     _NDISUIO_QUERY_OID QueryOid = new _NDISUIO_QUERY_OID();

    QueryOid.Oid = ulOIDCode; // required OID
    QueryOid.ptcDeviceName = STRING;//Required String

    //Imported coredll.dll with required prototype for this.
    IoctlResult = DeviceIoControl(
                                       handle,
                                       IOCTL_NDISUIO_QUERY_OID_VALUE,
                                       ref QueryOid,
                                       sizeof(NDISUIO_QUERY_OID)+ sizeof(My_STATS),
                                       ref QueryOid,
                                       sizeof(NDISUIO_QUERY_OID)+ sizeof(My_STATS),
                                       ref dwReturnedBytes,
                                       NULL
                                  );

Problem: How to marshal the NDISUIO_QUERY Struct to copy the another structure to its Data Member in C#?? and how to replace the memcpy as in c++?

Any suggestions or guidelines will be helpful.. :)

Thanks :)

arya2arya
  • 291
  • 2
  • 21
  • possible duplicate of [Conversion of C/C++ Struct To C#.Net CF WinCE](http://stackoverflow.com/questions/22560958/conversion-of-c-c-struct-to-c-net-cf-wince) – Hans Passant Mar 26 '14 at 14:49
  • @HansPassant hi sir.. structure conversion has no problems here i need to perform the task as in C# as working in C++ I added the code in C++. – arya2arya Mar 26 '14 at 14:50
  • @HansPassant In C++, I am copying one structure into Data member of another structure using memcpy. The same task I am trying in C# – arya2arya Mar 26 '14 at 14:53
  • As I told you in the last question that you cannot declare the `NDISUIO_QUERY_OID` as a C# struct. You have to use `Marshal.AllocHGlobal`. – David Heffernan Mar 26 '14 at 15:01
  • @DavidHeffernan Sir.. I tried by converting structure to pointer, and allocated memory using `Marshal.AllocHGlobal` and not able get the result. – arya2arya Mar 26 '14 at 15:06
  • @arya2arya Not according to the question. You keep asking the same question over and over again. I cannot see any code to indicate what you tried. – David Heffernan Mar 26 '14 at 15:07
  • @DavidHeffernan Sir Please check your Chat room I posted my code that I've Implemented. – arya2arya Mar 26 '14 at 15:16

1 Answers1

1

The Data member is not what it seems. It's a place holder and the struct is actually variable length. I think I'd be inclined to use Marshal.AllocHGlobal and marshal manually. But you seem to prefer declaring a C# struct. So long as the struct that you use is always the same one then you would declare it like this:

[StructLayout(LayoutKind.Sequential)]           
public struct NDISUIO_QUERY_OID
{
    public uint Oid;
    [MarshalAs(UnmanagedType.LPWStr)]
    public string ptcDeviceName;
    uint a;
    uint b;
};

This does not quite match the C++ code that you present because that declares the struct to have size sizeof(NDISUIO_QUERY_OID)+ sizeof(My_STATS) but then copies the My_STATS struct over Data rather than after Data.

On a more general note, you've now asked essentially question seven times. I think that it might be time for you to step back and try to understand the memory layout better than you currently do. Each time you ask the question you don't state clearly what layout you want to use for your version of the NDISUIO_QUERY_OID struct. It's high time that you got that clear in your mind.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thanks Sir!! I Don't have in-depth knowledge in .Net CF, I started my task by converting C++ to C# by looking some tutorials, able to do some basic things. this is the final thing. I have to make the structure properly and then have to send it to DeviceIoControl() – arya2arya Mar 26 '14 at 15:31