Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Converting C structures to C# equivalents...

Converting C structures to C# equivalents...

Scheduled Pinned Locked Moved C#
csharpjsonquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi all, What is the best way to convert C style structures to C#. I have converted the following C structure typedef struct { ISC_DATE timestamp_date; ISC_TIME timestamp_time; } ISC_TIMESTAMP; to public struct ISC_TIMESTAMP { long TIMESTAMP_DATE; ulong TIMESTAMP_TIME; }; Is this correct or should I be using another more C# style like.. [StructLayout(LayoutKind.Sequential)] public class ISC_TIMESTAMP { // rest of code goes here } How about this more complicated C structure... typedef struct blobcallback { short (ISC_FAR *blob_get_segment) (void ISC_FAR* hnd, unsigned char* buffer, ISC_USHORT buf_size, ISC_USHORT* result_len); void ISC_FAR *blob_handle; ISC_LONG blob_number_segments; ISC_LONG blob_max_segment; ISC_LONG blob_total_length; void (ISC_FAR *blob_put_segment) (void ISC_FAR* hnd, unsigned char* buffer, ISC_USHORT buf_size); ISC_LONG (ISC_FAR *blob_lseek) (void ISC_FAR* hnd, ISC_USHORT mode, ISC_LONG offset); } ISC_FAR *BLOBCALLBACK; What is the best way to handle this type of conversion. Sincerely, Dominique.

    J 1 Reply Last reply
    0
    • L Lost User

      Hi all, What is the best way to convert C style structures to C#. I have converted the following C structure typedef struct { ISC_DATE timestamp_date; ISC_TIME timestamp_time; } ISC_TIMESTAMP; to public struct ISC_TIMESTAMP { long TIMESTAMP_DATE; ulong TIMESTAMP_TIME; }; Is this correct or should I be using another more C# style like.. [StructLayout(LayoutKind.Sequential)] public class ISC_TIMESTAMP { // rest of code goes here } How about this more complicated C structure... typedef struct blobcallback { short (ISC_FAR *blob_get_segment) (void ISC_FAR* hnd, unsigned char* buffer, ISC_USHORT buf_size, ISC_USHORT* result_len); void ISC_FAR *blob_handle; ISC_LONG blob_number_segments; ISC_LONG blob_max_segment; ISC_LONG blob_total_length; void (ISC_FAR *blob_put_segment) (void ISC_FAR* hnd, unsigned char* buffer, ISC_USHORT buf_size); ISC_LONG (ISC_FAR *blob_lseek) (void ISC_FAR* hnd, ISC_USHORT mode, ISC_LONG offset); } ISC_FAR *BLOBCALLBACK; What is the best way to handle this type of conversion. Sincerely, Dominique.

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      Dominique wrote:_

      public struct ISC_TIMESTAMP
      {
      long TIMESTAMP_DATE;
      ulong TIMESTAMP_TIME;
      };

      Is this correct or should I be using another more C# style like..

      [StructLayout(LayoutKind.Sequential)]
      public class ISC_TIMESTAMP
      {
      // rest of code goes here
      }

      You need to use a combination of the two, keep it a struct but apply the StructLayout attribute its your only guarantee that the data will stay in that order; otherwise the compiler could move fields around to better align the data. Dominique wrote:

      struct blobcallback {
      short (ISC_FAR *blob_get_segment)
      (void ISC_FAR* hnd, unsigned char* buffer, ISC_USHORT buf_size, ISC_USHORT* result_len);
      void ISC_FAR *blob_handle;
      ISC_LONG blob_number_segments;
      ISC_LONG blob_max_segment;
      ISC_LONG blob_total_length;
      void (ISC_FAR *blob_put_segment)
      (void ISC_FAR* hnd, unsigned char* buffer, ISC_USHORT buf_size);
      ISC_LONG (ISC_FAR *blob_lseek)
      (void ISC_FAR* hnd, ISC_USHORT mode, ISC_LONG offset);
      }

      _Call backs are done with delegates, there is an article somewhere on CP that shows how to use a callback for the EnumWindows function, i think it was how to programmatcally control IE. void is usually a object When you have a character buffer pass in a StringBuilder object A ** (pointer to a pointer) usually uses the 'out' keyword followed by its type, a pointer is usually a reference ('ref'). HTH, James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups