Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

361
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!