Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

362
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda