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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:02:29+00:00 2026-05-18T01:02:29+00:00

I need to use an unmanaged VC++ dll. It has the following two functions

  • 0

I need to use an unmanaged VC++ dll.

It has the following two functions that require C# wrappers:

bool ReadData(byte* byteData, byte dataSize);
bool WriteData(byte* byteData, byte dataSize);

They both return true if they succeed, but false otherwise.

Currently in C# I have a class (WRAP) with functions:

[DllImport("kirf.dll", EntryPoint = "ReadData", SetLastError=true)]
[return: MarshalAs(UnmanagedType.VariantBool)]
public static extern Boolean ReadData([Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)]out Byte[] bytesData, Byte dataSize);

[DllImport("kirf.dll", EntryPoint = "WriteRFCard", SetLastError = true)]
[return: MarshalAs(UnmanagedType.VariantBool)]
public static extern Boolean WriteRFCard[In]Byte[] bytesData, [In]Byte dataSize);

I then have

byte[] newData = new byte[17];
byte dataSize = 17;
bool result = WRAP.ReadData(out newData, dataSize);

This always gives me a result=false, newData = null, and no error thrown(or rather it always come back successful).

And

byte[] bytesData = new Byte[] { (byte)0x9B, (byte)0x80, (byte)0x12, (byte)0x38, (byte)0x5E, (byte)0x0A, (byte)0x74, (byte)0x6E, (byte)0xE6, (byte)0xC0, (byte)0x68, (byte)0xCB, (byte)0xD3, (byte)0xE6, (byte)0xAB, (byte)0x9C, (byte)0x00 };
byte dataSize = 17; 
bool actual = WRAP.WriteData(bytesData, dataSize);

Any ideas as to what I could be doing wrong?

edit

This is how I eventually managed it:

        [DllImport("kirf.dll", EntryPoint = "ReadData", SetLastError=true)]
        [return: MarshalAs(UnmanagedType.VariantBool)]
        public static extern Boolean ReadData([Out] Byte[] bytesData, Byte dataSize);

[DllImport("kirf.dll", EntryPoint = "WriteData", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.VariantBool)]
        public static extern Boolean WriteData([In] Byte[] bytesData, Byte dataSize);

And:

Byte[] newData=new byte[17];
bool result = KABA_KIRF.ReadData(newData, (Byte)newData.Length);

and

bool result = KABA_KIRF.WriteData(data, datasize);

where data was a Byte[] parameter passed into the function.

  • 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-18T01:02:29+00:00Added an answer on May 18, 2026 at 1:02 am

    Does ReadData create a new byte array? Otherwise, I don’t think bytesData should be an out param.

    Look at the PInvoke spec of ReadFile for comparison:

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool ReadFile(IntPtr hFile, [Out] byte[] lpBuffer,
       uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, IntPtr lpOverlapped);
    

    lpNumberOfBytesRead is an out param, because ReadFile changes the value of it. i.e. if you pass a local variable for lpNumberOfBytesRead, ReadFile will change the value on the stack. The value of lpBuffer on the other hand is not changed: It still points to the same byte array after the call. The [In,Out]-Attributes tell PInvoke what to do with the contents of the array you pass.

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

Sidebar

Related Questions

I have a C# dll that I need to call from unmanaged C++. The
I have a 3rd party DLL that I need to use in my application.
I need to use the objdump and readelf commands in my application that runs
I need the InterOp Win32 code (unmanaged Win32 DLL's and EXE) with .NET. I
I have been provided with an unmanaged C++ DLL that contains several classes. I
I have a DLL from which I need to P/Invoke the following C method:
I have to use third party, unmanaged COM dll into my .NET application. I
I need to read unmanaged memory into a managed byte array. For this I
I have this method that I need to call and use in my application,
I have an unmanaged class that I'm trying to dllexport from a managed DLL

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.