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

  • Home
  • SEARCH
  • 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 9029235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:12:08+00:00 2026-06-16T07:12:08+00:00

Possible Duplicate: unsigned char ** equivalent in c# and have to write the return

  • 0

Possible Duplicate:
unsigned char ** equivalent in c# and have to write the return value in a file

I have to call a win32 dll function

int func1(int arg1, unsigned char *arg2, int *arg3);

and i wrote wrapped c# as

public extern int fuc1(int arg1, out IntPtr arg2, out IntPtr arg3);

The arg2 has to allocate 2048 bytes and send it to the win32 dll. I’ll get arg2 and arg3 as output.

how can i declare in c# test application as well as c# wrapper. am i doing it right ?

  • 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-06-16T07:12:09+00:00Added an answer on June 16, 2026 at 7:12 am

    byte in C# is unsigned 8-bit int. byte[] is an array of them. To get a pointer to this array, use:

     var myArray = new byte[2048];
     fixed(byte* arg2 = myArray)
     {
          // use arg2
     }
    

    or:

     var myArray = new byte[2048];
     GCHandle pinnedRawData = GCHandle.Alloc(myArray, GCHandleType.Pinned);
     try
     {  
        // Get the address of the data array
        IntPtr pinnedRawDataPtr = pinnedRawData.AddrOfPinnedObject();
     }
     finally
     {
        // must explicitly release
        pinnedRawData.Free(); 
     } 
    

    Alternatively, if the called function will not cache a pointer to the array, you can simply do this:

     public static extern int fuc1(int arg1, [In,Out] byte[] arg2, ref int arg3);
    
     var arg1 = 0;
     var arg2 = new byte[2048];
     int arg3 = 42; // If this value won't be used, you can skip initializing arg3 and mark arg3 as out instead of ref (of course, this is pedantic and extraneous, and C# shouldn't even have 'out' as a keyword)
    
     func1(arg1, arg2, ref arg3);
    

    P/Invoke will pin it automatically.

    MSDN Marshaling Arrays of Types

    Related SO question

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

Sidebar

Related Questions

Possible Duplicate: Unsigned and signed comparison unsigned int and signed char comparison I have
Possible Duplicate: Unsigned long with negative value Assigning negative numbers to an unsigned int?
Possible Duplicate: Set all bytes of int to (unsigned char)0, guaranteed to represent zero?
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Addition of two chars produces int Given the following C++ code: unsigned
Possible Duplicate: What does 'unsigned temp:3' means I have been trying to learn raw
Possible Duplicate: performance of unsigned vs signed integers I have read somewhere that it's
Possible Duplicate: What does template <unsigned int N> mean? Hi ! Are non-type template
Possible Duplicate: What does 'unsigned temp:3' mean? please what does this notation mean int
Possible Duplicate: how to convert unsigned char array to QByteArray in Qt? How to

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.