Wednesday, August 29, 2012

COM+ Practice (1)

So far, my understanding of COM+ in terms of registering is that it provide another way to register a COM.

The COM consuming applications (COM client) have nothing to change. 

 

Be aware, it still can be unregistered by using Regsvr32.exe.

Tuesday, July 31, 2012

Printing PDF, The easiest way

If the printer support PDF Direct Printing, LPR command line tool can be used for sending an PDF file directly to the printer, for example:

LPR –S <ip address of the printer server> –P “Printer name on the server” test.pdf

Please note:

  1. Double quote is not required for the printer name if the printer name has not contain space in it.
  2. LPR might not be turned on yet, at least on Window 7 it is not available by default, it can be turned on from Control Panel\All Control Panel Items\Programs and Features.

Wednesday, June 20, 2012

Undocumented Changes - From Delphi 2007 to Delphi XE–TRadioGroup

When I was trying  Evert Etienne's sample application for demonstrating all AnimateWindow possibilities (http://delphi.about.com/od/delphi-tips-2011/qt/hide-slide-fade-away-controls-delphi-form.htm), I noticed there was a difference of TRadioGroup in Delphi 2007 and Delphi XE(XE2).
This difference is in the below code snippet in Delphi XE:
procedure TCustomRadioGroup.ReadState(Reader: TReader);
begin
  FReading := True;
  inherited ReadState(Reader);
  FReading := False;
  if HandleAllocated then //<--- Delphi 2007 has no this checking!!!
    UpdateButtons;
end;
So, what is the deal of above difference?
The difference is, because of the checking, in Delphi XE and later version, the radio buttons will not be created until the handle of the radio group is allocated. The handle is not allocated when the radio group is just created, which means when TRadioGroup.Create is called, the buttons are not created yet. So the code like the below which is similar to  Evert Etienne’s sample will failed in Delphi XE as the controls is not nil but has no items at all:
procedure TForm1.FormCreate(Sender: TObject);
begin

TRadioButton(RadioGroup1.Controls[2]) …

end;
It is mystery to me that why it is doing that checking in Delphi XE.

Saturday, June 16, 2012

Delphi, Win.Ini and Windows7 (1)

Today I came across the following code:
var
  LDeviceList : TStringList;
  LIniFile : TIniFile;
begin
  LDeviceList := TStringList.Create;
  LIniFile := TIniFile.Create('win.ini');
  with LIniFile do
    try
      ReadSectionValues('devices', LDeviceList);
      ButtonPrinter.Enabled := LDeviceList.Count <> 0;
      …
    finally
      Free;
    end;
What  I was wondering was why it was using Win.Ini? I thought it had been discarded since long time ago, but above code is still working well on my Windows7 system.
So I did a search on my hard disk, there were three Win.Ini files. One was in C:\Windows, one was in C:\Users\<current user>\AppData\Local\VirtualStore\Windows, another one was in C:\Windows\winsxs\amd64_microsoft-windows-coreos_31bf3856ad364e35_6.1.7600.16385_none_814737ee58024dde. The one in Windows folder looked like the one above code was referring to. But what surprised me was that there was no [devices] section in the file at all, not either in the other two:
; for 16-bit app support
[fonts]
[extensions]
[mci extensions]
[files]
[Mail]
MAPI=1
CMCDLLNAME32=mapi32.dll
CMC=1
MAPIX=1
MAPIXVER=1.0.0.1
OLEMessaging=1
[MCI Extensions.BAK]
3g2=MPEGVideo
3gp=MPEGVideo
3gp2=MPEGVideo
3gpp=MPEGVideo
aac=MPEGVideo
adt=MPEGVideo
adts=MPEGVideo
m2t=MPEGVideo
m2ts=MPEGVideo
m2v=MPEGVideo
m4a=MPEGVideo
m4v=MPEGVideo
mod=MPEGVideo
mov=MPEGVideo
mp4=MPEGVideo
mp4v=MPEGVideo
mts=MPEGVideo
ts=MPEGVideo
tts=MPEGVideo

But the printer button was on when I ran the application and there was definitely no other code  controlled the printer button.

So I thought I had to debug the code. I put a break point on the line “ButtonPrinter.Enabled := LDeviceList.Count <> 0;” and it did run to there, and the LDeviceList did contain the correct printers installed on my Windows7!
So where was the real Win.Ini file it read from? Could not be the one in Windows folder. And the LIniFile does not tell you where the Win.Ini file was.
Then I launched VMMap.exe to view the memory and files were using by the application. I use it a lot to find out which DLL files the application is using to confirm the application is using the right one when there are a few versions of the same DLL stored in the system. But there is no sign of Win.Ini file in VMMap. Maybe FileMon is more proper for this case, so I ran FileMon and set its filter to show Ini file’s activity only, but there was no sign either!
That became  very interesting now. The code was reading something from the file but the system basically told you that sorry there was no such file has been opened and read.
What would you do then if you were me?

Thursday, May 3, 2012

Windows Installer: Can a installer invoke another installer during its installing?

In other kind of installer which is made by tools like Inno or NSIS, the installer can be run as many instances as you want, but there can only be one instance of Windows Installer running, so I was wondering how installers (.MSI) which are using Windows Installer to install files install pre-requested software which is also using Windows Installer?

Tuesday, April 10, 2012

What must have for my next smart phone

1. Bigger than3.5".
     3.2" is just big enough for A4 size PDF, but it hurts for long time reading.

2. Auto-focus camera. Barcode scanner does not work well with camera that can't auto-focus.

3. A louder speaker. For alarm in the morning of course.

Wednesday, March 21, 2012

Switch from code control to property control

In Delphi, we can control an Edit’s length and what can go in in the event KeyPress, for example, I have found my old code (code I have inherited from somebody else) do such things like this:

//Control only numbers can go in

if not (CharInSet(Key , ['0'..'9', #8])) then
  key := #0;

//Control the length

if (Length(edtFileNumber.Text) > 8) and (key <> #8) then
  key := #0;

But there are easier ways, they can be accomplished by the component’s properties (confirmed in Delphi XE).

For example, the property TLabeledEdit.MaxLength can control the length of editing text.

The property TLabledEdit.NumbersOnly can control only numbers can go in.

 

(Please note that I have found a lot places using above code-control pattern in my old code but I have not change all of them to property control, the reason will be worth another topic, long in short, that is about risk control while changing working code)

How to pass in and out address function/procedure ?

VBScript 5.0 has a new feature which is allow you to assign a function to an event handler. In a web page, it looks like this

  Window.click = getref(window_click)

It is easy isn’t? That is because “Window” is accessible to the script engine, what if it is not? For example, how do we pass

in the event handler’s variable to the script so that the script can assign a function to it, or vise versa, how do we return the

function’s address back to the host application so that it can assign it to the event handler?