Click here to Skip to main content
15,892,927 members
Home / Discussions / C#
   

C#

 
AnswerRe: Application Opening again and again Pin
vivasaayi11-Nov-09 18:46
vivasaayi11-Nov-09 18:46 
AnswerRe: Application Opening again and again Pin
Giorgi Dalakishvili11-Nov-09 19:06
mentorGiorgi Dalakishvili11-Nov-09 19:06 
AnswerRe: Application Opening again and again Pin
Luc Pattyn12-Nov-09 2:46
sitebuilderLuc Pattyn12-Nov-09 2:46 
Questionclick combobox open windows form with datagridview Pin
vikas shukla11-Nov-09 18:28
vikas shukla11-Nov-09 18:28 
AnswerMessage Closed Pin
11-Nov-09 18:35
stancrm11-Nov-09 18:35 
GeneralRe: click combobox open windows form with datagridview Pin
vikas shukla11-Nov-09 20:25
vikas shukla11-Nov-09 20:25 
QuestionMarshalling array segment from C# to C++ Pin
dybs11-Nov-09 18:18
dybs11-Nov-09 18:18 
AnswerRe: Marshalling array segment from C# to C++ [SOLVED] Pin
dybs12-Nov-09 18:47
dybs12-Nov-09 18:47 
dybs wrote:
C#
unsafe
{  
  IntPtr unmanagedArr = Marshal.AllocHGlobal(arr.Length * sizeof(int));  
  Marshal.Copy(arr, 0, unmanagedArr, arr.Length);  
  for(int i = 0; i < length; i += stepSize)  
  {    
    // Ignore bounds checking for now, this is just conceptual.    
    foo(new IntPtr(unmanagedArr.ToInt64() + i), stepSize);  
  }  
  Marshal.Copy(unmanagedArr, arr, 0, arr.Length);  
  Marshal.FreeHGlobal(unmanagedArr);
}

Had some time to poke around a bit more and discovered the GCHandle class. My solution is
C#
int[] managedInput = SomeFunctionThatReturnsAnIntArray();
GCHandle arrayHandle = GCHandle.Alloc(managedInput, GCHandleType.Pinned);
IntPtr arrayPtr = arrayHandle.AddrOfPinnedObject();
long arrayAddr = arrayPtr.ToInt64();
for(int i =0 ; i < length; i += stepSize)
{
  foo(new IntPtr(arrayAddr + (i * sizeof(int))), stepSize);
}
arrayHandle.Free();


Not sure if the sizeof() part works the same on 32-bit and 64-bit systems, I'll be testing that tomorrow.

What I like about this solution is that I can pass in the address I get from my GCHandle and my managed arrays is directly updated. No calls to Marshal required! Hopefully I'm not missing anything like unsafe context or fixed statment (I think the GCHandleType.Pinned takes care of that). Hope this helps other people.

Dybs

The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen

Questionhow to abort the running multi Thread Pin
scoket11-Nov-09 16:46
scoket11-Nov-09 16:46 
AnswerMessage Closed Pin
11-Nov-09 18:15
stancrm11-Nov-09 18:15 
GeneralRe: how to abort the running multi Thread Pin
scoket11-Nov-09 19:28
scoket11-Nov-09 19:28 
AnswerRe: how to abort the running multi Thread Pin
Shameel11-Nov-09 22:34
professionalShameel11-Nov-09 22:34 
QuestionACOS3 -24 card reading Pin
AnasChavadi11-Nov-09 16:36
AnasChavadi11-Nov-09 16:36 
AnswerRe: ACOS3 -24 card reading Pin
Dave Kreskowiak11-Nov-09 16:52
mveDave Kreskowiak11-Nov-09 16:52 
QuestionComments for your code.. generating MSDN style files Pin
Jacob Dixon11-Nov-09 15:24
Jacob Dixon11-Nov-09 15:24 
AnswerRe: Comments for your code.. generating MSDN style files Pin
Christian Graus11-Nov-09 15:36
protectorChristian Graus11-Nov-09 15:36 
GeneralRe: Comments for your code.. generating MSDN style files Pin
PIEBALDconsult11-Nov-09 16:48
mvePIEBALDconsult11-Nov-09 16:48 
GeneralRe: Comments for your code.. generating MSDN style files Pin
Shameel11-Nov-09 22:37
professionalShameel11-Nov-09 22:37 
GeneralRe: Comments for your code.. generating MSDN style files Pin
PIEBALDconsult12-Nov-09 4:09
mvePIEBALDconsult12-Nov-09 4:09 
GeneralRe: Comments for your code.. generating MSDN style files Pin
Jacob Dixon12-Nov-09 3:06
Jacob Dixon12-Nov-09 3:06 
AnswerRe: Comments for your code.. generating MSDN style files Pin
Not Active11-Nov-09 17:01
mentorNot Active11-Nov-09 17:01 
GeneralRe: Comments for your code.. generating MSDN style files Pin
Jacob Dixon12-Nov-09 3:07
Jacob Dixon12-Nov-09 3:07 
AnswerMessage Closed Pin
11-Nov-09 18:38
stancrm11-Nov-09 18:38 
GeneralRe: Comments for your code.. generating MSDN style files Pin
Jacob Dixon12-Nov-09 3:00
Jacob Dixon12-Nov-09 3:00 
QuestionDataGridView Pin
Illegal Operation11-Nov-09 13:18
Illegal Operation11-Nov-09 13:18 

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.