Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 195025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:38:44+00:00 2026-05-11T16:38:44+00:00

I have a C# project that imports a C dll, the dll has this

  • 0

I have a C# project that imports a C dll, the dll has this function:

int primary_read_serial(int handle, int *return_code, int *serial, int length);

I want to get access to the serial parameter. I’ve actually got it to return one character of the serial parameter, but I’m not really sure what I’m doing and would like to understand what is going, and of course get it working.

So, I’m very sure that the dll is being accessed, other functions without pointers work fine. How do I handle pointers? Do I have to marshal it? Maybe I have to have a fixed place to put the data it?

An explanation would be great.

Thanks!
Richard

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:38:44+00:00Added an answer on May 11, 2026 at 4:38 pm

    You will have to use an IntPtr and Marshal that IntPtr into whatever C# structure you want to put it in. In your case you will have to marshal it to an int[].

    This is done in several steps:

    • Allocate an unmanaged handle
    • Call the function with the unamanged array
    • Convert the array to managed byte array
    • Convert byte array to int array
    • Release unmanaged handle

    That code should give you an idea:

    // The import declaration
    [DllImport("Library.dll")]
    public static extern int primary_read_serial(int, ref int, IntPtr, int) ;
    
    
    // Allocate unmanaged buffer
    IntPtr serial_ptr = Marshal.AllocHGlobal(length * sizeof(int));
    
    try
    {
        // Call unmanaged function
        int return_code;
        int result = primary_read_serial(handle, ref return_code, serial_ptr, length);
    
        // Safely marshal unmanaged buffer to byte[]
        byte[] bytes = new byte[length * sizeof(int)];
        Marshal.Copy(serial_ptr, bytes, 0, length);
    
        // Converter to int[]
        int[] ints = new int[length];
        for (int i = 0; i < length; i++)
        {
            ints[i] = BitConverter.ToInt32(bytes, i * sizeof(int));
        }
    
    }
    finally
    {
        Marshal.FreeHGlobal(serial_ptr);
    }
    

    Don’t forget the try-finally, or you will risk leaking the unmanaged handle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MFC project that imports a C type DLL for use. This
I have a project that imports a DLL (written by me). Sometimes when an
I have a C++ DLL that imports a .tlb file generated in a C#
I have a website project. It has assembly references to external DLLs that we
I have a managed DLL that I want to use in a legacy application
I have an unmanaged dll that contains a function to read a data from
I've read the code that imports a dll into a project but I don't
I have all COM dll generated from a C++ library project. I want to
I have a project that may be started on local machine with ./manage.py runserver
I have a project that I have divided quite aggressively into different layers. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.