5

I have a declaration and in the declaration, I want to set a height is a pointer to a double but get the error mesasage:

Error 1 Pointers and fixed size buffers may only be used in an unsafe context,

Can someone show me the right way to declare the type of pointer in a double ?

Below is mine declaration and I set the height to a pointer of double (double* height) but gets an error message.

private static extern bool GetElevation(double dLat, double dLon, double* height);
Hiren Gohel
  • 4,942
  • 6
  • 29
  • 48
Fabian
  • 441
  • 4
  • 14

2 Answers2

6

Your extern declaration should probably be:

private static extern bool GetElevation(double dLat, double dLon, ref double height);

Hope this helps!

Edit

This question (and accepted answer) might shed some light on the subject. It talks about ref vs out (not sure which would fit better in your situation) and Marshalling.

Community
  • 1
  • 1
Mark Carpenter
  • 17,445
  • 22
  • 96
  • 149
3

I think you should:

  1. Learn more about using pointers and what unsafe blocks are in C#, here is a good resource
  2. Mark your function as "unsafe", see below:

private static unsafe extern bool GetElevation(double dLat, double dLon, double* height)

Once all that is done then you can compile with the /unsafe switch.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Panicos
  • 311
  • 4
  • 15