Pinning a fixed array of strings and getting char* for each index

37 views Asked by At

I have to pass a pointer of type char* to a structure in C# and I can successfully do it with this code:

  string myText = "Hello World";
  fixed (char* ptrText = myText) {
    lvItem.pszText = ptrText;
    ....
  }

However, I have to do this 20 times, and I would like to avoid pinning/fixing all 20 strings in a loop and instead populate them in an array of strings and just pin that array (seems more efficient), but I cannot get the part of pinning the whole array and then getting the address of each element as a char* pointer.

  string[] arrStrings = new string[20];
  fixed (string* ptrArray = arrStrings) {
     lvItem.pszText = &ptrArray[1]    // this doesn't work
     ....
  }

Anyone knows how is this done in unsafe context in C#?

0

There are 0 answers