Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6384927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:50:08+00:00 2026-05-25T02:50:08+00:00

I’ve got structure : type OneDevice = { mutable id : System.UInt16 mutable typeDev

  • 0

I’ve got structure :

type OneDevice = {
        mutable id              : System.UInt16
        mutable typeDev         : byte
        mutable portNum         : byte
        mutable Parity          : byte
        mutable StopBits        : byte
        mutable BaudRate        : byte
        mutable addr1           : byte 
        mutable addr2           : byte 
        mutable useCanal        : byte
        mutable idGroup1        : byte
        mutable idGroup2        : byte 
        mutable idGroup3        : byte
        mutable idGroup4        : byte
        mutable idGroupSos1     : byte 
        mutable idGroupSos2     : byte 
        mutable idGroupSos3     : byte 
        mutable idGroupSos4     : byte 
        mutable idSosReserv     : byte 
        mutable addrModbus      : byte 
        mutable offsetModbus    : System.UInt16
        mutable pwd             : byte array
        mutable offsetInModbus  : System.UInt16
        mutable reserv          : System.UInt16
    }

And I need to copy some use it as byte array. In C# I can declare the size of byte array here, but for now I don’t know the size of pwd.

I’m trying to use :

let memcp(device : OneDevice, bytes : byte array) =
    Array.zeroCreate <| Marshal.SizeOf(typeof<OneDevice>)
        |> fun (array : byte array) ->
        GCHandle.Alloc(array, GCHandleType.Pinned) |> fun handle ->
            Marshal.StructureToPtr(device, handle.AddrOfPinnedObject(), true)
            handle.Free()

But got error message :

Error Unable to package type “Model + OneDevice” as an unmanaged
structure; impossible to calculate the size or offset that make sense.

I think that is because I don’t know the pwd size here. So how can I use it on F# Structure then ? Or maybe I can declare static-size array type somehow ?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:50:09+00:00Added an answer on May 25, 2026 at 2:50 am

    You need to ensure that the structure you’re marshaling has the same layout as the native structure using the StructLayout attribute, eg.

    [<type: StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)>]
    type OneDevice = { 
        ...
    

    In addition, you need to explicitly mark any fields with the MarshalAs attribute if they require marshaling with non-default marshaling behavior, such as arrays. The default marshaling behavior for arrays is LPArray, but by the sounds of it, your native structure is expecting a ByValArray.

    [<field: MarshalAs(UnmanagedType.ByValArray, SizeConst = 17)>]
    mutable pwd             : byte array
    

    Lastly, replace GCHandle.Alloc with Marshal.AllocHGlobal for allocating unmanaged memory, and use Marshal.FreeHGlobal to free it.

    Note: I’m uncertain whether F# record types can accept these attributes, but I would expect them to work. If not, then you would need to use them in combination with jpalmer’s suggestion of using a struct.

    Edit:

    let size = Marshal.SizeOf(typeof<OneDevice>)
    let unmanagedPtr = Marshal.AllocHGlobal(size)
    Marshal.StructureToPtr(device, unmanagedPtr, false)
    Marshal.Copy(unmanagedPtr, bytes, 0, size)
    Marshal.FreeHGlobal(unmanagedPtr)
    

    Edit:

    The above is for copying a populated OneDevice structure into an empty byte array. If you want to do the reverse – converting a populated byte array into a structure, it’s largely the same thing.

    let size = Marshal.SizeOf(typeof<OneDevice>)
    let unmanagedPtr = Marshal.AllocHGlobal(size)
    Marshal.Copy(bytes, unmanagedPtr, 0, size)
    Marshal.PtrToStructure(unmanagedPtr, device)
    Marshal.FreeHGlobal(unmanagedPtr)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.