• 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

ASB

Godlike
Joined
Jul 6, 2005
Messages
5,480
Likes received
210
Thanks received
91
Location
Somerset
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?
 
ASB said:
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!!!!
 
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. :tantrum:
 

Advertisers

Back
Top