Delphi E2029: Declaration expected but end of file found - how to debug? - delphi

Hi guys I have an Error that appeared and that I cant get rid off..
I added 2 custom procedures to my delphi code and I read that you can hit crtl+shift+c to autogenerate the functions, which I did.
However my problem now is that I didnt need the autogenerated stuff thats why I deleted it after executing the command. Now my code does not work anymore because of this error I am getting:
E2029 Declaration expected but end of file found
Expected INITIALIZATION but recieved the end of file at line 520(520:1)
How can I fixx my code? Removing or adding a 'end' at the end of the file does not help me. Is there a way to find out where something is missing in my code? (I could post my delphi code but its 500lines long I dont think that makes sense.
Update Code:
unit Benutzerverwaltung_U;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.ComCtrls,
Vcl.StdCtrls,
Vcl.WinXCtrls, Vcl.CheckLst, System.Actions, Vcl.ActnList, Vcl.Menus,
System.StrUtils,
Data.DB, Vcl.Grids, Vcl.DBGrids, Vcl.DBCtrls, FireDAC.Stan.Intf,
FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS,
FireDAC.Phys.Intf, FireDAC.DApt.Intf, FireDAC.Comp.DataSet,
FireDAC.Comp.Client;
type
TForm1 = class(TForm)
other buttons and so on...
procedure SwapValues(var Zahl1, Zahl2: Integer); //new
procedure SelectionSort(Sender: TObject); // new
procedure Button11Click(Sender: TObject); //new
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
workerModel: record
VorName: string[40];
NachName: string[40];
Age: Integer;
Schließen: string[30];
Admin: TToggleSwitchState;
DatenSehen: TToggleSwitchState;
Gender: string[20];
end;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses Sonderrechte_U, CheckedItem_U, Unit1, BenutzerEdit_u;
procedure TForm1.SwapValues(var Zahl1, Zahl2: Integer);
var
h: Integer;
begin
h := Zahl1;
Zahl1 := Zahl2;
Zahl2 := h;
end;
procedure TForm1.SelectionSort(Sender: TObject);
var
i, j, min: Integer;
var
sortArray, Data: Array of string;
begin
for i := 1 to Form1.ListBox1.Items.Count - 1 do
// i muss wahrscheinlich 0 sein?
begin
min := i;
for j := i + 1 to Form1.ListBox1.Items.Count do
if (Data[j] < Data[min]) then
min := j;
SwapValues(i, min);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Form2 := TForm2.Create(Self);
try
Form2.ShowModal;
finally
Form2.Free;
end;
end;
// more code
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
l: Integer;
t: String;
begin
with ListBox1 do
begin
Canvas.FillRect(Rect);
t := Items[Index];
l := Rect.Right - Canvas.TextWidth(t) - 1;
Canvas.TextOut(l, Rect.Top, t);
end;
end;
procedure TForm1.SearchBox1Change(Sender: TObject);
var
i: Integer;
begin
// SearchBox1.Parent := ListBox1;
ListBox1.Items.BeginUpdate;
try
for i := 0 to ListBox1.Items.Count - 1 do
ListBox1.Selected[i] := ContainsText(ListBox1.Items[i], SearchBox1.Text);
finally
ListBox1.Items.EndUpdate;
end;
// end;
// this is the end of the file

A Delphi unit must end with
end.
(notice the full stop).

Related

Embed AVI as a resource file in Delphi

I have a TForm with a TPanel which is linked to the display properties of a TMediaPlayer. By selecting Project -> Resources and Images I was able to insert my video as a resource file, where
filename = abc.avi
type = RCDATA
identifier = Resource_1
unit uForm2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms,
Vcl.Dialogs, Vcl.ExtCtrls, Vcl.MPlayer, Vcl.ComCtrls, Mmsystem;
type
TForm2 = class(TForm)
MediaPlayer1: TMediaPlayer;
Panel1: TPanel;
procedure FormCreate(Sender: TObject);
end;
var
Form2: TForm2;
implementation
uses
ShellAnimations;
{$R *.dfm}
procedure TForm2.FormCreate(Sender: TObject);
var
fs: TFileStream;
rs: TResourceStream;
s : String;
m : TMediaPlayer;
begin
rs := TResourceStream.Create(hInstance, 'Resource_1', RT_RCDATA);
s := ExtractFilePath(Application.ExeName) + 'abc.avi';
fs := TFileStream.Create(s, fmCreate);
rs.SaveToStream(fs);
fs.Free;
MediaPlayer1.Close;
MediaPlayer1.FileName := s;
MediaPlayer1.Open;
MediaPlayer1.Play;
MediaPlayer1.Display := Panel1;
end;
When the code is compiled, I get an error:
There is no driver installed in the system
Actually, the "abc.avi" file is 1 MiB. If I use a 1 GiB AVI, I get an error:
The file is being used by another process
How can I play this AVI correctly as a Delphi resource? The AVI in both cases has no sound. If I use a TOpenDialog, the video is played, but I don't want the user to select anything. The video must be embedded in the compiled executable.
______________ Updated code and error messages ______________
TMediaPlayer property:
MediaPlayer1.DeviceType = dtAVIVideo
Reported 4 errors:
1 [dcc32 Error] uForm2.pas(56): E2010 Incompatible types: 'NativeUInt' and 'string'
Line: Res := TResourceStream.Create(ChangeFileExt(PChar(lParam1), ''), 'RT_RCDATA');
2 [dcc32 Error] uForm2.pas(56): E2035 Not enough actual parameters
Line: Res := TResourceStream.Create(ChangeFileExt(PChar(lParam1), ''), 'RT_RCDATA');
3 [dcc32 Error] uForm2.pas(98): E2026 Constant expression expected
Line: ccRES: FOURCC = MAKEFOURCC(Ord('a'), Ord('v'), Ord('i'), Ord(' '));
4 [dcc32 Error] uForm2.pas(146): E2089 Invalid typecast
Line: mmioInstallIOProc(ccRES, TFNMMIOProc(MyResourceIOProc), MMIO_INSTALLPROC or MMIO_GLOBALPROC);
unit uForm2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.MPlayer, Vcl.ComCtrls, Mmsystem;
type
TForm2 = class(TForm)
MediaPlayer1: TMediaPlayer;
Panel1: TPanel;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
uses ShellAnimations;
{$R *.dfm}
function MAKEFOURCC(ch0, ch1, ch2, ch3: BYTE): FOURCC;
begin
Result := DWORD(ch0) or (DWORD(ch1) shl 8) or (DWORD(ch2) shl 16) or (DWORD(ch3) shl 24);
end;
function MyResourceIOProc(lpMMIOInfo: PMMIOInfo; uMessage: UINT; lParam1, lParam2: LPARAM): LRESULT; stdcall;
var
Res: TResourceStream;
function GetResourceStream: TResourceStream;
begin
Move(lpMMIOInfo.adwInfo, Result, SizeOf(TResourceStream));
end;
procedure SetResourceStream(Stream: TResourceStream);
begin
Move(Stream, lpMMIOInfo.adwInfo, SizeOf(TResourceStream));
end;
begin
case uMessage of
MMIOM_OPEN: begin
try
Res := TResourceStream.Create(ChangeFileExt(PChar(lParam1), ''), 'RT_RCDATA');
except
SetResourceStream(nil);
Exit(MMIOERR_CANNOTOPEN);
end;
SetResourceStream(Res);
lpMMIOInfo.lDiskOffset := 0;
Exit(MMSYSERR_NOERROR);
end;
MMIOM_CLOSE: begin
Res := GetResourceStream;
SetResourceStream(nil);
Res.Free;
Exit(MMSYSERR_NOERROR);
end;
MMIOM_READ: begin
Res := GetResourceStream;
Move((PByte(Res.Memory) + lpMMIOInfo.lDiskOffset)^, Pointer(lParam1)^, lParam2);
Inc(lpMMIOInfo.lDiskOffset, lParam2);
Exit(lParam2);
end;
MMIOM_SEEK: begin
case lParam2 of
SEEK_SET: begin
lpMMIOInfo.lDiskOffset := lParam1;
end;
SEEK_CUR: begin
Inc(lpMMIOInfo.lDiskOffset, lParam1);
end;
SEEK_END: begin
Res := GetResourceStream;
lpMMIOInfo.lDiskOffset := Res.Size - 1 - lParam1;
end;
end;
Exit(lpMMIOInfo.lDiskOffset);
end;
else
Exit(MMSYSERR_NOERROR);
end;
end;
const
ccRES: FOURCC = MAKEFOURCC(Ord('a'), Ord('v'), Ord('i'), Ord(' '));
procedure TForm2.FormCreate(Sender: TObject);
begin
mmioInstallIOProc(ccRES, TFNMMIOProc(MyResourceIOProc), MMIO_INSTALLPROC or MMIO_GLOBALPROC);
end;
procedure TForm2.FormDestroy(Sender: TObject);
begin
mmioInstallIOProc(ccRES, nil, MMIO_REMOVEPROC);
end;
procedure TForm2.FormShow(Sender: TObject);
begin
MediaPlayer1.FileName := 'Resource_1.avi+';
MediaPlayer1.Open;
MediaPlayer1.Display:=Panel1;
MediaPlayer1.Play;
end;
end.

Why has the parameter of my Delphi function with PyDelphiWrapper always a fix wrong value?

I am using Python4Delphi and try to get the demo WrapDelphiDemo running.
What it should do is to calculate the amount of prime numbers for values up to 1000000.
The expected value is 78498 but when I let the demo code running I get 575843.
I found out that parameter value "MaxN" of the function is always a fix value of 8574564 instead the expected 1000000.
class function TDelphiFunctions.count_primes(MaxN: integer): integer;
var
Count : integer;
begin
Count := 0;
ShowMessage(format('function parameter MaxN=%d is WRONG!!!! Should be 1000000!!!',[MaxN]));
//MaxN := 1000000;
TParallel.&For(2, MaxN, procedure(i: integer)
begin
if IsPrime(i) then
AtomicIncrement(Count);
end);
Result := Count;
end;
I use Delphi Seattle with Win7.
Python4Delphi is the latest from GitHub.
I use the original demo code.
What I need to adapt is that with Seattle version I cannot use inline variable definition.
Does anyone have an idea what I can do?
Here the full code of MainForm:
unit MainForm;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, SynEdit, Vcl.StdCtrls,
PythonEngine, PythonGUIInputOutput, SynEditPythonBehaviour,
SynEditHighlighter, SynEditCodeFolding, SynHighlighterPython, Vcl.ExtCtrls,
WrapDelphi;
type
TForm1 = class(TForm)
sePythonCode: TSynEdit;
HeaderControl1: THeaderControl;
Panel1: TPanel;
Splitter1: TSplitter;
Panel2: TPanel;
HeaderControl2: THeaderControl;
mePythonOutput: TMemo;
SynPythonSyn: TSynPythonSyn;
SynEditPythonBehaviour: TSynEditPythonBehaviour;
PythonEngine: TPythonEngine;
PythonGUIInputOutput: TPythonGUIInputOutput;
btnRun: TButton;
PyDelphiWrapper: TPyDelphiWrapper;
PythonModule: TPythonModule;
procedure FormCreate(Sender: TObject);
procedure btnRunClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
System.Rtti,
System.Threading,
System.Math;
type
TDelphiFunctions = record
class function count_primes(MaxN: integer): integer; static;
end;
var
DelphiFunctions: TDelphiFunctions;
procedure TForm1.FormCreate(Sender: TObject);
var
Py : PPyObject;
begin
Py := PyDelphiWrapper.WrapRecord(#DelphiFunctions, TRttiContext.Create.GetType(TypeInfo(TDelphiFunctions)) as TRttiStructuredType);
PythonModule.SetVar('delphi_functions', Py);
PythonEngine.Py_DecRef(Py);
end;
procedure TForm1.btnRunClick(Sender: TObject);
begin
GetPythonEngine.ExecString(UTF8Encode(sePythonCode.Text));
end;
function IsPrime(x: Integer): Boolean;
var
q, i : integer;
begin
if (x <= 1) then Exit(False);
q := Floor(Sqrt(x));
for i := 2 to q do
if (x mod i = 0) then
Exit(False);
Exit(True);
end;
class function TDelphiFunctions.count_primes(MaxN: integer): integer;
var
Count : integer;
begin
Count := 0;
ShowMessage(format('function parameter MaxN=%d is WRONG!!!! Should be 1000000!!!',[MaxN]));
//MaxN := 1000000;
TParallel.&For(2, MaxN, procedure(i: integer)
begin
if IsPrime(i) then
AtomicIncrement(Count);
end);
Result := Count;
end;
end.

custom managed record and memory leak

Using Delphi 10.4.1 I tried Custom Managed record management to initialize a record but still get memory leaks.
unit Unit3;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, System.IOUtils, System.DateUtils, System.Character,
Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;
type
TForm3 = class(TForm)
Button1: TButton;
RadioGroup1: TRadioGroup;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
type
TMyRec = record
DateTime: TDateTime;
v, size: integer;
str: string;
class operator Initialize (out Dest: TMyRec);
end;
TMyREcHolder = class
data: TMyRec;
constructor Create(const e: TMyRec);
end;
TMyList = class(TList)
procedure Clear; override;
end;
implementation
{$R *.dfm}
class operator TmyRec.Initialize (out Dest: TMyRec);
begin
Dest.str := '';
end;
{ TMyREcHolder }
constructor TMyREcHolder.Create(const e: TMyRec);
begin
inherited Create;
data := e;
end;
{ TMyList }
procedure TMyList.Clear;
var
i: integer;
begin
for i := 0 to Count - 1 do
TMyREcHolder(Items[i]).Free;
inherited Clear;
end;
procedure TForm3.Button1Click(Sender: TObject);
var
lst: TMyList;
i: integer;
rec: TMyRec;
FI: TSearchrec;
begin
Initialize(rec);
lst := TMyList.Create;
try
if FindFirst(TPath.Combine('C:\temp', '*.txt'), faAnyFile, FI) = 0 then
begin
repeat
if (FI.FindData.dwFileAttributes and faDirectory = 0) and
(FI.FindData.dwFileAttributes and faArchive = faArchive) then
begin
Application.ProcessMessages;
case RadioGroup1.ItemIndex of
0: Initialize(rec);
1: rec.str := '';
2: fillchar(rec, sizeof(rec), 0);
end;
try
rec.DateTime := FI.TimeStamp;
except
rec.DateTime := EncodeDateDay(1970, 1);
end;
rec.size := FI.size;
rec.str := FI.Name;
lst.Add(TMyREcHolder.Create(rec));
end;
until (FindNext(FI) <> 0);
FindClose(FI);
end;
finally
lst.Free;
end;
end;
end.
The radiogroup offers three items, both 1 and 3 leak memory. Can anyone explain why the Initialize one does? I want a reliable way of clearing a record to help me wean myself off a 20 year fillchar habit.

Form1 disappears when focus is on Form1 and browser window loses focus

I have a problem when my Form1 appear on body of browser window, simply he disappear when I put focus in my Form and the browser window lose focus. How solved it? Any suggestion will welcome.
See image below:
and here is my complete code:
Unit for enumeration of windows (EnumWindowUtil_.pas):
unit EnumWindowUtil_;
interface
uses
Winapi.Windows,
System.SysUtils,
System.Classes;
type
TWindowList = class(TStringList)
private
FAddClassname: Boolean;
public
procedure EnumChildWindows(handle: HWND);
property AddClassname: Boolean read FAddClassname write FAddClassname;
end;
var
wlistChilds: TWindowList;
implementation
function GetWindowClassName(hwnd: HWND): string;
begin
SetLength(Result, 1024);
GetClassName(hwnd, PChar(Result), Length(Result));
Result := PChar(Result);
end;
procedure EnumWindowCallback(hwnd: HWND; lParam: TWindowList); stdcall;
var
buffer: array[0..255] of char;
texto: string;
begin
if (not IsWindowVisible(hwnd)) then
Exit;
SendMessage(hwnd, $000D, 256, Integer(#buffer));
texto := StrPas(buffer);
texto := texto + ':' + GetWindowClassName(hwnd) + ' - ' + Format('%6.6x', [hwnd]) + '/' + IntToStr(hwnd);
lParam.AddObject(texto, TObject(hwnd));
end;
procedure TWindowList.EnumChildWindows(handle: HWND);
begin
Clear;
if Winapi.Windows.EnumChildWindows(handle, #EnumWindowCallback, Integer(Self)) then;
end;
end.
Here is main unit:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, EnumWindowUtil_,
Vcl.StdCtrls;
type
TForm1 = class(TForm)
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
linha: string;
implementation
{$R *.dfm}
function GetNavigatorHandle(BASE: HWND): HWND;
var
I: integer;
begin
linha:= '';
Result := 0;
wlistChilds := TWindowList.Create;
wlistChilds.AddClassname := True;
wlistChilds.EnumChildWindows(BASE);
for I := 0 to wlistChilds.Count - 1 do
begin
linha := wlistChilds.Strings[I];
if
(Pos('Chrome_Render',linha)>0)then
begin
Result := StrToInt(copy(linha, pos('/', linha) + 1, Length(linha)));
Break;
end;
end;
FreeAndNil(wlistChilds);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
janela, janelaContainer: HWND;
begin
janela := GetForegroundWindow;
janelaContainer := GetNavigatorHandle(Janela);
if janelaContainer = 0 then
begin
Exit;
end;
Winapi.Windows.SetParent(form1.handle,janelaContainer);
end;
end.

Simple OpenGL Code not working

Just learning some OpenGL with delphi and trying something simple but not getting a result, I belive i should get a dark green form. But when i run this i get nothing. No errors either. maybe missing something?
unit First1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls,OpenGL, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm2 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormPaint(Sender: TObject);
private
{ Private declarations }
GLContext : HGLRC;
ErrorCode: GLenum;
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.FormCreate(Sender: TObject);
var
pfd: TPixelFormatDescriptor;
FormatIndex: integer;
begin
fillchar(pfd,SizeOf(pfd),0);
with pfd do
begin
nSize := SizeOf(pfd);
nVersion := 1; {The current version of the desccriptor is 1}
dwFlags := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL;
iPixelType := PFD_TYPE_RGBA;
cColorBits := 24; {support 24-bit color}
cDepthBits := 32; {depth of z-axis}
iLayerType := PFD_MAIN_PLANE;
end; {with}
FormatIndex := ChoosePixelFormat(Canvas.Handle,#pfd);
SetPixelFormat(Canvas.Handle,FormatIndex,#pfd);
GLContext := wglCreateContext(Canvas.Handle);
wglMakeCurrent(Canvas.Handle,GLContext);
end; {FormCreate}
procedure TForm2.FormDestroy(Sender: TObject);
begin
wglMakeCurrent(Canvas.Handle,0);
wglDeleteContext(GLContext);
end;
procedure TForm2.FormPaint(Sender: TObject);
begin
{background}
glClearColor(0.0,0.4,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
{error checking}
errorCode := glGetError;
if errorCode<>GL_NO_ERROR then
raise Exception.Create('Error in Paint'#13+
gluErrorString(errorCode));
end;
end.
Since you request a single buffered context, you must call glFinish at the end of the rendering code, to commit your drawing commands to the implementation. However I strongly suggest you switch to using a double buffered context and instead of glFinish-ing you issue a wglSwapBuffers which implies a finish.

Resources