C# pointer

I need to create a C# pointer for a DLL function.

I wrote

byte* Read_Data_ptr,

but I get the error:

Pointers and fixed size buffers may only be used in an unsafe context

Any help with this would be geatly appreciated.

Thank you,

Mike



Answer this question

C# pointer

  • jermu

    Hi,
    you need to declare an unsafe block where you can use unmanaged code:

    unsafe
    {
    byte* Read_Data_ptr;
    }

    Consider "marshalling" (i.e. converting your unmanaged type to managed type) when calling functions from DLL's.


  • CodePfo

    It shouldn't need to be a pointer. I looked at the documentation for your USBm.dll library. If you look at their Visual Basic declarations:

    Public Declare Function USBm_WriteA _
    Lib "USBm.dll" _
    (ByVal device As Byte, _
    ByVal data As Byte) _
    As Integer

    and

    Public Declare Function USBm_ReadB _
    Lib "USBm.dll" _
    (ByVal device As Byte, _
    ByRef data As Byte) _
    As Integer

    Then you can see that for USBm_WriteA, device is passed by value, data by value, and an integer is returned. And for USBm_ReadB, device is passed by value, data by reference, and an integer is returned. Since in this particular case for USBm_ReadB, data is an output only parameter, then the correct pInvoke declarations should be:

    [DllImport("USBm.dll", EntryPoint = "USBm_WriteA")]
    public static extern int USBm_WriteA(byte device, byte data);

    [DllImport("USBm.dll", EntryPoint = "USBm_ReadB")]
    public static extern int USBm_ReadB(byte device, out byte data);

    Note that out is just a special case of ref that doesn't require the variable to be pre-initialized where as ref does.

    By using these pInvoke declarations instead of pointers, then you can keep all your C# code 'safe'.


  • dbldown768

    Direct access to memory isn't allowed in 'safe' code and thus pointers have to be used in an unsafe context by wrapping them in an unsafe block. If you are trying to access an un-managed DLL though you are most likely going to want to use a p/Invoke call instead.

    For instance, if you had a function in a DLL "mySuperCoolDll.dll" to read data that had a prototype like:

    int ReadData(unsigned char *buf, int bufSize);

    Then it could be used via p/Invoke like this:

    using System;
    using System.Runtime.InteropServices;

    namespace ConsoleApplication1
    {
    class Program
    {
    //PInvoke signature for:
    //int ReadData(unsigned char *buf, int bufSize);
    //
    [DllImport("mySuperCoolDll.dll")]
    public static extern int ReadData([Out] byte[] buf, int bufSize);

    static void Main(string[] args)
    {
    int size = 256,
    bytesRead;
    byte[] buf = new byte[size];

    //Call the function in the unmanaged DLL
    bytesRead = ReadData(buf, size);
    }
    }
    }

    Doing a couple searches for C# and pinvoke should help point you in the right direction. More information about what you're actually trying to do though would be more helpful.

  • AmolWankhede

    I wrote this (for an output function):

    [DllImport("USBm.dll", EntryPoint = "USBm_WriteA")]

    public static extern void USBm_WriteA(byte deviceindex, byte data);

    and this (for an input function):

    [DllImport("USBm.dll", EntryPoint = "USBm_ReadB")]

    public static extern void USBm_ReadB(byte deviceindex, byte data);

    And this works :

    USBmWrapper.USBm_WriteA(0, Write_Data);

    But this doesn't:

    USBmWrapper.USBm_ReadB(0, Read_Data);

    Error : "AccessViolationException was unhandled" "Attempt to read or write protected memory. This is often an indication that other memory is corrupt."

    So, Read_Data has to be a pointer.


  • C# pointer