2

I am currently modelling a type conversion mechanism which converts native (Windows Data Types) to .NET

Example:

HANDLE = System.IntPtr
HDC = System.IntPtr
WORD = System.UInt16

INT_PTR is 32bit/64bit on their respective machines, so in .NET thats a System.IntPtr

HALF_PTR (and others) are 16bit/32bit on their respective 32bit/64bit machines...

so how would I model HALF_PTR in .NET - If possible?

I guess another way of looking at this is: Is There a "short" pointer equivalent for C#...something like ShortPtr?

Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
  • Use of HALF_PTR? Because if it's only for marshaling data for one or two methods, perhaps it's faster to create some proxies for the methods. – xanatos Aug 15 '13 at 13:09
  • @xanatos, do you think that in this respect, I could just omit this type of "short" pointer from my code and warn the user of its use instead? – Matthew Layton Aug 15 '13 at 13:13
  • 4
    In years of using the Win32 API I never met `HALF_PTR`, that seems to be used only in [very specialized cases](http://stackoverflow.com/questions/3343779/half-ptr-windows-data-type); I don't even think it is actually used in any public API, since Googling "HALF_PTR site:microsoft.com" doesn't give any result relative to a `struct` or function. Thus, I think it could be safe to just ignore it, and provide a warning in the rare instance where it could show up, explaining that a `struct`/function that uses it requires manual intervention. – Matteo Italia Aug 15 '13 at 13:15
  • 2
    @series0ne Show us the APIs you want to convert! :-) Otherwise it becomes an abstract problem. There are often multiple solutions to the same problem. – xanatos Aug 15 '13 at 13:23

2 Answers2

3

This is a non-issue. It is declared in Basetsd.h but it isn't actually used in any winapi declaration. The odds you'll actually ever run into it are very close to zero.

If you must, it is only defined for programs that target 64-bit processors. It maps a 64-bit pointer to two 32-bit halves. You'll get the same by converting an IntPtr to UInt64 and get the two UInt32 parts with & 0xffffffff and >> 32.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
2

I don't think it's possible in .NET (Microsoft .NET implementation, I mean). In Mono it's possible to conditionally redirect the reference to a dll based on the current architecture, so you could create two HalfPtr structs, one for 32 bits system and one for 64 bits system, but on .NET this isn't possible. Conditional compilation (#if) is only at compile time, not at runtime, and you would need something similar but at runtime, and automatic marshaling isn't powerful enough to do "conditionals".

On Mono:

<configuration>
    <dllmap dll="myhalfptrdll">
        <dllentry dll="myhalfptrdll32.so" wordsize="32" />
        <dllentry dll="myhalfptrdll64.so" wordsize="64" />
    </dllmap>
</configuration>

The only possible solution for the rare cases is to make 2 PInvoke externs that "point" one to an int and one to a short and that are selected by some static constructor.

xanatos
  • 109,618
  • 12
  • 197
  • 280