Recent

Author Topic: Porting Delphi to WinCE  (Read 15263 times)

Anonymous

  • Guest
Porting Delphi to WinCE
« on: July 08, 2006, 08:59:56 pm »
Hello,

I just finished porting a Delphi 5 project (app. 5000 lines, nearly no GUI) to WinCE.
It took me the most time, to get a running FPC/Lazarus configuration. Porting the application was real fun :-)

Here are some points, which could be usefull for others:

I needed a random generator, which produces the same numbers as Delphi 5. The FPC random generator doesn't. I am not used to ARM assembler yet, so the assembler _RandInt Delphi procedure did not help me. I found the following, portable code, which seems to work perfect. The orignal author of the code is Robert Rossmair, I found it in de.comp.lang.delphi.non-tech:

Code: [Select]
function MUL(N, M: Cardinal): Int64;
var
  i: Integer;
  Temp: Int64;
begin
  Result := 0;
  Temp := N;
  for i := 0 to 31 do
  begin
    if (M and 1) = 1 then // Bit i in M gesetzt
      Result := Result + Temp;
    Temp := Temp shl 1;
    M := M shr 1;
  end;
end;

function myrand(range:integer):integer;
var
  edx : Integer;
begin
  edx := RandSeed * $08088405;
  Inc(edx);
  RandSeed := edx;
  Result := MUL(edx, Range) shr 32;
end;


The next thing, which took me some time, was opening a serial COM port. In Win32 one calls CreateFile('COM1', ...);  in WinCE it is CreateFile('COM1:', ...);

Last but not least: I had to replace my GlobalAlloc, GlobalLock, GlobalUnlock and GlobalFree calls with the corresponding LocalAlloc, LocalLock, LocalUnlock and LocalFree calls.

Regards, Bernd.

glober

  • Full Member
  • ***
  • Posts: 130
Porting Delphi to WinCE
« Reply #1 on: September 04, 2006, 08:01:15 pm »
Bernd,  I'm am trying to convert some Delphi code to Laz/Linux and have run into the GlobalAlloc, GlobalLock, GlobalUnlock and GlobalFree functions. You say you have replaced them with LocalAlloc, LocalLock, LocalUnlock and LocalFree, yet these don't seem to exist under FPC/Lazarus. How did you use these ? Can you clarify please.

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Porting Delphi to WinCE
« Reply #2 on: September 04, 2006, 10:56:56 pm »
Quote from: "glober"
You say you have replaced them with LocalAlloc, LocalLock, LocalUnlock and LocalFree, yet these don't seem to exist under FPC/Lazarus. How did you use these ? Can you clarify please.


These functions are part of the Windows API, therefore they are only available on Windows platforms such as WinCE on the first post.

Try to substitute those functions with platform independent ones available on the Pascal Run-time Library. Here is a list:

http://www.freepascal.org/docs-html/rtl/system/memoryfunctions.html

The most used ones are GetMem and FreeMem

glober

  • Full Member
  • ***
  • Posts: 130
Porting Delphi to WinCE
« Reply #3 on: September 05, 2006, 12:59:19 am »
Duh... I think I should actually read the posts, as in, the post is about WinCE which when I actually use my brain I know is not Linux.

Regarding GetMem and FreeMem, I have tried these, but a simple substitution does not work, so if there is nothing else more similar available, it looks like I'll have to grind away at it to get it to work. BTW the code I'm converting is here: http://community.borland.com/article/0,1410,16412,00.html

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Porting Delphi to WinCE
« Reply #4 on: September 05, 2006, 03:27:38 am »
Quote from: "glober"
Regarding GetMem and FreeMem, I have tried these, but a simple substitution does not work,


Why not? Both just alloc memory, shouldn´t matter the method you use to get memory.

Quote
so if there is nothing else more similar available, it looks like I'll have to grind away at it to get it to work. BTW the code I'm converting is here: http://community.borland.com/article/0,1410,16412,00.html


Ok, looking at the code I can see you have a very big problem.

That code is *extremely* Windows API dependent. You should consider reimplementing it for Linux on a different way. You can do that with IFDEFs:

{$ifdef Win32}
  put here that code
{$endif}
{$ifdef Unix}
  put here new Unix Code
{$endif}

And then use something that works on Linux on the second part, like the TPrinter object perhaps. Or research about printing on Linux.

_Bernd

  • New Member
  • *
  • Posts: 20
Porting Delphi to WinCE
« Reply #5 on: September 08, 2006, 10:25:28 am »
BTW, I modified the random generator procedure:

Code: [Select]
function MyRand(iRange: Integer): Longint;
{
   Random generator, compatible to Delphi 5.
}
var
  edx: Integer;
  lResult: Longint;
begin
  edx:= RandSeed * $08088405;
  Inc(edx);
  RandSeed:= edx;
  lResult:= Longint((Int64(edx) * Int64(iRange)) shr 32);
  if lResult < 0 then
     lResult:= iRange + lResult;
  MyRand:= lResult;
end;


This procedure is now about 3 times faster than the version, I posted above. I used {$mode delphi} and switched the checking options {$Q-,R-} off.

Regards, Bernd.

 

TinyPortal © 2005-2018