Why doesn't my code find any registry keys? - delphi

I want to list USB VIDs from the registry, and I wrote the following Delphi code:
procedure FindUSBvids(VIDs: TStrings);
var
Reg1: TRegistry;
begin
Reg1 := TRegistry.Create;
try
Reg1.RootKey := HKEY_LOCAL_MACHINE;
Reg1.OpenKey('System\CurrentControlSet\Enum\USB', False);
Reg1.GetKeyNames(VIDs);
finally
Reg1.Free;
end;
end;
procedure TForm1.Button6Click(Sender: TObject);
FindUSBvids(Memo2.Lines);
end;
Unfortunately the Memo remains empty. I triple-checked the key, it exists and has a couple dozen subkeys. What am I doing wrong?
(Note: I'm not sure this is the right way to find USB VIDs, but that's not the point. It's just that I'm puzzled why the code doesn't produce any result.)

You are asking for write access to the registry key. Use OpenKeyReadOnly instead of OpenKey.

Are you running your application with elevated access rights as administrator or as a normal user.
When you are using OpenKey Delphi by default tries to access that registry key using Read and Write access.
Now on Windows XP this would be just fine but on Windows Vista and newer this can be blocked by UAC especially if you are trying to read registry contents from HKEY_LOCAL_MACHINE part because this part of the registry is protected and requires elevated privileges to be modified.
So you need either to launch your application with administrative rights or change the Access property to `KEY_READ'. I would recommend using the lather if you are only enumerating the USB devices.

Related

Delphi RAD Studio 10.2, unable to browse windows system folder using FileOpenDialog

I need to be able to enumerate the folders present under C:\Windows\system32\dns on a Windows server 2016 instance running Windows DNS server.
Having tried FindFirst()/FindNext() and getting no results, I built a quick VCL Forms App to understand what was happening. I have a TButton and a TEdit, and the button's OnClick is below:
procedure TForm1.Button1Click(Sender: TObject);
begin
FDir := 'C:\Windows\System32\';
with TFileOpenDialog.Create(nil) do
try
Title := 'Select Directory';
Options := [fdoPickFolders, fdoPathMustExist, fdoForceFileSystem];
OkButtonLabel := 'Select';
DefaultFolder := FDir;
FileName := FDir;
if Execute then
Edit1.Text := Filename;
finally
Free;
end;
end;
When I run this - either as Administrator, or normally, on the server - and try to browse to the folder C:\Windows\system32\dns\ in the FileOpenDialog, I get an error:
Windows can't find 'C:\Windows\system32\dns'. Check the spelling and try again.
However, I know the folder exists, and I can browse it using Windows Explorer on the server, so there must be an issue with the Delphi code, or the permissions the App is running under.
Please, can anyone suggest what I need to do to fix this?
Thanks to #SertacAkyuz for reminding me about file system redirection - trying to access %Windir%\system32 from a 32bit program will be redirected to %Windir%\SysWow64 which doesn't contain the dns folder.
You can use the virtual alias %Windir%\Sysnative to gain access to the actual system32 folder from a 32bit application, and that works for the above case. so browsing to %Windir%\sysnative\dns allows me to enumerate the folders correctly.

Change file permission in FTP using TIdFTP Delphi

How do I change the permissions of a file using TidFTP in Delphi?
with IdFTP1 do
begin
try
Connect;
ChangeDir(FTPDiretorio);
Put(FArquivo, NomeOnline);
chmod(' ');//640 HOW??????????????
Disconnect;
finally
FreeAndNil(IdFTP1);
end;
end;
CHMOD is not a standard FTP command. Some FTP servers implement it as a custom command, and others do not implement it at all. As such, you have to use the TIdFTP.Site() method to send it, on FTP servers you know support it, eg:
IdFTP.Site('CHMOD 640 filename');

Generate PDF using QuickReport from a Windows Service in Delphi

I'm writing a windows service using Delphi XE3. The service is going to read from a database, generate a pdf using quickreport 5.05.1.
I plan to generate the pdf using TQRPDFDocumentFilter.
Everything works fine in a normal VCL application, but when I implement it in a windows service the service hangs (without any exceptions) when I do a QuickRep.Prepare.
I have read that it is possible to use QuickReport in a windows service, but I do not know how. Any suggestions?
Where is the code:
procedure foo
var
pdfFilter: TQRPDFDocumentFilter;
begin
with TForm2.Create(Self) do
begin
ClientDataSet1.Open;
QuickRep1.Prepare;
pdfFilter := TQRPDFDocumentFilter.Create(GetApplicationFolder() + 'test.pdf');
try
QuickRep1.QRPrinter.ExportToFilter(pdfFilter);
finally
pdfFilter.Free;
ClientDataSet1.Close;
end;
end;
end;
Edit:
I have also tried turning off "show progress" on the QuickReport as suggested in another thread.
Writing some code to catch an exception reveals that it indeed throws one. The message is "There is no default printer currently selected".
So this leads me to believe that the local system user that the service is running under does not have any printers installed and that this is the problem.
I have resolved a similar problem (printing to a shared network printer from a Java server running as a Windows service) with these steps:
log on as the user who will run the service
install the printer
IIRC with Delphi applications, the printer name is not case sensitive (with Java it is).

Why does GetActiveObject fail to detect running instance of Outlook?

I am facing an issue in my windows7 32bit Pc(on i3).I have Outlook 2010 and Delphi 7 on it.
I am using following code to detect Outlook is running or not.
ClassID := ProgIDToClassID(ClassName);
Result := (GetActiveObject(ClassID, nil, Unknown) = S_OK);
This fails, ie, result become false and yet in other PCs this working fine.
The error I'm getting is MK_E_Unavailable.
Update:
May be it just happening with me only.
procedure TForm1.Button1Click(Sender: TObject);
function IsObjectActive(ClassName: string): Boolean;
var
ClassID: TCLSID;
Unknown: IUnknown;
begin
try
ClassID := ProgIDToClassID(ClassName);
Result := (GetActiveObject(ClassID, nil, Unknown) =S_OK );
except
Result := False;
end;
end;
begin
if IsObjectActive('Outlook.Application') Then
ShowMessage('OutLook is there.')
else
ShowMessage('OutLook is not there.')
end;
Plz note OL is running and
When I am running the created exe, I am getting message "OutLook is there".
when I am running from Delphi IDE, I am getting Message 'OutLook is not there.'
This happens always, I am using Delphi 7 on Windows 7, running with Run as Admin. Kindly tell me why this happening and how can I fix this.
What's the issue of Delphi 7 on Windows 7.
Please suggest.
Here's the entry for GetActiveObject.
http://msdn.microsoft.com/en-us/library/a276e30c-6a7f-4cde-9639-21a9f5170b62%28VS.85%29
If you want to decode the error, you need to find out what the HResult means.
Wikipedia has a link to a ERR.EXE utility from MS that will translate the HResult code into an error description. For COM HResults see: http://matthewbass.com/2005/11/15/decoding-com-hresult-error-codes/.
note the download link in the article is broken, here's a working link: http://www.softlookup.com/display.asp?id=7113
Once you know what the error is, update the question.
If you want to know whether a process is running without using OLE, see: How to check if a process is running using Delphi?
Another option might to use FindWindowEx to check for Outlook 2010 specific windows.
You can use WinID (a spy++ clone) to see the windows used by Outlook 2010.
I was facing the same issue and I found the solution.
It's simple, If Outlook is already running, it MUST have the same rights as the process that is trying to use it.
In simple words, if you are running Outlook with admin rights, you must execute your app with admin rights.
Your problem must be that you are running Outlook without admin rights, and Delphi IDE with admin rights. So when you are launching your app from within IDE, the rights doesn't match, and you get the error. This is why when running your app outside the IDE it works as expected. Because outside IDE your app runs without admin rights.
Try to match the rights. This is something to take into account for the end user environment too.
Also, the UAC under Windows Vista and later is known to cause multiple issues with these type of things. If everything else fails, disable UAC (User Account Control, you will find it under your account options) and see what happens.
try to use rctrl_renwnd32
try this:
(FindWindow('rctrl_renwnd32', nil) <> 0)
http://users.skynet.be/am044448/Programmeren/VBA/vba_class_names.htm

How to programatically detect if the MySql Connector/ODBC is installed? (and install it if needed)

I am thinking of the client PC here. When someone comes to run my app, they may not have the MySql Connector/ODBC installed.
Now, I could just try to connect the information scheme (or mysql) database (which is preferable?) - but, if that fails, it might only mean that the database server is down. I could ping it, but it might be up and the MySql process not currently running.
I guess I could just install the connector anyway, even though it sounds sloppy, but not if doing so is going to noticably slow my app's start-up time.
What is the best approach?
When I do install, how do I do that from Delphi, given that the connector will be available on the d/b server, who's IP address is know?
Or ... at the moment my app is a simple .EXE, with no installer. Should I create an install which also installs the ODBC connector? (if so, can anyone recommend a good freeware install builder (with no adware or toolbar installs)?
I hope that this is clear. Please ask me if not.
Thanks.
To check the ODB drivers installed you must check this windows registry key
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers
from there you can easily write a delphi function to detect if the MySQL ODBC driver is installed
{$APPTYPE CONSOLE}
uses
Windows,
Classes,
Registry,
SysUtils;
function ODBC_DriverInstalled(const DriverName:string) : Boolean;
Var
Reg : TRegistry;
Providers: TStrings;
i : Integer;
begin
Reg:=TRegistry.Create;
Result:=False;
try
Reg.RootKey:=HKEY_LOCAL_MACHINE;
if Reg.OpenKeyReadOnly('SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers') then
begin
Providers:=TStringList.Create;
try
Reg.GetValueNames(Providers);
for i := 0 to Providers.Count-1 do
begin
if CompareText(DriverName,Providers[i])=0 then
begin
Result:=True;
Break;
end;
end;
finally
Providers.Free;
end;
end;
finally
Reg.Free;
end;
end;
begin
try
Writeln(ODBC_DriverInstalled('MySQL ODBC 5.1 Driver'));
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
To install you have several options one can be use inno setup (which can read the windows registry as well to check for the odbc driver) and then install the driver included inside of your setup file. also you can donwload the driver directly from your app using a simple HTTP GET passing one the download address located here
There is no need to install the MySQL connector. You just need to put the right dll (depending on the server version) in the same folder as your application executable. I would recommend an installer like InnoSetup. This will also let you add start menu shortcut etc.

Resources