Get byte[] in C# from char* in C++ - c#

In C# I have an data type byte[], which I want to fill in using a C++ function which returns char*
The C++ function (in ImageData.dll)
char* pMemoryBuffer = NULL;
char* LoadData(const char *fileName)
{
// processing pMemoryBuffer ...
return pMemoryBuffer;
}
Import native dll into C#:
[DllImport(".\\Modules_Native\\ImageData.dll", EntryPoint = "LoadData")]
private extern static byte[] LoadData(string fileName);
The byte[] data in C#
byte[] buffer = new byte[256*256];
buffer = LoadData("D:\\myPic.tif");
Apparently it is not working yet, but it presents the idea of what I want to do. So I am wondering how to make this work, and what is the right way to do it. Thanks very much for your education.

try this
// c++
void LoadData(unsigned char* *pMemoryBuffer, const char *fileName)
{
// processing pMemoryBuffer ...
*pMemoryBuffer = resutss;
}
Import native dll into C#:
[DllImport(".\\Modules_Native\\ImageData.dll", EntryPoint = "LoadData")]
private extern static void LoadData(out IntPtr data, string fileName);
When the function returns data will point to the array and you can read the contents using the Marshal class. I guess you would copy it to a new byte array.
byte[] buffer = new byte[256*256];
buffer = Marshal.Copy(LoadData(buffer ,"D:\\myPic.tif"), buffer , 0, buffer.Length);

This should do it:
[DllImport(#".\Modules_Native\ImageData.dll")]
private extern static IntPtr LoadData(string fileName);
byte[] buffer = new byte[256*256];
buffer = Marshal.Copy(LoadData("D:\\myPic.tif"), buffer, 0, buffer.Length);
However, it won't free the memory. Hopefully the C(++) library frees it automatically during the next call, or else provides a deallocation function.
A better approach is to use a caller-allocated buffer, then you would just do:
byte[] buffer = new byte[256*256];
LoadData("D:\\myPic.tif", buffer);
For this, the C(++) code would need to be changed to
int LoadData(const char *fileName, char* pMemoryBuffer)
{
// processing pMemoryBuffer ...
return 1; // if success
}
and the p/invoke declaration to
[DllImport(#".\Modules_Native\ImageData.dll")]
private extern static int LoadData(string fileName, byte[] buffer);

I'm not sure, but my gut says that you can't assign a char* to a byte array, just as you can't in C++ itself. You can either use an IntPtr in C# (probably not super useful), OR, you can pass C++ a byte[] buffer and a number of bytes to write. In other words, I think the following would work:
char* pMemoryBuffer = NULL;
int size = 0;
int seek = 0;
bool LoadData(const char* filename)
{
// load filename
// set seek = 0
// set size to data size
}
int ReadData(char* buffer, int nBytesToRead)
{
// nCopyBytes = min(nBytesToRead, size - seek)
// copy nCopyBytes from pMemoryBuffer+seek to buffer
// seek += nCopyBytes
// return nCopyBytes
}
From C#, you'd use it like this:
byte[] buffer = new byte[256*256];
LoadData("foo.tif");
int bytesRead = ReadData(buffer, 256*256);
Sorry if you specifically want to avoid doing something like this.

Related

Convert managed unsigned char* array into a jbyteArray

How can I convert a managed unsigned char* into a jbyte*?
since I'm new to this, a snippet would be helpful too.
here is my native code:-
JNIEXPORT jobject JNICALL Java_com_me_NativeCaller_processImages
(JNIEnv *env, jclass c, jbyteArray front, jbyteArray back){
unsigned char* frontBuffer = convertJByteArrayIntoUnsignedChar(env, front);
unsigned char* backBuffer = convertJByteArrayIntoUnsignedChar(env, back);
System::Collections::Generic::List<cli::array<unsigned char>^>^ returnedValue = processImages(frontBuffer,backBuffer);
jbyte* bytes;
bytes = returnedValue[1];
bytes = returnedValue[2];
jbyteArray intermediateArray1 = env->NewByteArray(1);
env->SetByteArrayRegion(intermediateArray1,0,1,bytes);
return static_cast<jobject>(intermediateArray1);
}
here at bytes = returnedValue[1];I'm getting an error saying a value of type cli::array<unsigned char,1>^ cannot be assign to an entity of type jbyte*
c# code which returns a List of byte[] :-
public static List<byte[]> processImages(byte[] frontImage, byte[] backImage)
{
CallProcessFrontBackAndMICR(frontImage,backImage);
List<Image> imagesToReturn = GetAllPages(tiffFileBuffer);//convert byte[] into two separate images
front = imagesToReturn[0];
back = imagesToReturn[1];
List<byte[]> bytesToReturn = new List<byte[]>();
bytesToReturn.Add(imageToByteArray(front));
bytesToReturn.Add(imageToByteArray(back));
return bytesToReturn;
}
it's really helpful if someone can help me with this.

Convert a struct to bytes, save to file and read back to struct by another application in C#

I tried writing structures to a binary file but I can't read them correctly.
This is my struct. It has a dynamic number of "values". If the number of values is 3, then GetSize() will return 8 + (8*3) = 32
[StructLayout (LayoutKind.Sequential)]
public struct Sample
{
public long timestamp;
public double[] values;
public int GetSize()
{
return sizeof(long) + sizeof(double) * values.Length;
}
}
First, I convert the structure to bytes by:
public static byte[] SampleToBytes(Sample samp)
{
int size = samp.GetSize();
byte[] arr = new byte[size];
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(samp, ptr, true);
Marshal.Copy(ptr, arr, 0, size);
Marshal.FreeHGlobal(ptr);
return arr;
}
Then, I write the bytes using BinaryWriter, and exit.
When I have to run the program again and read the file I saved. I use BinaryReader. I get every 32 bytes from the file and convert each array of 32 bytes back to struct using:
public static Sample BytesToSample(byte[] arr)
{
Sample samp = new Sample();
IntPtr ptr = Marshal.AllocHGlobal(32);
Marshal.Copy(arr, 0, ptr, 32);
samp = (Sample)Marshal.PtrToStructure(ptr, samp.GetType());
Marshal.FreeHGlobal(ptr);
return samp;
}
However, a SafeArrayTypeMismatchException occurs at PtrToStructure().
Could anyone please tell me what I am doing wrong?
Thanks.
You do realize that double[], being an array type, is a reference type? The struct holds the long followed by a reference to the heap object.
That would be the reason why it doesn't work, I think.
I believe you should simply write the elements of your array to a binary writer.

Marshal float* to C#

I have a DLL which exports a function that returns a float*, that I would like to use it in my C# code. I am not sure how to Marshal my float* so that I can safely use it in C#.
So, in my C++ DLL, I have declared:
static float* GetSamples(int identifier, int dataSize);
In my C# script, I have:
[DllImport ("__Internal")]
public static extern float[] GetSamples (int identifier, int dataSize);
The C++ GetSamples(int,int) allocates memory and return a pointer t the float array. How do I declare the C# GetSamples to Marshal my float array, and how do I access the data (either by iteration or Marshal.Copy)?
Also, can I delete the float* from C# or do I have to call another C++ function to delete the allocated memory?
EDIT:
So this is what I have tried up to now.
First, on the C# side:
Declaration:
[DllImport ("__Internal")]
public static extern int GetSamples ([In, Out]IntPtr buffer,int length, [Out] out IntPtr written);
Trying to call it:
IntPtr dataPointer = new IntPtr();
IntPtr outPtr;
GetSamples(dataPointer, data.Length, out outPtr);
for (var i = 0; i < data.Length; i++){
copiedData[i] = Marshal.ReadByte(dataPointer, i);
}
Then in my C++ lib:
int AudioReader::RetrieveSamples(float * sampleBuffer, size_t dataLength, size_t * /* out */ written)
{
float* mydata = new float[dataLength];
//This is where I copy the actual data into mydata
memcpy(sampleBuffer, mydata, dataLength*sizeof(float));
delete data;
return dataLength;
}
I don't really know what outPtr is for... And I know I have some additional copying steps that I can removes, I just want to get it working for now.
So this is a bit of a complicated answer...
.NET doesn't know how to handle C++ memory allocation, so regardless returning a float * is dangerous at best for this. Furthermore the .NET memory model is based on COM so it is CoTaskMemAlloc based, not that it really helps you here. So here is what I would suggest:
int AudioReader::RetrieveSamples(
float * sampleBuffer,
int dataLength,
int * /* out */ written)
{
// assuming mydata is already defined
if(sampleBuffer == NULL || dataLength == 0)
{
*written = sizeof(mydata);
return -1;
}
ZeroMemory(sampleBuffer, dataLength);
int toCopy = min(dataLength, sizeof(myData));
//This is where I copy the actual data into mydata
memcpy(sampleBuffer, mydata, toCopy);
*written = toCopy;
return 0;
}
[DLLImport("__internal")]
private static extern int GetSamples(
[In, Out]IntPtr buffer,
[In] int length,
[Out] out int written);
float[] RetrieveFloats()
{
int bytesToAllocate = 0;
GetSamples(IntPtr.Zero, 0, out bytesToAllocate);
if(bytesToAllocate == 0)
return null;
int floatCount = bytesToAllocate/ sizeof(float);
float[] toReturn = new float[floatCount];
IntPtr allocatedMemory = Marshal.AllocHGlobal(bytesToAllocate);
int written = 0;
if(GetSamples(allocatedMemory, bytesToAllocate, out written) != -1)
{
floatCount = written/sizeof(float);
Marshal.Copy(allocatedMemory, toReturn, 0, floatCount);
}
Marshal.FreeHGlobal(allocatedMemory);
return toReturn;
}
Passing a bufferLength of zero would return the space required for the buffer, which can then be allocated and passed in.
You will need to allocate the memory for the buffer in C#, you cannot allocate it in C++

passing c++ char* to c# via shared-memory

Sorry for probably simple question but I'm newbie in shared memory and trying to learn by example how to do things.
On c++ side I receive such pair: const unsigned char * value, size_t length
On c# side I need to have regular c# string. Using shared memory what is the best way to do that?
It's not that easy to using the string.
If it's me, I'll try these ways:
1.simply get a copy of the string. System.Text.Encoding.Default.GetString may convert from a byte array to a string.
You may try in a unsafe code block (for that you could use pointer type) to do:
(1) create a byte array, size is your "length"
byte[] buf = new byte[length];
(2) copy your data to the array
for(int i = 0; i < length; ++i) buf[i] = value[i];
(3) get the string
string what_you_want = System.Text.Encoding.Default.GetString(buf);
2.write a class, having a property "string what_you_want", and each time you access it, the above process will perform.
before all, you should first using P/Invoke feature to get the value of that pair.
edit: this is an example.
C++ code:
struct Pair {
int length;
unsigned char value[1024];
};
#include <windows.h>
#include <stdio.h>
int main()
{
const char* s = "hahaha";
HANDLE handle = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(Pair), L"MySharedMemory");
struct Pair* p = (struct Pair*) MapViewOfFile(handle, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(Pair));
if (p != 0) {
p->length = lstrlenA(s);
lstrcpyA((char*)p->value, s);
puts("plz start c# program");
getchar();
} else
puts("create shared memory error");
if (handle != NULL)
CloseHandle(handle);
return 0;
}
and C# code:
using System;
using System.IO.MemoryMappedFiles;
class Program
{
static void Main(string[] args)
{
MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("MySharedMemory");
MemoryMappedViewStream mmfvs = mmf.CreateViewStream();
byte[] blen = new byte[4];
mmfvs.Read(blen, 0, 4);
int len = blen[0] + blen[1] * 256 + blen[2] * 65536 + blen[3] * 16777216;
byte[] strbuf = new byte[len];
mmfvs.Read(strbuf, 0, len);
string s = System.Text.Encoding.Default.GetString(strbuf);
Console.WriteLine(s);
}
}
just for example.
you may also add error-check part.

Calling unsafe code from managed (C#). Reading byte array

I have this method that I need to call and use in my application, but I don't know really know how to do it exactly.
This is the function that I need to call.
[DllImport(dll_Path)]
public static extern int DTS_GetDataToBuffer(int Position, int Length, char* Buffer, int* DataRead);
In my code, I have this function and I'm missing its implementation.
internal static void GetDataToBuffer(int position, int length, out byte[] data, out int dataRead)
{
unsafe
{
// the code I need
}
}
I think most of this is very selfexplanatory. I need to implement the latter function so I can be able to read the data into the buffer and the amount of data read (which should actually be the same as data.Length, but the manufacturer has this as separate option, so I need it).
Can anyone help? Is this clear enough?
Thank you
Edit: Here is the unmanaged declaration from the .h file. Hope it helps.
extern NAG_DLL_EXPIMP int DTS_GetDataToBuffer(int Position,
int Length,
unsigned char *Buffer,
int *DataRead );
Edit #2:
Positon - the position from which to star reading the data.
Length - The amount of data to read (this would be the buffer size).
DataRead - the actual data size that was read.
I don't think you really need to use unsafe pointers here.
Declare function as
[DllImport(dll_Path)]
public static extern int DTS_GetDataToBuffer(
int position,
int length,
byte[] buffer,
ref int dataRead);
Reasonable C# wrapper for this function:
internal static byte[] GetDataToBuffer()
{
// set BufferSize to your most common data length
const int BufferSize = 1024 * 8;
// list of data blocks
var chunks = new List<byte[]>();
int dataRead = 1;
int position = 0;
int totalBytes = 0;
while(true)
{
var chunk = new byte[BufferSize];
// get new block of data
DTS_GetDataToBuffer(position, BufferSize, chunk, ref dataRead);
position += BufferSize;
if(dataRead != 0)
{
totalBytes += dataRead;
// append data block
chunks.Add(chunk);
if(dataRead < BufferSize)
{
break;
}
}
else
{
break;
}
}
switch(chunks.Count)
{
case 0: // no data blocks read - return empty array
return new byte[0];
case 1: // single data block
if(totalBytes < BufferSize)
{
// truncate data block to actual data size
var data = new byte[totalBytes];
Array.Copy(chunks[0], data, totalBytes);
return data;
}
else // single data block with size of Exactly BufferSize
{
return chunks[0];
}
default: // multiple data blocks
{
// construct new array and copy all data blocks to it
var data = new byte[totalBytes];
position = 0;
for(int i = 0; i < chunks.Count; ++i)
{
// copy data block
Array.Copy(chunks[i], 0, data, position, Math.Min(totalBytes, BufferSize));
position += BufferSize;
// we need to handle last data block correctly,
// it might be shorted than BufferSize
totalBytes -= BufferSize;
}
return data;
}
}
}
I can't test this but I think you should let the Marshaler do you conversion(s):
[DllImport(dll_Path)]
public static extern int DTS_GetDataToBuffer(out byte[] data, out int dataRead);
i agree you don't need to use unsafe block. you are using pinvoke, i hope below links might be useful :
http://msdn.microsoft.com/en-us/magazine/cc164123.aspx
http://www.pinvoke.net/
and there are post on stackoverflow too

Categories

Resources