Click here to Skip to main content
15,888,113 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to pin List to unmanaged byte** Pin
Chesnokov Yuriy19-Jan-11 10:04
professionalChesnokov Yuriy19-Jan-11 10:04 
GeneralRe: How to pin List to unmanaged byte** Pin
DaveyM6919-Jan-11 10:14
professionalDaveyM6919-Jan-11 10:14 
AnswerRe: How to pin List to unmanaged byte** Pin
Chesnokov Yuriy19-Jan-11 19:02
professionalChesnokov Yuriy19-Jan-11 19:02 
GeneralRe: How to pin List to unmanaged byte** Pin
_Erik_19-Jan-11 3:06
_Erik_19-Jan-11 3:06 
GeneralRe: How to pin List to unmanaged byte** Pin
Chesnokov Yuriy19-Jan-11 4:18
professionalChesnokov Yuriy19-Jan-11 4:18 
GeneralRe: How to pin List to unmanaged byte** Pin
_Erik_19-Jan-11 5:10
_Erik_19-Jan-11 5:10 
AnswerRe: How to pin List to unmanaged byte** Pin
Chesnokov Yuriy19-Jan-11 9:45
professionalChesnokov Yuriy19-Jan-11 9:45 
GeneralRe: How to pin List to unmanaged byte** [modified] Pin
_Erik_19-Jan-11 10:55
_Erik_19-Jan-11 10:55 
I've been lucky and have found it again. Here is a link[^] to the msdn documentation for this. See how they import the TestMatrixOfInts function.

I don't have time to make tests, but I am trying to help you. However, I won't be able to do so if you discard my suggestions before giving them a try. I know it may seem wrong at a first glance, but anyway a .NET bidimensional array might be internally implemented with an array of arrays.

Anyway, up to this moment I had understood that all of your byte[] arrays were the same size, but now I am not sure if that premise is correct. Is it?


Edit:
I wrote this before reading the part of the conversation with Dave about this topic. Now I have a better picture of it. The byte[] arrays are not of the same size, so a bidimensional array would not be helpful.

You have suggested stackalloc, but that is not what I recommend you. It allocates the memory in the stack, not in the heap, and that memory will be released when the method ends. Dave gave you a good advise with the IntPtr[] array, but instead of making Marshal.AllocHGlobal and Marshal.Copy you can use GCHandle, so you would not have to make a copy of the arrays into the global heap. Something like this:

C#
int CallUnmanagedMethod(List<byte[]> lst)
{
    GCHandle[] handleArray = new GCHandle[lst.Count];

    for (int i=0; i<lst.Count; i++)
        handleArray[i] = GCHandle.Alloc(lst[i], GCHandleType.Pinned);

    IntPtr[] pointers = (from GCHandle handle in handleArray select
                            handle.GetAddrOfPinnedObject()).ToArray();

    // Call the unmananged method here
    int ret = unmanagedMethod(pointers, ... // rest of the parameters

    // Release the handles
    foreach (GCHandle handle in handleArray)
        handle.Free();

    return ret;
}


This method would just pin into the managed heap the byte[] arrays you have in the list, without making any copy of them, then, using GCHandle, you build an IntPtr[] array to hold the address of each one and you can make the P/Invoke call.
AnswerRe: How to pin List to unmanaged byte** Pin
Chesnokov Yuriy19-Jan-11 19:12
professionalChesnokov Yuriy19-Jan-11 19:12 
GeneralRe: How to pin List to unmanaged byte** Pin
_Erik_20-Jan-11 2:37
_Erik_20-Jan-11 2:37 
AnswerRe: How to pin List to unmanaged byte** Pin
Chesnokov Yuriy19-Jan-11 4:27
professionalChesnokov Yuriy19-Jan-11 4:27 
AnswerRe: How to pin List<byte[]> to unmanaged byte** Pin
SledgeHammer0118-Jan-11 9:33
SledgeHammer0118-Jan-11 9:33 
AnswerRe: How to pin List to unmanaged byte** Pin
Chesnokov Yuriy18-Jan-11 19:08
professionalChesnokov Yuriy18-Jan-11 19:08 
GeneralRe: How to pin List to unmanaged byte** Pin
SledgeHammer0119-Jan-11 10:25
SledgeHammer0119-Jan-11 10:25 
AnswerRe: How to pin List to unmanaged byte** Pin
Chesnokov Yuriy19-Jan-11 19:03
professionalChesnokov Yuriy19-Jan-11 19:03 
GeneralRe: How to pin List to unmanaged byte** [modified] Pin
SledgeHammer0120-Jan-11 8:41
SledgeHammer0120-Jan-11 8:41 
QuestionOn Error Resume Next Pin
Anubhava Dimri17-Jan-11 18:24
Anubhava Dimri17-Jan-11 18:24 
AnswerRe: On Error Resume Next PinPopular
JF201517-Jan-11 18:31
JF201517-Jan-11 18:31 
GeneralRe: On Error Resume Next Pin
Anubhava Dimri17-Jan-11 19:09
Anubhava Dimri17-Jan-11 19:09 
GeneralRe: On Error Resume Next Pin
fjdiewornncalwe18-Jan-11 8:56
professionalfjdiewornncalwe18-Jan-11 8:56 
AnswerRe: On Error Resume Next Pin
Dave Kreskowiak17-Jan-11 18:59
mveDave Kreskowiak17-Jan-11 18:59 
AnswerRe: On Error Resume Next Pin
Abhinav S17-Jan-11 20:55
Abhinav S17-Jan-11 20:55 
AnswerRe: On Error Resume Next Pin
OriginalGriff17-Jan-11 21:20
mveOriginalGriff17-Jan-11 21:20 
GeneralRe: On Error Resume Next Pin
nlarson1118-Jan-11 3:43
nlarson1118-Jan-11 3:43 
GeneralRe: On Error Resume Next Pin
OriginalGriff18-Jan-11 4:00
mveOriginalGriff18-Jan-11 4:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.