Skip to content

Error with Enum GCHandleType.Pinned #174

Closed
@aPere3

Description

@aPere3

Hello,

I try to use the following snippet from this page to turn a byte[] into a numpy array:

from ctypes import string_at
clr.AddReference("System.Runtime.InteropServices")
from System.Runtime.InteropServices import GCHandle, GCHandleType

handler = GCHandle.Alloc(CSArray, GCHandleType.Pinned)
try:
    pointer = handler.AddrOfPinnedObject().ToInt32()
    dest = np.fromstring(string_at(pointer, len(CSArray))) 
finally:
    if handler.IsAllocated: handler.Free()

On this line:

handler = GCHandle.Alloc(CSArray, GCHandleType.Pinned)

I get the following error: TypeError: No method matches given arguments

Executing this code:

handler = GCHandle.Alloc(CSArray)

brings no error, so I assume that the issue must be from GCHandleType.Pinned.

I'm a bit troubled by:

print(type(GCHandleType.Pinned))
>>>int
print(GCHandleType.Pinned)
>>>3

Is it normal that enumare modeled by int?

Anyway, I can't get to have my code working...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions