Set environment variables from Delphi (Win32) - delphi

I'm trying to set a system wide environment variable from within my delphi application. The Documentation says to add a registry key to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment and then broadcast a WM_SETTINGCHANGE message. Anyone know how to Broadcast this message?
PS. I'm trying to do this in a console application.

Call SendMessage. Use HWnd_Broadcast as the target window.
A better option may be SendNotifyMessage, which doesn't wait for all the recipients to return.

There is also a topic in the Embarcadero Developer Network covering this issue and providing sample code for sending the message. The author uses SendMessageTimeout as recommended in the MSDN doc for WM_SETTINGCHANGE:
http://edn.embarcadero.com/article/28254

Related

wso2 esb console localization

I'd like to translate the management console to localized language. I've try to follow the instructions from http://wso2.com/library/knowledge-base/2011/11/playing-around-carbon-product-themes/ as a starting point, cause I think that should be in the same theory. But this didn't work, and cause an exception when entering the site.
I see the document is pretty old (2011), is this not work anymore? or the procedure has been changed?
Edit:
sorry, forgot to mention, I'm using WSO2 ESB 4.8.1.
Yes as you have mentioned, above link is outdated. However, the concept is same in new versions as well. Keeping an extracted jar file in the plugins directory will no longer work since we have disabled that along the way as a security fix.
Herewith I have given [1]. the latest documentation of "Customizing the Management Console" of the WSO2 Enterprise Service Bus. You can use it as a starting point.
If you need further help regarding this issue, please let me know.
Thanks,
Upul
[1]. https://docs.wso2.com/display/ESB481/Customizing+the+Management+Console

Get the Description of a Service?

How may I get the description of a Windows Service like below?
I tried by using the Windows Registry, but the majority of Services appear to not have a description, or the description value is stored in a dll - so this seems to be the wrong approach.
Example:
Windows Time Service (W32Time), the description in the registry is shown as
#%SystemRoot%\system32\w32time.dll,-201
Yet the actual description as seen in Services.msc is:
Maintains date and time synchronization on all clients and servers in
the network. If this service is stopped, date and time synchronization
will be unavailable. If this service is disabled, any services that
explicitly depend on it will fail to start.
-
I have been searching on the MSDN website and came across this:
SERVICE_DESCRIPTION structure
lpDescription
The description of the service. If this member is NULL, the
description remains unchanged. If this value is an empty string (""),
the current description is deleted.
The service description must not exceed the size of a registry value
of type REG_SZ.
This member can specify a localized string using the following format:
#[path]dllname,-strID
The string with identifier strID is loaded from dllname; the path is
optional. For more information, see RegLoadMUIString....
-
pszOutBuf [out, optional]
A pointer to a buffer that receives the string.
Strings of the following form receive special handling:
#[path]\dllname,-strID
The string with identifier strID is loaded from dllname; the path is
optional. If the pszDirectory parameter is not NULL, the directory is
prepended to the path specified in the registry data. Note that
dllname can contain environment variables to be expanded.
Which I think would suggest why viewing the Registry showed the W32Time description as #%SystemRoot%\system32\w32time.dll,-201
If I understand correctly I need to read the dll name in memory and retrieve the strID where the Service description is stored?
This is all confusing for me, I would be grateful if someone could help.
All I need is to get the description of a Service, it surely cannot be as complicated as this can it?
Thanks :)
In all versions of Delphi, the the JEDI JCL contains everything you need to get friendly descriptions of services, and anything else to do with the Service Control APIs.
THe class TJclSCManager in the unit JclSvcCtrl.pas contains a property Services, which includes name and description of each service registered, and lets you also do things like start, stop, enable, and disable services too.
Update: The other answer here from ldsandon points out that the Delphi RTL apparently includes this already in XE2, in the unit WinSvc. See answer below about QueryServiceConfig2. Thanks ldsandon for pointing this fact out.
Call QueryServiceConfig2 (you will find also a C example there).
Whatever you need to do with services should be done through the Service Manager API. Registry data should be treated as "private" to the OS.
Using WMI is another way to use the Windows API directly, for example with the help of the (free) API code generator
WMI Delphi Code Creator
The WMI Delphi Code Creator tool allows you to generate Object Pascal
and C++ code to access the WMI (Windows Management Instrumentation)
classes, events and methods.
Are you using Unicode? The remarks for the RegLoadMUIString function say that only the Unicode version is supported.
The RegLoadMUIString function is supported only for Unicode. Although
both Unicode (W) and ANSI (A) versions of this function are declared,
the RegLoadMUIStringA function returns ERROR_CALL_NOT_IMPLEMENTED.
Applications should explicitly call RegLoadMUIStringW or specify
Unicode as the character set in platform invoke (PInvoke) calls.
Have you tried calling RegLoadMUIStringW directly?
You also can check the GLibWMI library. Free (and source included) library for work with WMI. Include a component named TServiceInfo.
Also is included a demo for work wirh services.
With this component you can access at Win32_Service Class; You can check the properties and structure here.
Regards

QuickReport throws "There Is No Default Printer Currently Selected" Exception

I have created a Delphi Service which prints TQuickReports. Everything works fine if compiled and run as a Windows Application. But when converted to operate as a service trying to create a form containing a TQuickRep component throws the exception.
This service runs fine on many other boxes but not this one in particular. Here are some details:
Using QuickReport version 4.07
Box is a Windows Server 2008 operating system.
Using Delphi 2007
Printer.Printers.Count is returning a positive value. In fact I can list out all of the printers.
I have tried running the service both using Local System Account and Logged on as an Admin.
Is there a default printer set up in session 0? Remember that under Vista / Server 2008 / Windows 7, services run in a separate session. Whether or not the logged-in user has a default printer set is not relevant - it's a per-session setting and doesn't affect session 0.
Can you rewrite the code to gracefully handle that exception and pick a printer to use?
You can solve this problem by creating a new dword UserSelectedDefault with the value: 1 in
HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\SessionDefaultDevices\Session_ID
Make sure you have a local printer selected.
You might give the user a way to select the printer for the service. The Windows service probably does not have a default printer set.
Set TQuickRep.PrinterSettings.PrinterIndex to set the printer number. Then, TQuickRep.Print to print the report.
A colleague ended up finding the solution. I should have added these are "network" printers and not Local printers (at the time I didn't think this was related to the problem). So the service needed to be installed with "NetworkService" as the user account under the logon tab. From the Windows Help:
To specify that the service uses the Network Service account, click This account, and
then type NT AUTHORITY\NetworkService
We had a simular problem here. Using TS servers, Citrix and Powerfuse 9.
Powerfuse had all printers capitalized, however they were shared in a mixed case.
This combination caused Delphi/QReport to crash
When all printers are from printserver to powerfuse in the same case (not important upper or lower or even mixed), the problem was gone
Actually it is a Delphi(5) problem. The comparison of the available printers and the default printer is case sensitive (Printers.pas):
if TPrinterDevice(Objects[I]).Device = Device then
begin
with TPrinterDevice(Objects[I]) do
SetPrinter(PChar(Device), PChar(Driver), PChar(Port), 0);
Exit;
end;
Changing the comparison to:
if lowercase(TPrinterDevice(Objects[I]).Device) = lowercase(Device)
solves the problem.
If using terminal services 2008, same user for multiple sessions, you should look into the:
HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\SessionDefaultDevices\Session_ID
instead of
HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\
I solved a similar problem: If a Delphi application (or service) uses QuickReport, it runs before the system loads the default printer (or printers).
When QuickReport executes TQRPrinter.Init, the printer.printers.count is zero,
shortly after the system loads, the printer.printers.count is the number of printers,
but tqrprinter.int has already executed, so TQRPrinter.FPrinterOK is false,
you then see this error when you try open a QuickReport.
The solution for me was wait until the printers were loaded before launching the application (in citrix and terminal server). I solved this in two ways, either by overwriting tqrprinter or delay the dpr.

JavaFX url-proxy?

My computer is running behind proxy. I want to access url from JavaFX. for example say i want to show image from a url. But i haven't seen anyway to provide proxy settings for the connection(?). Please if someone can tell me how to do things in such situation?
Thanks
As link in accepted answer doesn't work anymore I'm adding proxy setting code here:
System.setProperty("http.proxyHost", "myproxyhost");
System.setProperty("http.proxyPort", "80");
By default, JavaFX will automatically use your Operating Systems proxy settings, this is controlled via the Java Preferences or Java Control panel icons on your Mac or PC.
You can also set, within Java, the proxy environment variables, but I haven't tried this.
If you are trying to request an external resource from an unsigned application, Java may prompt you (the user) for permission when it runs.
Setting proxy in Java/FX

How to tell if Windows Taskbar's "autohide" is enabled?

How can I tell via a Delphi program if the Windows Explorer Taskbar is set to Autohide?
In Windows XP and higher, you can call SHAppBarMessage API with ABM_GETSTATE message.
Syntax:
SHAppBarMessage(ABM_SETSTATE, pabd);
pabd is a pointer to APPBARDATA struct.
header file is: shellapi.h.
If you want to get state of taskbar, use ABM_GETSTATE message.
you can call this api in delphi.
What is is that you really want to find out? Is it because you want to know the area of the screen that is useable?
If so, then I believe you can use the Screen.WorkAreaRect to determine the available screen area, where all (permanent) tool bars etc. are excluded.
Use Win32 shell apis (IsTBAutohide and others)
See Google Groups for undocumented apis.
Never read registry (ans stop removing right answers, it's stupid...)
U have to deal with windows registry because this information is keept in there. Value indicating "autoohide" is written(read) only while user logon /logout ont with his account
Registry key responsible for storing this information is located in
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\StuckRects2
The only thing in there is settings and it is a 9th hex value
for "autohide on" this value is 03 for "autohide off" it is 02

Resources