Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Converting Delphi Win32 code to Delphi.NET with pointer arithmetic

Posted on 2008-10-14
5
Medium Priority
?
941 Views
Last Modified: 2013-11-23
I am trying to convert some Delphi Win32 code to Delphi.NET (in Delphi 2007). I am getting an error "E2089 Invalid typecast".

The function is taking a dib handle as a parameter and attempting to return a pointer to the image data bits. How do I do this properly in Delphi.NET?
{$UNSAFECODE ON}
function TEsImage.GetPtrToBits: Pointer; {$IFDEF CLR}unsafe;{$ENDIF}
var
  imageBits: Pointer;
  bitmapInfo: PBitmapInfo;
  numberOfColors: Integer;
begin
  bitmapInfo := PBitmapInfo(GlobalLock(FDIB));
  if Assigned(bitmapInfo) then begin
    { Get number of colors }
    if bitmapInfo.bmiHeader.biClrUsed > 0 then begin
      numberOfColors := bitmapInfo.bmiHeader.biClrUsed
    end else begin
      if (bitmapInfo.bmiHeader.biBitCount <> 24) and (bitmapInfo.bmiHeader.biBitCount <> 16) then begin
        numberOfColors := 1 shl bitmapInfo.bmiHeader.biBitCount;
      end else begin
        numberOfColors := 0;
      end;
    end;
 
    // THIS IS THE LINE GIVING THE TYPECAST COMPILER ERROR
    imageBits := Pointer(DWORD(bitmapInfo) +
                  SizeOf(BITMAPINFOHEADER) +
                  bitmapInfo*SizeOf(RGBQUAD));
 
    result := pointer(imageBits);
  end else begin
    result := Nil;
  end;
  GlobalUnLock(FDIB);
end;
{$UNSAFECODE OFF}

Open in new window

0
Comment
Question by:llanphear
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 6

Expert Comment

by:ChristianWimmer
ID: 22713647
I think you must convert bitmapInfo first as you did the first time.
imageBits := Pointer(DWORD(bitmapInfo) +
                  SizeOf(BITMAPINFOHEADER) +
                  DWORD(bitmapInfo)*SizeOf(RGBQUAD));

Open in new window

0
 

Author Comment

by:llanphear
ID: 22713793
Whoops I had an error in the line. Still does't work though
imageBits := Pointer(DWORD(bitmapInfo) +
                  SizeOf(BITMAPINFOHEADER) +
                  numberOfColors*SizeOf(RGBQUAD));

Open in new window

0
 
LVL 27

Accepted Solution

by:
BigRat earned 2000 total points
ID: 22721876
I don't understand this, because it looks as if the code has been marked unsafe!!!

Why not try incrementing the pointer?

   Inc(bitMapInfo,a+b*c);
0
 

Author Closing Comment

by:llanphear
ID: 31505993
Thank you. My biggest mistake was expecting a return result from Inc().
0
 

Author Comment

by:llanphear
ID: 22725849
Here is the final bit of code in case anyone was interested...
var
  pInfo: PByte;
  imageBits: PByte;
  bitmapInfo: PBitmapInfo;
  numberOfColors: Integer;
begin
  pInfo := PByte(GlobalLock(FDIB));
  bitmapInfo := PBitmapInfo(pInfo);
  if Assigned(pInfo) then begin
    if bitmapInfo.bmiHeader.biClrUsed > 0 then begin
      numberOfColors := bitmapInfo.bmiHeader.biClrUsed
    end else begin
      if (bitmapInfo.bmiHeader.biBitCount <> 24) and (bitmapInfo.bmiHeader.biBitCount <> 16) then begin
        numberOfColors := 1 shl bitmapInfo.bmiHeader.biBitCount;
      end else begin
        numberOfColors := 0;
      end;
    end;
    Inc(pInfo, SizeOf(BITMAPINFOHEADER) + numberOfColors * SizeOf(RGBQUAD));
    imageBits := pInfo;
 
    FData := Pointer(imageBits);
 
  end else begin
    FData := Nil;
  end; 
  GlobalUnLock(FDIB);
end;

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question