C#: How to access managed 2D array on the C++-CLI side? - c#

How do I access a managed 2D array on the C++ side?
I know we are supposed to use pin_ptr<T> in order to access the managed array from the C++ side. It's straightforward with a 1D array, but with a 2D array I don't know how to use pin_ptr<T> correctly.
My code looks like this, where foo() will be called from the C# side:
nativeFunc(double **ptr);
foo(array<double,2> ^A)
{
const int len = A->GetLength(0);
double **ptr = (double**) alloca(sizeof(double*) * len);
for(int i = 0; i < len; i++)
{
pin_ptr<double> pinned = &A[i,0]; //Will go out of scope!
ptr[i] = pinned;
}
nativeFunc(ptr);
}
The problem is that my pin_ptr<T> will go out of scope too early, as it's located inside the loop body, and thus I think above code is NOT safe. But how can I avoid this? Apparently, it's not allowed to make an array of pin_ptr<T>, neither managed nor unmanaged. It also cannot be added to a std::vector and it cannot be made a class member either. So I'm kind of stuck here...
Thanks for any suggestions...

Okay, after some more digging, I found out that GCHandle::Alloc(x, GCHandleType::Pinned) may work as a more flexible replacement for pin_ptr<T> here. However, it seems we can only pin down the managed array as a whole. It does not seem to be possible to pin down a single sub-array (inner array) this way, like the pin_ptr<T> would do. Furthermore, by "try and error" method I have figured out that from the GCHandle handle I can get an unmanaged pointer via hdl.AddrOfPinnedObject().ToPointer() and that this one points to a continuous block of memory which contains the whole 2D array in a "flattened" (serialized) form. From here I can reconstruct the unmanaged 2D array, by using the proper base-pointer and stride. But is this considered a "safe" method and does it always work or is it even implementation specific ???
So I have hacked together a solution like this:
class ArrayPinHandlerRAII
{
public:
ArrayPinHandlerRAII(array<double,2> ^managedArray)
{
m_dimOuter = managedArray->GetLength(0);
m_dimInner = managedArray->GetLength(1);
m_handle = GCHandle::Alloc(managedArray, GCHandleType::Pinned);
m_ptr = new double*[m_dimOuter];
double *basePointer = reinterpret_cast<double*>
(m_handle.AddrOfPinnedObject().ToPointer());
for(size_t d = 0; d < m_dimOuter; d++)
{
m_ptr[d] = basePointer;
basePointer += m_dimInner;
}
}
~ArrayPinHandlerRAII(void)
{
delete [] m_ptr;
m_handle.Free();
}
inline double **data(void)
{
return m_ptr;
}
inline const size_t &dimOuter(void) const
{
return m_dimOuter;
}
inline const size_t &dimInner(void) const
{
return m_dimInner;
}
private:
GCHandle m_handle;
double **m_ptr;
size_t m_dimOuter;
size_t m_dimInner;
};
Any opinions? ;-)

Okay, one of the examples in MSDN has the following important info:
Pinning a sub-object defined in a managed object has the effect of pinning the entire object. For example, if any element of an array is pinned, then the whole array is also pinned. There are no extensions to the language for declaring a pinned array. To pin an array, declare a pinning pointer to its element type, and pin one of its elements.
So the code can actually be simplified to:
void nativeFunc(double **ptr);
void foo(array<double,2> ^A)
{
int dimOuter = managedArray->GetLength(0);
int dimInner = managedArray->GetLength(1);
pin_ptr<double> pinned = &A[i,0]; //This pins the *entire* array!
double **ptr = (double**) alloca(sizeof(double*) * dimOuter);
double *basePtr = pinned;
for(int i = 0; i < dimOuter; i++)
{
ptr[i] = basePtr;
basePtr += dimInner;
}
nativeFunc(ptr);
}

Related

string[,] array of string arrays equivalent in C

In C#, if I were to create an array of string arrays it is easy as doing:
string[,] array = new string[50,7];
How would I go about doing that in C? I do understand that I should make use of pointers but what I have come up with doesn't seem to work properly.
char *array[50][7];
Please note that I'm not looking to set the elements to constant values, I need to be able to access them/set them in the program again either by means of using the scanf() function or simply with = if possible. What is the simplest way of achieving this?
EDIT: Am I doing something wrong here? The following very simple example crashes the program:
char *username;
printf("Username: ");
scanf("%s", &username);
array[0][0] = malloc(strlen(username) + 1);
strcpy(array[0][0], username);
I have, in fact, added a reference to stdlib.h
There is no easy way :) Since C has no high level string class, at least not without invoking some non-standard library.
To be flexible you would indeed need a 2D array of pointers char *array[50][7];, where every pointer is set to point at the actual string. Upon creating a string, you will have to allocate enough memory to hold it, plus the null terminator, by using malloc(). For example:
array[x][y] = malloc(strlen(the_string) + 1);
assert(array[x][y] != NULL);
It is not possible to copy strings in C with the = assignment operator, so after allocating the memory, you have to copy the string into the allocated memory area by using strcpy.
strcpy(array[x][y], the_string);
The following code crashes as code is attempting to save scanf() input to the place pointed to by username, yet username is not initialized.
char *username;
scanf("%s", &username); // bad
Instead, could use
char username[100];
scanf("%99s", username);
Or better
char username[100];
fgets(username, sizeof username, stdin);
username[strcspn(username, "\n")] = '\0'; // lop off potential \n
It appear OP wants a 50 x 7 array of pointers to C strings allocated like in string[,] array = new string[50,7];
Recall, in C, a string is itself a character array ending with a null character
#include <stdlib.h>
typedef char *a50_7_T[50][7];
a50_7_T *a50_7_alloc(void) {
a50_7_T *a = malloc(sizeof *a);
for (int i=0; i<50; i++) {
for (int j=0; j<7; j++) {
(*a)[i][j] = NULL; // or whatever OP wants as initial state
}
}
return a;
}
void a50_7_free(a50_7_T *a) {
for (int i=0; i<50; i++) {
for (int j=0; j<7; j++) {
free((*a)[i][j]);
}
}
free(a);
}
// Sample usage code
#include <string.h>
void foo(void) {
a50_7_T *a = a50_7_alloc();
printf("Size %zu\n", sizeof *a); // e.g. "Size 1400"
(*a)[0][0] = strcpy(malloc(6), "Hello");
a50_7_free(a);
}
OTOH if OP want to create the array as part of the declaration, what OP did was on the right track.
// Initialize all to zeros, (NULL)
char *array[50][7] = { 0 };
...
array[0][0] = strcpy(malloc(6), "Hello");
...
free(array[0][0]);
The array is ok as that. The problem may come from strings. C 'strings' are very low level, and can't really be compared to C# strings. You will have to manually allocate and deallocate space for them. If you plan on using scanf on them, you can allocate a buffer for each one, but you will have problems if you find a string longer than the buffer. You can assign them with '=', but only if you handle deallocation of the old value. Maybe you can check a library such as 'glib' to do some of the work for you.
Well, you have to decide two things where C# simply doesn't give you a choice (or at least, the standard one is very strongly recommended):
How do you want to represent your 2-dimensional array?
You have the choice between, among others,
a single-dimensional array and explicit pointer-arithmetic,
a pointer to a 2-dimensional array with one fixed dimension,
a pointer to a 1-dimensional array of pointers to 1-dimensional arrays.
How do you want to represent your string?
Decide on the code-unit: char (hopefully 8-bit), wchar16_t, wchar32_t, wchar_t?
Counted strings or 0-terminated?
In a fixed-size-buffer or dynamically allocated?

Passing pointer-to-pointer to c++ class library from c#

I have legacy c library and exported it all in a c++ class library. So far, I have problem with passing arguments of type double** which simply is an out 2D array. Here is my c++ method signature:
public: int GetVariableValues(double **time_values) {
return LegacyGetVariableValues(time_values);}
And here is my c# client call:
double[][] a;
Legacy.GetVariableValues(a);
However it does not work and I get type errors.
Anyone knows how to pass **(out) of course without using unsafe tag in C# client.
You don't need the C++/CLI library is this case, pinning a multidimensional array is harder than just pinning a one-dimensional array as you need to pin the inner arrays manually.
If you do it with C#, the compiler does it automatically for you.
With C#, you can do this:
[DllImport('legacylibrary.dll')]
public static extern int LegacyGetVariableValues(double[][] timeValues);
For further reading, check http://msdn.microsoft.com/en-us/library/fzhhdwae(v=vs.110).aspx
Edit:
As you need the C++/CLI layer, you need to do something like this:
ref class Legacy {
public:
int GetVariableValues(array<array<double> ^> ^values) {
array<GCHandle> ^handles = gcnew array<GCHandle>(values->Length);
double **valuesPtr = calloc(sizeof(double *), values->Length);
int result;
for (int i = 0; i < values->Length; i++) {
handles[i] = GCHandle::Alloc(values[i]);
valuesPtr[i] = (double *)GCHandle::ToIntPtr(handles[i]).GetPointer();
}
result = LegacyGetVariableValues(valuesPtr);
for (int i = 0; i < values->Length; i++) {
handles[i].Free();
}
return result;
}
}
PS: Don't know if the syntax is completely correct as I don't write C++/CLI in a long time.

Pass array from c++ library to a C# program

I'm making a dll in c++ and I want to pass an array to a c# program. I already managed to do this with single variables and structures. Is it possible to pass an array too?
I'm asking because I know arrays are designed in a different way in those two languages and I have no idea how to 'translate' them.
In c++ I do it like that:
extern "C" __declspec(dllexport) int func(){return 1};
And in c# like that:
[DllImport("myDLL.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "func")]
public extern static int func();
Using C++/CLI would be the best and easier way to do this.
If your C array is say of integers, you would do it like this:
#using <System.dll> // optional here, you could also specify this in the project settings.
int _tmain(int argc, _TCHAR* argv[])
{
const int count = 10;
int* myInts = new int[count];
for (int i = 0; i < count; i++)
{
myInts[i] = i;
}
// using a basic .NET array
array<int>^ dnInts = gcnew array<int>(count);
for (int i = 0; i < count; i++)
{
dnInts[i] = myInts[i];
}
// using a List
// PreAllocate memory for the list.
System::Collections::Generic::List<int> mylist = gcnew System::Collections::Generic::List<int>(count);
for (int i = 0; i < count; i++)
{
mylist.Add( myInts[i] );
}
// Otherwise just append as you go...
System::Collections::Generic::List<int> anotherlist = gcnew System::Collections::Generic::List<int>();
for (int i = 0; i < count; i++)
{
anotherlist.Add(myInts[i]);
}
return 0;
}
Note I had to iteratively copy the contents of the array from the native to the managed container. Then you can use the array or the list however you like in your C# code.
You can write simple C++/CLI wrapper for the native C++ library. Tutorial.
You could use Platform Invoke. This will definitely be simpler if you have only one array to pass. Doing something more complicated might be impossible though (such as passing nontrivial objects). Documentation.
In order to pass the array from C++ to C# use CoTaskMemAlloc family of functions on the C++ side. You can find information about this on
http://msdn.microsoft.com/en-us/library/ms692727
I think this will be suffice for your job.

Unmanaged C code in C# Marshalling by ref string array!

I am having a really hard time getting this marshalling down.
I have umanaged code that looks like this:
WORD HLP_GetDeviceNames (LPSTR *DevNames, WORD Max_Len, WORD Max_Num)
Just FYI I did not write this unmanaged code but must use it.
Returns: WORD indicating an error.
DevNames: Pointer to an array of char arrays. Basically an array of strings that will be modified and returned back to me!
Max_Len: Length of each string (I am told this must be 256)
Max_Num: Length of array. I am using another Invoke call that is working that tells me number of devices so i know exactly how many strings to send.
I have used P/Invoke interop signatureToolkit to figure alot of this out but also read a bunch to get even further. Where I am now is here:
[DllImport("UsbMeasLib.dll")]
public static extern ushort HLP_GetDeviceNames([MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] ref StringBuilder[] DevNames, ushort Max_Len, ushort Max_Num);
I call my code like this:
StringBuilder[] DevNames = new StringBuilder[deviceCount];
for(int i = 0; i< deviceCount; i++)
{
DevNames[i] = new StringBuilder().Append(' ', 256);
}
HachUsbMeasLib.HLP_GetDeviceNames(ref DevNames, 256, Convert.ToUInt16(DevNames.Count()));
I am using string builder array because I need the unmanaged code to modify the string builder so that it can return the new string since string is unmutable.
When I run the code, My array is unmodified!
I'm not really sure what is going on but I think it has something to do with CLR telling unmanaged code to not modify my array in place but instead creates a new reference(pointer). Even if this is the case, I dont know how to fix it.
Thanks for any insight anybody can offer!
Try to work on low level. Declare DevNames parameter as IntPtr[]. Prepare it by the following way:
IntPtr[] devNames = new IntPtr[deviceCount];
for(int i = 0; i < deviceCount; i++)
{
devNames[i] = Marshal.AllocHGlobal[256];
}
Pass this array to HLP_GetDeviceNames. To handle output data, apply Marshal.PtrToStringAnsi to every DevNames member. Don't forget to release DevNames[i] with Marshal.FreeHGlobal in the end.
I figured this one out. Thanks to anybody who replied.
I found out how it works. I simply supply the memory space but I have to let the marshaling know that I expect in and out with this object so it allows the unmanaged code to modify the allocated space.
I did it like this:
[DllImport("UsbMeasLib.dll")]
private static extern ushort HLP_GetDeviceNames([In, Out, MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] string[] DevNames, ushort Max_Len, ushort Max_Num);
I use string instead of string builder because the unmanaged code will simply replace the string which is ok. I am getting the array pointer back, not modified strings. the managed code is just changing an array of pointers to point to new string objects (I think).
int numDev = HLP_GetNumDevices();
string[] names = new string[numDev];
for (int i = 0; i < names.Length; i++)
{
names[i] = new StringBuilder().Append(' ', 256).ToString();
}
ushort errorCode = HLP_GetDeviceNames(names, 256, Convert.ToUInt16(numDev));
I allocate memory for the unamanged code then let the unmanaged code chane the strings there.
This works but I dont know if I have any potential memory leaks or other potential problems.

How do I change the address of a New struct in a loop?

I'm writing a simple program which is about polynomials using linked lists in C#. The problem I have is that whenever it creates a new struct (node) in the for loop it gives it the same address as the previous node was given. How do I fix that? Here is my struct:
struct poly { public int coef; public int pow; public poly* link;} ;
And here is where the problem occurs:
for (; i < this.textBox1.Text.Length; i++)
{
q = new poly();
...
p->link = &q;
}
But &q remains unchanged!
Update:
In order to clarify it more, here is the full code:
namespace PolyListProject
{
unsafe public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
struct poly { public int coef; public int pow; public poly* link;} ;
poly *start ;
poly *p;
private void button1_Click(object sender, EventArgs e)
{
string holder = "";
poly q = new poly();
start = &q;
int i = 0;
while (this.textBox1.Text[i] != ',')
{
holder += this.textBox1.Text[i];
i++;
}
q.coef = int.Parse(holder);
i++;
holder = "";
while (this.textBox1.Text[i] != ';')
{
holder += this.textBox1.Text[i];
i++;
}
q.pow = int.Parse(holder);
holder = "";
p = start;
//creation of the first node finished!
i++;
for (; i < this.textBox1.Text.Length; i++)
{
q = new poly();
while (this.textBox1.Text[i] != ',')
{
holder += this.textBox1.Text[i];
i++;
}
q.coef = int.Parse(holder);
holder = "";
i++;
while (this.textBox1.Text[i] != ';'&& i < this.textBox1.Text.Length-1)
{
holder += this.textBox1.Text[i];
if (i < this.textBox1.Text.Length-1)
i++;
}
q.pow = int.Parse(holder);
holder = "";
p->link = q;
}
p->link = null;
}
}
}
Our professor asked us to do it in C but we decided to do it in C# yet giving it a C look, since no one actually uses C anymore.
Okay, since you're definitely using C++, not C#, I'll answer in terms of C++.
In this function, the q variable is (I'm assuming), a pointer that's local to this function. That means its address is NOT going to change.
The problem is that you're assigning the address of a pointer to p->link. Since new poly() returns a poly* already (which IS an address!) you don't need the address.
Try this:
q = new poly();
...
p->link = q;
Problem solved :) like this : (but q is a pointer instead)
IntPtr newP = Marshal.AllocHGlobal(sizeof(poly));
poly* q = (poly*)newP.ToPointer();
// ......
p->link = q;
The problem with &q, is that the structure instance q lives on the execution stack while the method is running. Even though you use the new() syntax, the structure is still on the stack. As such, the address is always the same (and will become invalid when you return from the function.) If you want to get a pointer to a structure on the heap (not the GC heap, but a special unmanaged memory area), you need to use AllocHGlobal to allocate memory and then cast the IntPtr to (poly*). This is unmanaged memory, so you'll also need to remember to free it.
In general, I think it's a very bad idea to try to use C# in this way, as it's confusing to C# and C++ programmers alike. Unsafe syntax is useful in very rare border cases where you need fast access to underlying memory or for certain inter op scenarios. Using it to implement a data structure with C-style pointers is just plain wrong.
Here's a concrete example, in case the above is unclear. The operation q = new poly(); simply replaces the contents of q (a local variable on the stack) with a newly-initialized poly(). This is more akin to clearing memory than it is to allocating a new instance. The reason it's confusing is because, in C++, there is no difference between struct and class when it comes to allocation. A new() in C++ is always allocation on the heap. In C#, the allocation location is generally determined by the type, not the usage, so when you call new() on a value type (struct), this is shorthand for initializing it, not allocating memory for it on the heap.
You asked a question about 'fixed' and there's a very good example of why you shouldn't use unsafe C# thinking it is basically the same as C. In C#, references are not the same as pointers. One of the big difference is that, since C# references are garbage collected, the GC could, at almost any time, decide to hijack your program's execution and replace all the references to point to new memory locations. If you use unsafe code that's referring to referenced memory via a pointer, the object referenced by the pointer may move without the pointer being updated. To address this, you can mark a specific instance as 'fixed' so that the GC won't move it. The C# compiler is trying to protect you from yourself.

Categories

Resources