C # and void pointers

I am writing my first C # application, but, with luck, I have to use void pointers (working with a DLL that returns descriptors). From what I read, there are several options:

It should also be noted that I need to collect information between DLL and C # applications.

An unsafe option was the first that appeared on Google, but for some reason wrote the unsafe keyword before all of my functions just wouldn't feel right.

Any recommendations?

+5
source share
3 answers

Generally, for marshaling situations, using IntPtr would be the preferred approach here. This is allowed in safe code, and it shows very clearly that your intention is to marshal the pointer back and forth.

This is what BCLs are descriptors. For example, you can build a cursor from IntPtr that represents your own handle.

+6
source

Basic rule: if you need to interact with it as a pointer in C #, use unsafe code / pointers. If you can think of it as an opaque handle, use IntPtr. You should use ref if you need to pass a pointer to a structure to unmanaged code.

0
source

Unsafe is a compiler instruction that there will be pointers in this context. From MSDN

In a common language environment (CLR), unsafe code is called unchecked code. Insecure C # code is not necessarily dangerous; it is just code whose security cannot be verified using the CLR . The CLR will therefore only execute unsafe code if it is in a fully trusted assembly. If you use unsafe code, you are responsible for ensuring that your code does not pose a security risk or pointer error.

I used it with .net 1.1 when using the C ++ API in C #, which used to talk to mainframes via the PC COM port :)

0
source

All Articles