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

266
Views
How to make this code safe when accessing DLL?

To return arrays in C pointers needed. To clarify my question, I created a simple .c function and compiled as DLL and accessing it by C#. The C function is:

#include <stdio.h>

__declspec(dllexport) int* ReturnArray(int size) {

    int* arr = malloc(size * sizeof(int)); 

    arr[0] = 19;
    arr[1] = 18;
    arr[2] = 17;
    arr[3] = 16;
    arr[4] = 15;

    return arr;
}

On the C# side:

namespace ConsoleC2CSExample
{
    class Program
    {
        [DllImport("array.dll", EntryPoint = "ReturnArray", CallingConvention = CallingConvention.Cdecl)]
        public unsafe static extern short* ReturnArray(int size);
        static unsafe void Main(string[] args)
        {
            int arraySize = 5;

            short *ptr = ReturnArray(arraySize);
            
            for(int i = 0; i < arraySize; i++)
                Console.WriteLine(*(ptr + i * sizeof(short)));              

            Console.Read();
        }
    }
}

And the output is as expected as follows:

enter image description here

I have two questions here.

  1. How can this be done by not using unsafe mode?
  2. And what are the dangers if any of using unsafe?
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

C#/.NET is so-called managed code because it runs safely on the common language runtime (CLR). Whereas, C code is so-called unmanaged code.

The CLR ensures, that we will not suffer the typical problems known from C code (like buffer underflow, buffer overflow, ...) resulting in vulnerable code, crashing applications, unknown behavior, etc.

So the danger of using unsafe code is, that errors in your C library will probably terminate your entire C#/.NET application unexpectedly, causing you a lot of headache.

To get at least rid of the unsafe context within your C# code, you can try the following:

using System.Runtime.InteropServices;

namespace ConsoleC2CSExample
{
    class Program
    {
        [DllImport("array.dll", EntryPoint = "ReturnArray", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr ReturnArray(int size);

        static void Main(string[] args)
        {
            int arraySize = 5;

            IntPtr ptr = ReturnArray(arraySize);
            
            for(int i = 0; i < arraySize; i++)
                Console.WriteLine(Marshal.ReadInt32(ptr + i * sizeof(int)));

            Console.Read();
        }
    }
}
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!