Recent

Author Topic: 3 Diffrent Questions, Compiler Directives and Code  (Read 9893 times)

KillerCode

  • New Member
  • *
  • Posts: 29
3 Diffrent Questions, Compiler Directives and Code
« on: November 17, 2010, 02:14:02 pm »
i was just hoping i can get some help with those
1.
im trying to cross-compile my code between delphi and lazarus, so i used {$MODE DELPHI}, and it works fine without changing much code, but, is this ok? i mean is there any side effects from doing this?

2.
my variables are already being intialized, not directly, is there anyway lazarus can detect this?
image in attachments

3.
this code works fine inside delphi, but not inside lazarus, no errors, no warning, no hints. it simply shows wrong result, eventho same delphi code with same everything even parameters works :S
Code: [Select]
function GetFileInfo(const FileName, BlockName: String): String;
var
 dwSize, lpdwHandle: DWORD;
 lpData, lpTranslation, lplpBuffer: Pointer;
const
 VarFileInfoTraslation = '\\VarFileInfo\\Translation';
 VarStringFileInfo = '\\StringFileInfo\\%.4x%.4x\\%s';
begin
 try
  Result := '';
  dwSize := GetFileVersionInfoSize(PAnsiChar(FileName), lpdwHandle);
  if dwSize <> 0 then
  begin
   GetMem(lpData, dwSize);
   GetFileVersionInfo(PAnsiChar(FileName), 0, dwSize, lpData);
   if lpData <> nil then
   begin
    VerQueryValue(lpData, VarFileInfoTraslation, lpTranslation, dwSize);
    if lpTranslation <> nil then
    begin
     VerQueryValue(lpData, PAnsiChar(Format(VarStringFileInfo, [LOWORD(LongInt(lpTranslation^)), HIWORD(LongInt(lpTranslation^)), BlockName])), lplpBuffer, dwSize);
     if lplpBuffer <> nil then
     Begin
      SetString(Result, PAnsiChar(lplpBuffer), dwSize -1);
      Result := PAnsiChar(Result);
     end;
    end;
   end;
   FreeMem(lpData, dwSize);
  end;
 except
  Result := '';
 end;
end;
what am i missing?
« Last Edit: November 17, 2010, 02:15:44 pm by KillerCode »

Laksen

  • Hero Member
  • *****
  • Posts: 662
    • J-Software
Re: 3 Diffrent Questions, Compiler Directives and Code
« Reply #1 on: November 17, 2010, 03:18:06 pm »
i was just hoping i can get some help with those
1.
i mean is there any side effects from doing this?
There shouldn't be :)
What sort of side effects are you thinking about

Quote
2.
my variables are already being intialized, not directly, is there anyway lazarus can detect this?
image in attachments
There isn't a lot to do. You can disable the hint with a compiler directive, but otherwise just ignore it. That question has been discussed a lot over the years, and it's not going to change

Quote
3.
this code works fine inside delphi, but not inside lazarus, no errors, no warning, no hints. it simply shows wrong result, eventho same delphi code with same everything even parameters works :S
Works fine for me in Lazarus 0.9.29 in Windows 7 32bit. What platform are you trying it on?

KillerCode

  • New Member
  • *
  • Posts: 29
Re: 3 Diffrent Questions, Compiler Directives and Code
« Reply #2 on: November 17, 2010, 05:32:53 pm »
1. no idea, i thought it was some kind of cheat, so is it just some kinda of syntax handling? like the diffrence between AT&T and Intel ASM Syntax?

2. ok, thanks for the info :D

3. Windows XP with SP2, but i dont think the os is the problem... i guess its my code, cause i just testedon new pure lazarus/fp project and it works fine, thanks anyway :)

ivan17

  • Full Member
  • ***
  • Posts: 173
Re: 3 Diffrent Questions, Compiler Directives and Code
« Reply #3 on: November 17, 2010, 07:52:51 pm »
as for question one, you can find the documentation on compiler directives here. in short you choose between two modes:

{$MODE DELPHI}
and
{$MODE OBJFPC} {$H+}  

delphi mode is intended for compatibility with delphi which means - nice relaxing drive we're used to.

objfpc mode is a bothersome "modern" mode which uses standards as an excuse to irritate you:
  • function declarations in implementation must be repeated exactly as defined in forward declaration or class declaration (not too much of a bother for me, but it sometimes makes you lose time and nerves fixing an otherwise perfectly good old code)
  • long strings are off by default (WTF is with that?) meaning you must add {$H+} or {$LONGSTRINGS ON} (same thing) at the top or each unit or your variables using string type will become turbo-pascal style shortstring variables.
  • you need to write @ in statements like Edit1.OnChange := SomeMethod; (ok, i don't really mind that one)
  • parameters in class methods cannot have the same names as class properties (WHICH IDIOT CAME UP WITH THAT?!? now i can't write function TMainForm.FillHistoryMenu(popupmenu: TPopupMenu)) because forms have a popupmenu property which NOBODY ever uses. and even if you wanted, it is separately available as Self.PopupMenu so there is nothing preventing you from assigning one to the other or whatever.)

KillerCode

  • New Member
  • *
  • Posts: 29
Re: 3 Diffrent Questions, Compiler Directives and Code
« Reply #4 on: November 18, 2010, 12:44:04 am »
thats was some good punch of info :D
thanks guys   ;)

KillerCode

  • New Member
  • *
  • Posts: 29
Re: 3 Diffrent Questions, Compiler Directives and Code
« Reply #5 on: November 18, 2010, 06:30:57 am »
ok, its my Format function which isnt working, any idea how to fix it? i need this one working cause im trying to avoid large exe size, thx

Code: [Select]
function ____Format(sFormat: String; Args: Array of const): String;
var
  i: Integer;
  pArgs1, pArgs2: PDWORD;
  lpBuffer: PChar;
begin
  pArgs1 := nil;
  if Length(Args) > 0 then
    GetMem(pArgs1, Length(Args) * sizeof(Pointer));
  pArgs2 := pArgs1;
  for i := 0 to High(Args) do
  begin
    pArgs2^ := DWORD(PDWORD(@Args[i])^);
    inc(pArgs2);
  end;
  GetMem(lpBuffer, 1024);
  try
    SetString(Result, lpBuffer, wvsprintf(lpBuffer, PChar(sFormat), PChar(pArgs1)));
  except
    Result := '';
  end;
  if pArgs1 <> nil then
    FreeMem(pArgs1);
  if lpBuffer <> nil then
    FreeMem(lpBuffer);
end;

function GetFileInfo(const FileName, BlockName: String): String;
var
 dwSize, lpdwHandle: DWORD;
 lpData, lpTranslation, lplpBuffer: Pointer;
 outt: PAnsiChar;
const
 VarFileInfoTraslation = '\\VarFileInfo\\Translation';
 VarStringFileInfo = '\\StringFileInfo\\%.4x%.4x\\%s';
begin
 try
  Result := '';
  dwSize := GetFileVersionInfoSize(PAnsiChar(FileName), lpdwHandle);
  if dwSize <> 0 then
  begin
   GetMem(lpData, dwSize);
   GetFileVersionInfo(PAnsiChar(FileName), 0, dwSize, lpData);
   if lpData <> nil then
   begin
    VerQueryValue(lpData, VarFileInfoTraslation, lpTranslation, dwSize);
    if lpTranslation <> nil then
    begin
     VerQueryValue(lpData, PAnsiChar(____Format(VarStringFileInfo, [LOWORD(LongInt(lpTranslation^)), HIWORD(LongInt(lpTranslation^)), BlockName])), lplpBuffer, dwSize);

     Messagebox(0,  PAnsiChar(____Format(VarStringFileInfo, [LOWORD(LongInt(lpTranslation^)), HIWORD(LongInt(lpTranslation^)), BlockName])), 'Sub', 0);

     if lplpBuffer <> nil then
     Begin
      SetString(Result, PAnsiChar(lplpBuffer), dwSize -1);
      Result := PAnsiChar(Result);
     end;
    end;
   end;
   FreeMem(lpData, dwSize);
  end;
 except
  Result := '';
 end;
end;   

ivan17

  • Full Member
  • ***
  • Posts: 173
Re: 3 Diffrent Questions, Compiler Directives and Code
« Reply #6 on: November 18, 2010, 09:36:07 am »
ok, its my Format function which isnt working, any idea how to fix it? i need this one working cause im trying to avoid large exe size, thx
what you tried wouldn't reduce the size one slightest bit. just use normal function from SysUtils unit (which you need anyway) and when you're done, open the console, go to the project dir and type strip myexefilename and then type upx myexefilename

i got those two as items in the tools menu. when i'm done, 15mb->0.7mb without leaving the ide.

KillerCode

  • New Member
  • *
  • Posts: 29
Re: 3 Diffrent Questions, Compiler Directives and Code
« Reply #7 on: November 18, 2010, 10:08:15 am »
actually i did, im not using classes, sysutils or forms and it works :D, i tried to strip this function off sysutils file but its huge :(
if u can help me fixing this function would be appreciated :D

ivan17

  • Full Member
  • ***
  • Posts: 173
Re: 3 Diffrent Questions, Compiler Directives and Code
« Reply #8 on: November 18, 2010, 02:46:59 pm »
keeping forms out (and dialogs and the rest of LCL) is a good idea. classes and sysutils are part of RTL which you don't need to keep out.  in fact, sysutils unit has some invisible sideeffects which you probably need.

and no, i don't think anyone here will bother with that format-mess.  if you want things bare and simple, use IntToStr/FloatToStr and the rest of that stuff. if you want a heavy-hitter function like format, include sysutils unit.

Laksen

  • Hero Member
  • *****
  • Posts: 662
    • J-Software
Re: 3 Diffrent Questions, Compiler Directives and Code
« Reply #9 on: November 18, 2010, 10:08:11 pm »
Maybe something like this:
'\\StringFileInfo\\'+HexStr(LoValue, 4)+HexStr(HiValue, 4)+'\\+BlockName;

HexStr is a system unit function
« Last Edit: November 18, 2010, 10:11:38 pm by Laksen »

 

TinyPortal © 2005-2018