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 8489993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:58:55+00:00 2026-06-10T21:58:55+00:00

I need to call a c++ method via dllimport. Everything is working fine as

  • 0

I need to call a c++ method via dllimport. Everything is working fine as long as there are not the following two parameters in the c++ method:

const void * key,
void * out

I think I need to marshal them. But how does it work?
The key should be a pointer to a byte array and the out parameter is also a byte array with a length of 16.

After trying what Jcl suggests, I have the following:

Using the first method (use out byte[] outprm), the program crashes without error (when reaching the call point).
But using the second way (from Jcl’s comment), I have the following:

[DllImport(@"MyDLL.dll", SetLastError = true)]
public static extern void MyMethod(IntPtr key, out IntPtr outprm); 

private void button1_Click(object sender, EventArgs e)
    {            
        byte[] hash = new byte[16];
        byte[] myArray = new byte[] { 1, 2, 3 };
        IntPtr outprm = Marshal.AllocHGlobal(hash.Length);
        IntPtr key = Marshal.AllocHGlobal(myArray.Length);
        Marshal.Copy(myArray, 0, key, myArray.Length);
        MyMethod(key, out outprm);
        Marshal.Copy(outprm, hash, 0, 16);
        Marshal.FreeHGlobal(key);                       
    }

Now there is no error when calling MyMethod. I just get the following error when I try to copy the data back: AccessViolationException

It said I want to write into protected memory. The dll and the c# software is x64 (and needs to be x64). Maybe this i the reason?

  • 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-10T21:58:57+00:00Added an answer on June 10, 2026 at 9:58 pm

    Use IntPtr for both key and out, something like:

    [DllImport ("whatever.dll")]
    public static extern void MyMethod(IntPtr key, IntPtr outprm);
    

    To make key be an IntPtr, taking myArray being a byte array:

    IntPtr key = Marshal.AllocHGlobal(myArray.Length);
    Marshal.Copy(myArray, 0, key, myArray.Length);
    // ... call your function ...
    Marshal.FreeHGlobal(key );
    

    To get the 16 byte array from outprm:

    IntPtr outprm;
    MyMethod(key, outprm);
    byte [] myArray = new byte[16];
    Marshal.Copy(outprm, myArray, 0, 16);    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

does anybody know how to call C++ functions or methods via JavaScript. Need scripting
I need to call a method and pass an object from my custom UITableViewClass
I need to call a method I have defined as: -(IBAction)next:(id)sender{ ... } I
I have several models that need to call a method that takes a particular
This is hopefully a softball syntax question: I need to call a method with
I need a help from ur side. Actually I need to call a method
I am creating a Console Application, in that I need to Call a Method,
This one has me stumped. I need to call an activity method from within
i need to log each method call in my application. i dont want to
I am using postsharp to log method call and i need to log the

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.