Monday, May 21 2007 @ 02:56 CEST
Contributed by: DaynaCode
Views: 618
Writing programs that alter the registry is becoming harder and harder and often requires adminstrator rights. Here is a function I garnered off the internet and fixed the missing code.
const
SECURITY_BUILTIN_DOMAIN_RID = $20;
DOMAIN_ALIAS_RID_ADMINS = $220;
var
SECURITY_NT_AUTHORITY : TSIDIdentifierAuthority;
function CurrentUserIsAdministrator: Boolean;
var
hAccessToken: THandle;
ptgGroups: PTokenGroups;
dwInfoBufferSize: DWORD;
psidAdministrators: PSID;
x: integer;
bSuccess: BOOL;
begin
SECURITY_NT_AUTHORITY.Value[0] := 0;
SECURITY_NT_AUTHORITY.Value[1] := 0;
SECURITY_NT_AUTHORITY.Value[2] := 0;
SECURITY_NT_AUTHORITY.Value[3] := 0;
SECURITY_NT_AUTHORITY.Value[4] := 0;
SECURITY_NT_AUTHORITY.Value[5] := 5;
Result:=False;
bSuccess:=OpenThreadToken(GetCurrentThread, TOKEN_QUERY, True, hAccessToken);
if not bSuccess then
if (GetLastError=ERROR_NO_TOKEN) then
bSuccess := OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, hAccessToken);
if bSuccess then
begin
GetMem(ptgGroups,1024);
try
bSuccess:=GetTokenInformation(hAccessToken, TokenGroups, ptgGroups,
1024,dwInfoBufferSize);
CloseHandle(hAccessToken);
if bSuccess then
begin
AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0, psidAdministrators);
try
for x:=0 to (ptgGroups.GroupCount - 1) do
if EqualSid(psidAdministrators, ptgGroups.Groups[x].Sid) then
begin
Result:=True;
Break;
end;
finally
FreeSid(psidAdministrators);
end;
end;
finally
FreeMem(ptgGroups);
end;
end;
end;
Wednesday, November 15 2006 @ 11:34 CET
Contributed by: DaynaCode
Views: 657
Question: Is there a way to convert the value of an enumerated type back to its name?
Answer: Yes, using Delphi's Runtime Type Information (RTTI). Specifically, the GetEnumName() function from the TYPINFO unit returns the name of a enumerated type constant as a string. It requires two parameters, a pointer to the enumerated type containing the constant and an integer value corresponding to the constant's value.
This article demonstrates GetEnumName() in some detail. It shows how to:
Return the names of all constants in an enumerated set
Use GetEnumName() to create clearer documentation
Write safety nets into your enumerated type processing code
Resolve common errors encountered with GetEnumName()
Friday, November 10 2006 @ 04:37 CET
Contributed by: DaynaCode
Views: 916
Since this is a commonly asked question, and since I was given this simple to use solution. I thought I'd post it for you. This uses the THttpCli component in the ICS component set, but it should
Simply its a LOT easier to ask an external site what your external IP is. Than it is to connect through the various levels of routers etc to find what could be several layers of gateways before you find the external IP.
This code depends on the existance of the free service by dynupdate.no-ip.com and it may evapourate at any time. You may wish to rush such a script on one of your own servers.
Tuesday, October 17 2006 @ 09:17 CEST
Contributed by: DaynaCode
Views: 4
When I open my project, Delphi (all versions) complains saying "<classname> not found". How to fix.
I only discovered this by accident.
This message is very cryptic and people will ask you all kinds of legitimate but misleading questions. "Did you include ____?" "Do you have a <classname> object on your form?"
Its caused when you edit the text of a form or other persistant class where details about objects are loaded from another file (eg. Main.dfm.) and the .dfm doesn't jive with the class definition. There is a class in the .dfm that you accidentally deleted from the class.
Right click on your form and select view as text. Manually remove the offending lines. Save your changes. Or... add the missing definition back into the class definition.
Close your project and re-open.
How did I get so wise? I had this happen to me and spend several hours with my client screaming about costs and time wasted. I hope this saves you some embarassment.
Sunday, August 27 2006 @ 07:59 CEST
Contributed by: joe90
Views: 2,751
Trim spaces and CRLFs from a string. Delphi doesn't have one of these functions apparently built in.
function TrimSpaces(stemp: string): string;
const Remove = [' ', #13, #10];
var
i: integer;
begin
result := '';
for i := 1 to length(stemp) do begin
if not (stemp[i] in remove) then
result := result+stemp[i];
end;
end;
{ Useage:
s := ' a b c d ';
s := TrimSpaces(s);
// s now = 'abcd'.
}
Wednesday, July 26 2006 @ 11:39 CEST
Contributed by: joe90
Views: 1,563
Easy to come up with, but tedious to code function for return a string name of a given file size in the Windows Explorer way. Three significant digits (unless the file size is a byte size of two digits, which is returned as such).
function FormatSize(FS: integer): string;
var
sTemp: string;
begin
if FS < 1000 then Result := IntToStr(FS)+ ' bytes'
else
if (FS < 1048576) then begin
// Format KB
sTemp := FloatToStr(FS/1024);
sTemp := Copy(sTemp, 1, Pos('.', sTemp)+2);
if Length(sTemp) > 3 then begin
if Length(Copy(sTemp, 1, Pos('.', sTemp)-1)) >= 3 then begin
Result := Copy(sTemp, 1, 3)+ ' KB';
end else begin
if Length(Copy(sTemp, 1, Pos('.', sTemp)-1)) = 2 then
Result := Copy(sTemp, 1, Pos('.', sTemp)+1)+' KB'
else Result := sTemp+' KB';
end;
end else Result := sTemp+' KB';
end
else if (FS < 1073741824) then begin
// Format MB
sTemp := FloatToStr(FS/1048576);
sTemp := Copy(sTemp, 1, Pos('.', sTemp)+2);
if Length(sTemp) > 3 then begin
if Length(Copy(sTemp, 1, Pos('.', sTemp)-1)) >= 3 then begin
Result := Copy(sTemp, 1, 3)+' MB';
end else begin
if Length(Copy(sTemp, 1, Pos('.', sTemp)-1)) = 2 then
Result := Copy(sTemp, 1, Pos('.', sTemp)+1)+' MB'
else Result := sTemp+' MB';
end;
end else Result := sTemp+' MB';
end else begin
// Format GB
sTemp := FloatToStr(FS/1073741824);
sTemp := Copy(sTemp, 1, Pos('.', sTemp)+2);
if Length(sTemp) > 3 then begin
if Length(Copy(sTemp, 1, Pos('.', sTemp)-1)) >= 3 then begin
Result := Copy(sTemp, 1, 3)+' GB';
end else begin
if Length(Copy(sTemp, 1, Pos('.', sTemp)-1)) = 2 then
Result := Copy(sTemp, 1, Pos('.', sTemp)+1)+' GB'
else Result := sTemp+' GB';
end;
end else Result := sTemp+' GB';
end;
end;
{ Useage:
SizeStr := FormatSize(MyFileStream.Size);
..Results such as: '2.45 KB', '1.06 MB',
'453 bytes'
}
Sunday, July 16 2006 @ 12:02 CEST
Contributed by: Blueaura
Views: 720
Wondering about KOL?
Want to know how to use it?
Here's a quick taste of what's to come:
Ever wondered why the VCL always seems to give you at least a 400K executable, even if you were just writing a small utility? You probably know that when you go down to the bare metal and write your utility in plain WIN32 API, you can get your program down to just a couple of Kb. But if you know that, you know it's pretty hard to do.
As an introduction I will present you with three programs that do all the same thing:nothing!
First, the Delphi way: Just open Delphi (any version) and hit F9. You will get the famous Project1.exe which is - depending on the Delphi version you are using - between 200Kb and 400Kb.
Nothing to it, hit F9, obtain a working Windows program. Doesn't do too much...you think!
Now let's explain a bit what is done for you by writing something that is about the same thing in plain WIN32 API programming:
// A Window function, dispatches the messages for our program
function WindowProc(TheWindow: HWnd; TheMessage, WParam,
LParam: Longint): Longint; stdcall;
begin
case TheMessage of
// If we receive a WM_DESTROY message ,
// we quit the program
WM_DESTROY:
begin
PostQuitMessage(0);
Exit;
end;
end;
// Call the default (os) window procedure for all messages
// we don't handle ourselves
Result := DefWindowProc(TheWindow, TheMessage, WParam, LParam);
end;
// This function registers the Window Class
function RegisterClass: Boolean;
var
WindowClass: TWndClass;
begin
//Prepare our new window class
windowClass.Style := CS_HREDRAW or CS_VREDRAW;
// The Style
windowClass.lpfnWndProc := @WindowProc;
// The window procedure as defined above
windowClass.cbClsExtra := 0;
// No extra memory needed for the class
windowClass.cbWndExtra := 0;
// No extra memory needed for the window itself
windowClass.hIcon := LoadIcon(0, IDI_APPLICATION);
// Use a standard logo
windowClass.hCursor := LoadCursor(0, IDC_UPARROW);
// Use a standard cursor
windowClass.hbrBackground := COLOR_BTNSHADOW;
// Use a standard color
windowClass.lpszMenuName := nil;
// No menu, except for the system menu
windowClass.lpszClassName := 'BlueAura';
// Thee registered class name
//Registrer the class in the system}
Result := Windows.RegisterClass(WindowClass) <> 0;
end;
var
TheMessage: TMsg;
OurWindow: HWND;
begin
// register the new class first
if not RegisterClass then
begin
MessageBox(0,'RegisterClass failed',nil,MB_OK);
Exit;
end;
//
OurWindow := CreateWindowEx(0, {no extended styles}
'BlueAura', // Windows registered class name
'Project1', // Caption
WS_OVERLAPPEDWINDOW or //normal screen
WS_VISIBLE, // visible
CW_USEDEFAULT, // horizontal position
CW_USEDEFAULT, // vertical position
CW_USEDEFAULT, // standard width
CW_USEDEFAULT, // standard height
0, // geen ouders
0, // geen menu
hInstance, // instance of our application
nil // Nothing further
);
// If it doesn't work, quit!
if OurWindow=0 then
begin
MessageBox(0,'Phew, lukt niet...',nil,MB_OK);
Exit;
end;
// This runs the actual program:
while GetMessage(TheMessage,0,0,0) do
begin
TranslateMessage(TheMessage);
DispatchMessage(TheMessage);
end;
end.
Now, this does the same, looks almost the same, but took a lot of work I hope you'll agree.
But it is only a couple of Kb big, maybe 8-12Kb, again depending on the Delphi version, a possible saving of some 2000%! You should also be able to notice that it loads a lot faster and feels a lot snappier.
But then again, this way of coding is tedious and really no fun at all once you've done it once or twice. Wouldn't it be nice if we had the comfort of the VCL with the small executable size of the hardcore way of doing things?
That is where the Key Objects Library comes in: It does just that.
First you need the KOL library available from http://bonanzas.rinet.ru/kol.zip.
Unzip it to a new directory and then create this program:
Delphi Code:
program firstkol;
uses
windows, kol;
begin
applet:=Newform(nil,'Project1');
Run(Applet);
end.
If you compile and run it, you'll see it does the same as the above, with less lines of code than even the VCL version autogenerated for you and that it is only about 24Kb in executable size.
And it can become even better:
- KOL's MCK add-on allows you to design in much the same way as with the VCL
- If you use tweaked system.pas replacements the executable size will go down to only 12Kb.
I will write about those two features the next time.
Wednesday, May 03 2006 @ 06:23 CEST
Contributed by: CyberK
Views: 2,906
Today i came to sittuation where i had to read string presentation of binary data and to convert it to byte
Function BinaryStrToByte(S: string ): Byte;
Var
i: Integer;
Begin
Result := 0;
For i := 1 To 8 Do
Case S[i] Of
'0': Result := Result shl 1;
'1': Result := Result shl 1 + 1;
End; { Case }
End;
for example BinaryStrToByte('00000010') will return 2