Contribute  :  Calendar  :  Advanced Search  :  Site Statistics  :  Directory  :  User Profiles  :  FAQ's  :  Web Resources  :  Polls  
    #delphi / #C-sharp EFNet #Delphi / #C-sharp    
 Welcome to #delphi / #C-sharp
 Tuesday, October 07 2008 @ 01:09 CEST
Rules
Rules for #delphi / #c-sharp on EFNet

  • Read the help / search the web before you ask
  • No code pasting in the channel, use pastebin
  • English Only
  • Don't ask if you may ask a question, just ask your question
  • Repeating your question every minute isn't gonna help, wait or ask later again
  • Just behave
  • We do not make your homework, so no spoon feeding
  • No colors, bold, styles
  • No warez
  • Religion/Politics is considered off topic and generally not allowed
  • If you don't get an answer, nobody understands you, nobody knows the answer, or nobody active has the time to answer.
  • Given enough time/money/resources, anything is possible; so don't ask if <insert something here> is possible, because the answer is "yes".


Feel free to register an account on this website, contribute articles or links.

Is the current user an Administrator

 Email Article To a Friend View Printable Version 
Delphi ArticlesWriting 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;
 
Post a comment
Comments (0) Trackbacks (0)
User Profiles
an9liu
avgjoe
BromaxSux
chris
cmf
CyberK
@Danborg
@DaynaCode
dmi
@Drk`Angel
drbob42
@FuzyLogic
@Hylander
iNDiGLo
joke32
joe90
joepezt
@ki9a
@[Kutulu]
@LordCrc
Macca
@MeNotYou
@MrBtrieve
mrw
mytoz
noiseafk
obeliks
@oLeOle
ootje
PaC
pango
PoseFant
protektor
relips
@SirPsycho
Squasige
Starlet
Torquai
Tucker
@Vibes
warchest
Not on this list? Let me know.

How to fade in/out a component on a form

 Email Article To a Friend View Printable Version 
Delphi ArticlesI was given this code. I haven't tried it. The person who gave it to me says it allows you to fade a component into view. It doesn't do partial fades.
 
read more (828 words) Post a comment
Comments (0) Trackbacks (0)

Question: Is there a way to convert the value of an enumerated type back to its name?

 Email Article To a Friend View Printable Version 
Delphi ArticlesQuestion: 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()
 
read more (28 words) Post a comment
Comments (0) Trackbacks (0)

Finding the External IP of a computer using ICS components and 'http://dynupdate.no-ip.com/ip.php'

 Email Article To a Friend View Printable Version 
Delphi ArticlesSince 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.

function GetExternalIP:string;
const
IPCheckAddress = 'http://dynupdate.no-ip.com/ip.php';

var
request : ThttpCli;

begin
request := TDownload.Create( Application.MainForm);
try
request.URL := IPCheckAddress;
request.RcvdStream := TStringStream.Create('');
request.Get;
result := TStringStream(Request.RcvdStream).DataString;
finally
request.RcvdStream.Free;
request.RcvdStream := nil;
FreeAndNil(request);
end;
end;
 
Post a comment
Comments (0) Trackbacks (0)

not found when opening a form or project

 Email Article To a Friend View Printable Version 
Delphi ArticlesWhen 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.

Dayna.
 
Post a comment
Comments (0)

Trim / remove spaces from a string

 Email Article To a Friend View Printable Version 
Delphi ArticlesTrim 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'.
}
 
Post a comment
Comments (0) Trackbacks (1)

Convert file byte size to 3 significant digits KB, MB, GB

 Email Article To a Friend View Printable Version 
Delphi ArticlesEasy 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'
}
 
Post a comment
Comments (1) Trackbacks (266)

KOL Introduction

 Email Article To a Friend View Printable Version 
Delphi ArticlesWondering 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:

Delphi Code:

program Hardcore;
//
// purpose: Hardcore API delphi program
// author: This version © 2004, Thaddy de Koning
// Based on a code example from John Ayres
// In: The Tomes of Delphi, Win32 Core Api Win2000 edition
uses
Windows, Messages;

// 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.hInstance := hInstance;
// Application instance

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.

Have fun

Source: Thaddy De Koning
http://delphi.blue-aura.co.uk
 
Post a comment
Comments (0) Trackbacks (6)

Binary String to byte

 Email Article To a Friend View Printable Version 
Delphi ArticlesToday 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
 
Post a comment
Comments (4) Trackbacks (21)

Add shadow to form in Windows XP

 Email Article To a Friend View Printable Version 
Delphi ArticlesYou can add shadow to your form by overriding CreateParams() of your form class

Interface

type
 TForm1 = class(TForm)
 private
   { Private declarations }
 public
   { Public declarations }
 protected
   procedure CreateParams(var Params: TCreateParams); override;
 end;

Implementation 

procedure TForm1.CreateParams(var Params: TCreateParams);
begin
inherited;
 Params.WindowClass.Style := Params.WindowClass.Style or $00020000;
end;
 
Post a comment
Comments (5) Trackbacks (32)
 Copyright © 2008 #delphi / #C-sharp
 All trademarks and copyrights on this page are owned by their respective owners.
Powered By Geeklog 
Created this page in 0.39 seconds