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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:27:10+00:00 2026-05-31T10:27:10+00:00

I’m working on a tag reader and i was able to connect it and

  • 0

I’m working on a tag reader and i was able to connect it and read some data. My problem is when I attempt to read the tag id which is a large sequence of characters.

The SDK is in C language and I’m developing a c# application.

short GetIDBuffer(HANDLE hCom, unsigned char* DataFlag, unsigned char * Count, 
      unsigned char *value, unsigned char* StationNum)

In my C# application:

[DllImport("Reader2.dll",CharSet = CharSet.Ansi)]
public static extern short GetIDBuffer(IntPtr hCom, ref uint DataFlag, 
       ref uint Count, ref String value, ref uint StationNum);

Dataflag, count, station number are mainly small sequences where a uint type is doing well. But when it comes to value it’s a large sequence. I tried the type string but it’s throwing this exception:

Attempted to read or write protected memory. This is often an
indication that other memory is corrupt.

  • [MarshalAs(UnmanagedType.LPWStr)]string value

    didnt solve the problem

  • count value is returned correctly

  • My operating system is 64 bit: i used corflags application.exe/32bit+ and i was able to load the dll correctly.

code snapshot:

 [DllImport("Reader2.dll")] 
    public static extern byte OpenReader(ref IntPtr hCom, byte LinkType, string com_port, uint port);
    [DllImport("Reader2.dll")]
    public static extern  short GetIDBuffer(IntPtr hCom, ref byte DataFlag, ref byte Count,**(type)** value , ref byte StationNum);

    static  void Main(string[] args)
    {

        byte count = 0, station = 1, flag = 0;
        IntPtr hcom = IntPtr.Zero;        
        OpenReader(ref hcom, 2, "192.168.0.178", 4001);
        // valid handle returned from openReader 
      //
        **GetIDBuffer code**
            //
  • 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-31T10:27:11+00:00Added an answer on May 31, 2026 at 10:27 am

    You shouldn’t need to use corflags application.exe/32bit+. All you need to do is set the platform target to x86 in project/properties/build.

    This will work (well it does using a test native method I created with the same signature as given above).
    This first method doesn’t require the unsafe keyword, or require the project to be built with ‘Allow unsafe code’ set to true.

    internal static class NativeMethods
    {
        [DllImport("Reader2.dll")]
        public static extern short GetIDBuffer(
               IntPtr hCom, ref byte dataFlag, ref byte count, 
               byte [] value, ref byte stationNum);
    }
    
    static int TestGetIDBuffer()
    {
        const int arraySize = 255;
        byte[] bytes = new byte[arraySize + 1]; 
    
        byte dataFlag = 0;
        byte count = arraySize;
        byte status = 0;
    
        int retval = NativeMethods.GetIdBuffer(IntPtr.Zero, ref dataFlag, ref count, bytes, ref status);
    
        Debug.WriteLine(Encoding.ASCII.GetString(bytes));
        Debug.WriteLine(dataFlag);
        Debug.WriteLine(status);
        Debug.WriteLine(count);
        Debug.WriteLine(retval);
    
        return retval;
    }
    

    Here’s an alternative using a fixed array of bytes.
    This second method requires the unsafe keyword, and also that the project is built with ‘Allow unsafe code’ set to true.

    internal static class NativeMethods
    {
        [DllImport("Reader2.dll")]
        public static extern unsafe short GetIDBuffer(
               IntPtr hCom, ref byte dataFlag, ref byte count, 
               byte* value, ref byte stationNum);
    }
    
    static unsafe int TestGetIDBuffer()
    {
        const int arraySize = 255;
        byte[] bytes = new byte[arraySize + 1];
    
        byte dataFlag = 0;
        byte count = arraySize;
        byte status = 0;
    
        int retval;
        fixed (byte* buffer = bytes)
        retval = NativeMethods.GetIdBuffer(
                 IntPtr.Zero, ref dataFlag, ref count, buffer, ref status);
    
        Debug.WriteLine(Encoding.ASCII.GetString(bytes));
        Debug.WriteLine(dataFlag);
        Debug.WriteLine(status);
        Debug.WriteLine(count);
        Debug.WriteLine(retval);
    
        return retval;
    }
    

    The dataFlag, count and stationNum all seem to be in/out byte values.

    The data buffer being filled is an array of byte. This buffer needs to fixed so that the GC won’t move it while you’re calling the native method. This is done implicitly in the first example, and explicitly in the second.

    I’m assuming the available buffer size should be passed into the method in the count parameter, and that this value on exit will be the amount of buffer used. I’ve allowed an extra byte to ensure there’s a null terminating character if the array of bytes needs to converted to a string.

    There are actually two forms of the fixed statement. One mentioned in this MSDN article allows you to create fixed size array, as in
    public fixed byte Bytes [ArraySize];
    The other in this MSDN article allow you to pin the location of a variable in order to take it’s address.

    Here’s my C++ test code:

    extern "C" __declspec(dllexport) unsigned short __stdcall GetIDBuffer( 
        HANDLE hCom, unsigned char * dataFlag, unsigned char * count, 
        unsigned char* buffer,  unsigned char * status )
    {
        memset(buffer, 0x1E, *count);
    
        *dataFlag = 0xa1;
        *count = 0x13;
        *status = 0xfe;
    
        return 0x7531;
    }
    

    The only difference between the C# code given above and my test code is that the entry point has to be specified differently since I used the C++ compiler, e.g.

    [DllImport("PInvokeTestLib.dll", EntryPoint = "_GetIDBuffer@20")]
    public static extern unsafe short GetIdBuffer(...
    

    You can safely specify the parameters passed to the method (not including the value array parameter) as primitive types other than byte, such as int, long etc. This is because 1) the values are passed in reference and 2) x86 uses little-endian byte ordering. This results in the single byte being written to the least significant byte of the four byte int passed in.

    It’s advisable to use matching types though, byte in this case.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a text area in my form which accepts all possible characters from
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported

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.