Click here to Skip to main content
15,881,812 members
Home / Discussions / C#
   

C#

 
QuestionRe: How to pin List to unmanaged byte** Pin
Chesnokov Yuriy19-Jan-11 1:25
professionalChesnokov Yuriy19-Jan-11 1:25 
AnswerRe: How to pin List to unmanaged byte** Pin
DaveyM6919-Jan-11 2:40
professionalDaveyM6919-Jan-11 2:40 
AnswerRe: How to pin List to unmanaged byte** Pin
Chesnokov Yuriy19-Jan-11 3:00
professionalChesnokov Yuriy19-Jan-11 3:00 
QuestionRe: How to pin List to unmanaged byte** Pin
Chesnokov Yuriy19-Jan-11 4:13
professionalChesnokov Yuriy19-Jan-11 4:13 
AnswerRe: How to pin List to unmanaged byte** Pin
DaveyM6919-Jan-11 5:43
professionalDaveyM6919-Jan-11 5:43 
AnswerRe: How to pin List to unmanaged byte** Pin
Chesnokov Yuriy19-Jan-11 9:28
professionalChesnokov Yuriy19-Jan-11 9:28 
GeneralRe: How to pin List to unmanaged byte** Pin
DaveyM6919-Jan-11 9:31
professionalDaveyM6919-Jan-11 9:31 
AnswerRe: How to pin List to unmanaged byte** [modified] Pin
DaveyM6919-Jan-11 8:48
professionalDaveyM6919-Jan-11 8:48 
Try this...

Change the PInvoke method signature to take a System.IntPtr.
The following method isn't very efficient as it takes two iterations: 1 to determine the memory size required; 2 to copy the data to unmanaged memory, but it should work.
C#
public static IntPtr GetPointerToListOfByteArrays(List<byte[]> byteArrayList)
{
    int size = 0;
    foreach (byte[] byteArray in byteArrayList)
        size += byteArray.Length;
    IntPtr pointer = Marshal.AllocHGlobal(size);
    IntPtr counter = pointer;
    foreach (byte[] byteArray in byteArrayList)
    {
        Marshal.Copy(byteArray, 0, counter, byteArray.Length);
        counter += byteArray.Length;
    }
    return pointer;
}

It's important that the allocated memory is freed when done so either call Marshal.FreeHGlobal on the pointer returned above or add this helper method and call it passing the pointer.
C#
public static void FreePointerToListOfByteArrays(IntPtr pointer)
{
    Marshal.FreeHGlobal(pointer);
}

Edit: Just realized this will layout the bytes themselves in memory, not pointers to the arrays - will fix and repost!
See next post.
Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



modified on Wednesday, January 19, 2011 3:23 PM

AnswerRe: How to pin List to unmanaged byte** Pin
DaveyM6919-Jan-11 9:23
professionalDaveyM6919-Jan-11 9:23 
AnswerRe: How to pin List to unmanaged byte** Pin
Chesnokov Yuriy19-Jan-11 9:35
professionalChesnokov Yuriy19-Jan-11 9:35 
GeneralRe: How to pin List to unmanaged byte** Pin
DaveyM6919-Jan-11 9:41
professionalDaveyM6919-Jan-11 9:41 
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 
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 

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.