8

I was wondering if the C# project setting "Allow unsafe code" applies only to unsafe C# code in the project itself, or is it necessary to set this option when linking in a native C++ DLL? What about linking in a managed DLL that itself links to a native DLL? What does this option really do, under the hood?

Brian Stewart
  • 9,157
  • 11
  • 54
  • 66

4 Answers4

11

It has to do with the "unsafe" keyword in C#. "unsafe" turns off all the checks that would normally happen and allow you to directly access the memory. it doesn't refer to calling native C++ DLL's or interfaces.

Nick Berardi
  • 54,393
  • 15
  • 113
  • 135
7

It allows you to use the "unsafe" block.

unsafe(...)
{
}
Maxime Rouiller
  • 13,614
  • 9
  • 57
  • 107
6

This just relates to the use of unsafe blocks (where pointers can be used). It does not govern P/Invoke.

Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
4

Its necessary to use the unsafe { } context. It used to be required to use sizeof() but in later versions that's no longer true.

You don't need to allow unsafe code if you are externing to another DLL written in another language like C.

cfeduke
  • 23,100
  • 10
  • 61
  • 65