Hello everyone, i'm porting an application from C# to C++, and i find the syntax and all that of C++ rather cumbersome (though C/C++ was my first language).
I have a very specific question, and another few more-or-less general ones.
The specific question:
In C#:

Bitmap image;
BitmapData ImageData;
ImageData=image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, image.PixelFormat); 
byte* walker = (byte*)ImageData.Scan0;
//Sift through the bytes using a for
for(int i=0; i< image.Height*ImageData.Stride;i++)
{
      string bit = Convert.ToString(*walker, 2);
      //Get the byte represented by walker, by dereferencing it
     //and convert the number (e.g. 235) to base 2, getting
     //a string of 1's and 0's.
      walker++;
    //Increment the pointer
}

Easy enough, huh?
Here's the C++ version, where i'm having difficulties:

Bitmap^ image;
BitmapData^ ImageData;
ImageData=image->LockBits(Rectangle(0,0,image->Width,image->Height),ImageLockMode::ReadWrite,image->PixelFormat);
IntPtr walker=ImageData->Scan0;
for(int i=0; i<Image->Height * ImageData-> Stride; i++)
{
        //1.How do i get the value at IntPtr?? Dereference does
       //not work.
       //2.How do i increment the pointer?
}

So my problems are, how do i increment IntPtr, and how do i get the value at its address (dereference) ?.
Thank you.
And now, general questions:
1. Why do i need to put the '^' sign when declaring stuff.

String^ myString;

2.How do i know when to use scope resolution operator :: or arrow sign -> ?
3.Why does this instruction:

if(!Regex::IsMatch(myString, "\[[0-9]{8}\]"))
{ //something }

give a warning that ']' and '[' are unrecognized escape characters? (in C# it worked...)
Thank you very much!

Recommended Answers

All 2 Replies

I haven't done a lot with C++/CLI but coming back from C# to the C++ I learned first it has been interesting to play around with this illegitimate child of C++ and .NET.

See this for question #2, it's quick but it will get you used to some of the terminology so you can search further. Essentially you are telling the system I want a "pointer" but I want you to keep track of it for me.

The answer to #3 is that go with your gut and let the intellisense help. Basically anytime you would be going from a namespace down to a class you need the :: and if the object's a pointer going down into the methods itself you need the -> operator. Some datatypes such as Point in the System:: Drawing, for example, are actually C# structs (which are not generally used with pointers in .NET for some reason) so in referencing the elements of one of those you need the dot operator.

I'm not precisely sure what to do about incrementing the pointer. I know there are ways to do it but they straddle the unmanaged bit. I can't really help you there.

EDIT: I was partly correct, you do need to use the Marshal class but I'm not sure which member. Again, wish I could be more help.

Hello
I not sure if you found an answer to your problem with "IntPtr"
I was looking for an answer myself and found this works.
Anyway I thought I would post this in case anyone else was looking for the answer.

Cheers
Milton

Bitmap^ image;
BitmapData^ ImageData;
ImageData=
image->LockBits(System::Drawing::Rectangle(0,0,image->Width,
image->Height),ImageLockMode::ReadWrite,
image->PixelFormat);
IntPtr walker=ImageData->Scan0;
for(int i=0; i<Image->Height * ImageData-> Stride; i++)
{
     //1.How do i get the value at IntPtr?? Dereference does       
     //not work.      
     //2.How do i increment the pointer?
     //TRY THIS
     Byte b = Marshal::ReadByte(walker, i) // walker = IntPtr to Scan0, i = offset
     //Marshal is an interop service so you will need to use:
     //System::Runtime:InteropServices
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.