Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

363
Vistas
CS0212 Error using dll with unsafe code: You can only take the address of an unfixed expression inside of a fixed statement initializer

The (incomplete) snippet

unsafe class MainWindow
{
     ...
     IntPtr somePtr = IntPtr.Zero;
     unsafe private void Click(object sender, RoutedEventArgs e)
     {
         NamespaceFromReferencedUnsafeDll.SomeFunction(&somePtr)
     }
     ...
}

}

Is supposed to call SomeFunction from a managed Dll with unsafe code, to set the pointer somePtr, but results in the compiler error

CS0212: You can only take the address of an unfixed expression inside of a fixed statement initializer

According to this answer, the fixed keyword has to be used in some way, but

fixed(IntPtr somePtr = IntPtr.Zero);

didn't help.

How can I fix this (no pun intended) ?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The problem was the signature of

SomeFunction 

in the referenced unsafe dll.

After changing

public static unsafe uint SomeFunction(IntPtr* somePtr)

to

public static unsafe uint SomeFunction(out IntPtr somePtr)

the snippet

class MainWindow
{
     ...
     IntPtr somePtr = IntPtr.Zero;
     private void Click(object sender, RoutedEventArgs e)
     {
            NamespaceFromReferencedUnsafeDll.SomeFunction(out somePtr)
     }
     ...
}

compiles without errors and works at runtime.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda