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. Trouble with marshalling structure

Trouble with marshalling structure

Scheduled Pinned Locked Moved C#
helpcsharp
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.
  • J Offline
    J Offline
    Josh Lindenmuth
    wrote on last edited by
    #1

    Hello, We are trying to marshal a simple C structure within our C# code. All values are coming across except for doubles (bolded below). A double comes across as junk. Any help you can provide would be appreciated. Here is our C structure: struct other_earn { short o_id; char o_desc[12]; char o_tpind[2]; char o_payee[31]; char o_addr1[31]; char o_addr2[31]; char o_addr3[31]; char o_hours[2]; **double o_amt1**; short o_dept; char o_txficas[2]; char o_txficam[2]; char o_txfit[2]; char o_txfuta[2]; char o_txsit[2]; char o_txsuta[2]; char o_txloc[2]; short o_mdt; char o_wcexempt[2]; } And here is how we marshal the struct. Note that all fields except the double are marshalled without issue. We have the same problem with all our other structs: [StructLayout(LayoutKind.Sequential)] public struct ROtherEarn { public Int16 o_id; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 12)] public String o_desc; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public String o_tpind; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 31)] public String o_payee; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 31)] public String o_addr1; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 31)] public String o_addr2; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 31)] public String o_addr3; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public String o_hours; **public Double o_amt1;** public Int16 o_dept; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public String o_txficas; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public String o_txficam; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public String o_txfit; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public String o_txfuta; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public String o_txsit; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public String o_txsuta; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public String o_txloc; public Int16 o_mdt; [MarshalAs(Unmanage

    B 1 Reply Last reply
    0
    • J Josh Lindenmuth

      Hello, We are trying to marshal a simple C structure within our C# code. All values are coming across except for doubles (bolded below). A double comes across as junk. Any help you can provide would be appreciated. Here is our C structure: struct other_earn { short o_id; char o_desc[12]; char o_tpind[2]; char o_payee[31]; char o_addr1[31]; char o_addr2[31]; char o_addr3[31]; char o_hours[2]; **double o_amt1**; short o_dept; char o_txficas[2]; char o_txficam[2]; char o_txfit[2]; char o_txfuta[2]; char o_txsit[2]; char o_txsuta[2]; char o_txloc[2]; short o_mdt; char o_wcexempt[2]; } And here is how we marshal the struct. Note that all fields except the double are marshalled without issue. We have the same problem with all our other structs: [StructLayout(LayoutKind.Sequential)] public struct ROtherEarn { public Int16 o_id; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 12)] public String o_desc; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public String o_tpind; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 31)] public String o_payee; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 31)] public String o_addr1; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 31)] public String o_addr2; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 31)] public String o_addr3; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public String o_hours; **public Double o_amt1;** public Int16 o_dept; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public String o_txficas; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public String o_txficam; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public String o_txfit; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public String o_txfuta; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public String o_txsit; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public String o_txsuta; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public String o_txloc; public Int16 o_mdt; [MarshalAs(Unmanage

      B Offline
      B Offline
      Bojan Rajkovic
      wrote on last edited by
      #2

      What do you mean it's not being marshaled correctly? As in it won't compile? As in when you try to use the structure it behaves unexpectedly? Did you try using the MasrhalAs(UnmanagedType.R4) or MarshalAs(UnmanagedType.R8) properties? As I recall, a C double is smaller than a C# double or something to that effect. (The reason I think this is because for some reason I think that a C long is actually 4 bytes while a C# long is 8 bytes) So try adding a MarshalAs(UnmanagedType.R4) to your Double. (R4 and R8 marshal floating-point numbers, I2, I4, and I8 marshal integers)

      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