Visual Studio Developer Center > Visual Studio Forums > Visual Studio Extensibility > Refering a IntPtr passed from Visual Studio Text Editor is throwing Exception in AddIN
Ask a questionAsk a question
 

Proposed AnswerRefering a IntPtr passed from Visual Studio Text Editor is throwing Exception in AddIN

  • Wednesday, October 28, 2009 10:29 AMVicky2006 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,
        

    EnvDTE.

    Expression getPixelsAsPointer = applicationObject.Debugger.GetExpression("pixelIntPtr", true,5);
    pixelIntPtr is a IntPtr refering to a byte array.

    I will get the IntPtr by the following way

     

    IntPtr pixPointer = (IntPtr)Convert.ToInt32(getPixelsAsPointer.Value);

     

    unsafe

    {

    byte* ptr=(byte*)pixPointer.ToPointer(); //Getting Exception Here

    for (int iIndex = 0; iIndex < height; iIndex++)
    {

     pixData[iIndex] =*ptr;

     ptr = ptr + 1;

     }

    }

    Whetn i try to iterate through the byte array i am getting Exception:AccessViolationException
    Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

    And i also want to know whether the AddIn and Visual Studio Text Editor are in same process?
    And how to use IntPtr in AddIn?
    Please let me know how to solve this issue.

     

All Replies

  • Wednesday, November 04, 2009 4:49 AMVicky2006 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    Does anyone knows how to solve this .
    Please let me know is anything wrong in the above code.
    Thanks and Regards.
  • Wednesday, November 04, 2009 10:46 AMNancy ShaoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi Vicky,

    Sorry for the delay.

    I have a test in my side, it's no problem to use Debugger.GetExpression to get IntPtr type. I think there is some wrong with getPixelsAsPointer.Value in your code. Maybe this sentence throw exception, please check it. For your reference, I post my test sample here, hope this helps.

    Firstly, I write a AddIn, adding following code in Exec function:

     EnvDTE.Expression getPixelsAsPointer = _applicationObject.Debugger.GetExpression("pixPointer",true,100);
    


    Second, I create a C# console project with unsafe code, below is the code:

            static void Main(string[] args)
            {
                int var1 = 5;      
    
                unsafe
                {
    
                    int* ptr1, ptr2;              
    
                     ptr1 = &var1;
    
                     ptr2 = ptr1;
    
                    *ptr2 = 20;
    
                     IntPtr pixPointer = (IntPtr)Convert.ToInt32(*ptr2);
    
                }                     
                
    


    And then put a breakpoint after expression, run my AddIn, and will get the expression.

    Please let me know if you have any questions.

    Best Regards,
    Nancy
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Wednesday, November 04, 2009 4:56 PMEd DoreMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer
    Hi Vicky,

    The problem here is that your addin is running within the devenv.exe process, so that IntPtr value has no intrisic meaning in that context. The IntPtr value specific to the debugee, which is a different process than the one your addin code is being run from. I'm not sure if there is way to do this from managed code, unless you can pass an expression that dereferences the IntPtr into a format you want. Both EEAddins and Debugger visualizers can get at this info, so I don't think it's impossible. But I'm unfamiliar with the plumbing that would be required. I'd take a look at the debugger visualizer stuff and see how they pull the data from the debuggee and see if you can do something similar.

    Sincerely,
    Ed Dore
  • Thursday, November 05, 2009 8:43 AMVicky2006 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Nancy Shao,

             Thanks for the reply. I tried your example it works fine if i pass the value i.e *ptr2.
    But if i pass ptr2 and try to get the value it gives same exception as mentioned in my first post.
    As in my case i need to read the array(2D/1D) it is not a good idea to pass the values, instead what i thought was
    if i pass the IntPtr of the array it would be easy to retrieve the values.

    The error might be coming because both addIn and Debugger are runnning in different processes as mentioned by ED Dore.

    Is there any alternative way to get array values from debugger.

    Thanks,