0

I have picked up this piece of code but as I understand it is unsafe code to use and may not be possible in WP7. Does anyone have an idea about how I can make this code safe to use, maybe with try/catch?

GCHandle gch = GCHandle.Alloc(waveInBytes,GCHandleType.Pinned);
int pPtr;

unsafe
{
    pPtr = (int)gch.AddrOfPinnedObject().ToPointer();
    pPtr += 4;
}
Josh Mein
  • 26,372
  • 12
  • 72
  • 84
Jason94
  • 12,424
  • 33
  • 99
  • 173
  • 4
    What are you trying to accomplish? Perhaps there's a different way altogether. – Ani Aug 01 '12 at 20:51
  • It seems to be a good example for [XY-problem](http://www.perlmonks.org/?node_id=542341) – L.B Aug 01 '12 at 21:02
  • 1
    This code doesn't make sense in WP7, you can't run native/unmanaged code there at all without quite a bit of work and I think you need special permission from Microsoft before it can go in the App Marketplace. – EkoostikMartin Aug 01 '12 at 20:54
  • @ananthonline: looks like he's doing some low level audio fun. Probably not capable on WP7, unless maybe using directsound. –  Aug 01 '12 at 21:09
  • @L.B - I call it the "thin metal ruler" (by Eric Lippert) http://blogs.msdn.com/b/ericlippert/archive/2003/11/03/a-parable.aspx – Ani Aug 01 '12 at 21:10

1 Answers1

1

Pointers in general are unsafe. IntPtr.ToPointer() returns a void* type, which is what's causing the error. try/catch doesn't help.

Depending on the larger context of what you're trying to do, it may work to just change the ToPointer() call to be ToInt32(). Realistically you're likely going to have more problems than just this compiler error with trying to convert this kind of code to WP7.

Hope that helps, anyways.

Joe Castro
  • 2,101
  • 18
  • 24