• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

P/Invoke marshalling

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    P/Invoke marshalling

    Bit long and rambly but here goes:-

    I have a number of API's (about 300), they tend to use structures pretty much like this.

    <StructLayout(LayoutKind.Sequential)> _
    Friend Structure SomeReturnedData
    Friend f1 As SomeField1
    Friend f2 As SomeField2
    Friend f3 As SomeField3
    .....
    .....
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Friend Structure SomeField1
    Friend day As byte
    Friend month As byte
    Friend year As byte
    End Structure

    Now some field is the representation of a date at the other end (and there are a couple of hundred different types like this). Marshalling SomeReturnedData is not a problem, off it goes as a byte array and back it comes.

    My problem arises when I want to do this:-

    <StructLayout(LayoutKind.Sequential)> _
    Friend Structure SomeField1
    Friend day As byte
    Friend month As byte
    Friend year As byte

    'Now some internal stuff which I don't want marshalled say:
    Friend dt as DateTime

    End Structure

    The problem now of course is that the length changes from that expected. There is no <MarshalAs(IgnoreThisPlease)> type attribute.

    Obviously I have to ensure that I ensure the integrity of the internal data structures after the structure has been marshalled - a nuisance but easily enough achievable (though not automatically of course since the constructor isn't run or anything).

    Equally I can write wrappers for all the damn things, but what I want to do is encapsulate both the external and CLR representations in the structure.

    Any suggestions as to how I can do that without resorting to a custom marshaller?

    #2
    Originally posted by ASB
    Any suggestions as to how I can do that without resorting to a custom marshaller?
    None that spring to mind, sorry!!! Would be curious to see if anyone submits an answer tho, just out of professional curiosity!!!!

    Comment


      #3
      Have you tried this free tool http://www.paulyao.com/resources/tools/pinvoke.asp

      Anyway, you want Explicit not Sequential.

      http://spellcoder.com/blogs/bashmoha...2/21/4654.aspx

      I Think this will help.

      Comment


        #4
        DP,

        The problem with the tools is I don't actually have any .H etc for the structure definitions. Only Cobol ones so I can't even avoid all the bleedin typing.

        I want to avoid explicit and actually do this by applying pack:=1 to the structlayoutattribute. This ensures that the members are not reordered and contiguous.

        But it does sound as though I am a bit stuffed and am going to have to produce a whole bunch of wrapper classes. Ah well.

        Comment

        Working...
        X