Using Windows clipboard in Delphi console program - delphi

I am trying to use the (Windows) clipboard in a Delphi console program, but when I try to compile I get the message
"[dcc32 Fatal Error] Clipboard_Project.dpr(6): F2613 Unit 'Clpbrd' not found."
The code looks like this:
program Clipboard_Project;
{$R *.res}
uses
System.SysUtils, Clpbrd;
var
s: String;
begin
try
s := Clipboard.AsText;
writeln(s);
readln;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
I can imagine that it's something simple and obvious, but I can't find it! Any help would be appreciated!

for correction of this question, it should be used in the uses clause Vcl.ClipBrd correctly and not Clpbrd as incorrectly typed.

Related

Cannot pass typed char array to open array of char?

When compiled under Delphi 7 or Delphi XE, the code below complains
[DCC Error] Project1.dpr(25): E2010 Incompatible types: 'array of Char' and 'TAChar'
According to Rudy's article, it should be allowed to pass typed array to open array ?
Furthermore, why does it not complain for 'array of Boolean' and 'TABoolean' ?
Many thanks for help !
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
TAChar = array of Char;
TABoolean = array of Boolean;
procedure Test1(const CharArr: array of Char);
begin
end;
procedure Test2(const BoolArr: array of Boolean);
begin
end;
var
Arr1: TAChar;
Arr2: TABoolean;
begin
try
Test1(Arr1); // <------- Does not compile in Delphi 7 & XE
Test2(Arr2);
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
The code in the question is valid. Any compiler that refuses to compile it is defective. Probably there is little point submitting a bug report because modern versions will compile this code.
If you cannot move to a compiler that is not defective then you will have to work around the defect. Sertac's answer to a similar question demonstrates one such work around: https://stackoverflow.com/a/3781425/505088

DTD validation with SAX from MSXML 6 - Delphi XE5

The following code outputs "EOleException: error during the validation". It is this error:
0xC00CE225
XMLOM_VALIDATE_INVALID
Validate failed.
If I add the error handler the exact error message is "XML is neither valid nor invalid as no schema was found".
Does MSXML 6 SAX parser support embedding a DTD?
program TestSAXValidation;
{$APPTYPE CONSOLE}
{$R *.res}
uses
ActiveX, System.SysUtils, Winapi.msxml;
var
R: IVBSAXXMLReader;
begin
CoInitialize(nil);
try
R := CoSAXXMLReader60.Create;
R.putFeature('use-inline-schema', True);
R.putFeature('schema-validation', True);
R.putFeature('exhaustive-errors', True);
R.putFeature('prohibit-dtd', False);
R.parse('<!DOCTYPE a[<!ELEMENT a (#PCDATA)>]> <a></a>');
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
ReadLn;
end.

Save a IXMLDOMDocument3 to xml-file in runtime with Delphi 2007

If I would like to save a IXMLDOMDocument3 in runtime to a file on my harddrive, what is the syntax for that?
E.g. like IXMLDOMDocument3.save('c:\test.xml')
Or is it even possible?
Best regards!
the sample code below demonstrates how to load and save IXMLDomDocument3 XML at runtime. It uses msxml header file from Delphi-2010. IXMLDomDocument3 inherits from IXMLDomDocument and has Save method (as you wrote in your question). If method parameter is a string, then it specifies file name (it creates or replaces target file).
program Project3;
{$APPTYPE CONSOLE}
uses SysUtils, msxml, comObj, activex;
procedure LoadAndSaveXML(LoadFile, SaveFile : string);
var xml : IXMLDOMDocument3;
tn : IXMLDOMElement;
begin
xml := CreateComObject(CLASS_DOMDocument60) as IXMLDOMDocument3;
xml.load(LoadFile);
xml.save(SaveFile);
end;
begin
try
CoInitialize(nil);
try
LoadAndSaveXML('D:\in.xml', 'D:\out.xml');
finally
CoUninitialize();
end;
except
on E: Exception do begin
Writeln(E.ClassName, ': ', E.Message);
readln;
end;
end;
end.

TBitmap.Create not working in delphi console application

I need to process a set of bmp files using a console application, i'm using the TBitmap class, but the code doesn't compile because this error
E2003 Undeclared identifier: 'Create'
This sample app reproduces the issue
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
Vcl.Graphics,
WinApi.Windows;
procedure CreateBitMap;
Var
Bmp : TBitmap;
Flag : DWORD;
begin
Bmp:=TBitmap.Create; //this line produce the error of compilation
try
//do something
finally
Bmp.Free;
end;
end;
begin
try
CreateBitMap;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
why this code doesn't compile?
The issue is in the order of your uses clause, the WinApi.Windows and Vcl.Graphics units have a type called TBitmap, when the compiler find an ambiguous type resolves the type using the last unit of the uses list where is present. in this case use the TBitmap of the Windows unit which points to the BITMAP WinAPi structure , to resolve this change the order of your units to
uses
System.SysUtils,
WinApi.Windows,
Vcl.Graphics;
or you can declare the type using the full qualified name like so
procedure CreateBitMap;
Var
Bmp : Vcl.Graphics.TBitmap;
Flag : DWORD;
begin
Bmp:=Vcl.Graphics.TBitmap.Create;
try
//do something
finally
Bmp.Free;
end;
end;

ZipForge and invalid archives

I have ZipForge for Delphi XE2 & Delphi XE2.
I try to test any invalid zip archives (e.g. not fully downloaded) like in their demo:
procedure TfmMain.bnStartClick(Sender: TObject);
begin
with Archiver do
begin
FileName := 'c:\2.zip';
OpenArchive;
try
TestFiles('*.*');
except
MessageDlg('Errors occurred in the archive file', mtError, [mbOk], 0);
end;
CloseArchive;
end;
end;
But my exception doesn't fire; ZipForge's dialog fires instead of mine.
I tried Abbrevia Component but it even can't recognize if an archive is invalid...
Please help me to make my exception working (not ZipForge's one) or suggest me a better component for zip files with a test feature. Thanks!
Be aware that you can modify ZIP files, e.g. by truncating them somewhat, the ZIP file will still be valid. With my test file, I removed the final 5000 bytes and it was reported as valid. I extracted it successfully using my ZIP program. Of course the extracted contents were incorrect and not the original contents. Perhaps this is what was happening for you. Maybe your attempts to corrupt your ZIP file were not in fact making it into an invalid ZIP file.
Delphi XE2 comes with a built in ZIP component that worked well in my simple test and successfully detected an invalid file, once I had truncated the file enough to make it truly corrupt.
I used the IsValid method to check validity. Here is my very simple test program.
program ZipTest;
{$APPTYPE CONSOLE}
uses
System.SysUtils, System.Zip;
procedure Main;
const
ZipArchive = 'C:\desktop\test.zip';
var
ZipFile: TZipFile;
FileName: string;
begin
ZipFile := TZipFile.Create;
try
if ZipFile.IsValid(ZipArchive) then begin
ZipFile.Open(ZipArchive, zmRead);
for FileName in ZipFile.FileNames do begin
Writeln(FileName);
end;
end else begin
Writeln(ZipArchive + ' not valid');
end;
finally
ZipFile.Free;
end;
end;
begin
try
Main;
Readln;
except
on E: Exception do begin
Writeln(E.ClassName, ': ', E.Message);
end;
end;
end.
If you have an invalid ZIP file, it is most likely that the call to OpenArchive will fail. As long as your execption handling doesn't cover that case, you will get the result you describe.
Update: The suggested way to catch exceptions during TestFiles or any other method is to connect an OnProcessFileFailure event handler.

Resources