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
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
C# pointer
jermu
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
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
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.