create windows user using Delphi - windows

I need to create new windows user as administrator using Delphi
Thanks

you can use the NetUserAdd and NetUserSetGroups functions declarated in the JEDI Headers.
see this simple sample.
program ProjectAddNewUser;
{$APPTYPE CONSOLE}
uses
JclWin32,//Jedi Library
Windows,
SysUtils;
function CreateWinUser(const wServer, wUsername, wPassword, wGroup:WideString): Boolean;
var
Buf : USER_INFO_2;//Buf for the new user info
Err : NET_API_STATUS;
ParmErr : DWORD;
GrpUsrInfo: USER_INFO_0;//Buf for the group
wDummyStr : WideString;
begin
wDummyStr:='';
FillChar (Buf, SizeOf(USER_INFO_2), 0);
with Buf do
begin
usri2_name := PWideChar(wUsername);
usri2_full_name := PWideChar(wUsername);//You can add a more descriptive name here
usri2_password := PWideChar(wPassword);
usri2_comment := PWideChar(wDummyStr);
usri2_priv := USER_PRIV_USER;
usri2_flags := UF_SCRIPT OR UF_DONT_EXPIRE_PASSWD;
usri2_script_path := PWideChar(wDummyStr);
usri2_home_dir := PWideChar(wDummyStr);
usri2_acct_expires:= TIMEQ_FOREVER;
end;
GrpUsrInfo.usri0_name:=PWideChar(wGroup);
Err := NetUserAdd(PWideChar(wServer), 1, #Buf, #ParmErr);
Result := (Err = NERR_SUCCESS);
if Result then //NOw you must set the group for the new user
begin
Err := NetUserSetGroups(PWideChar(wServer),PWideChar(wGroup),0,#GrpUsrInfo,1);
Result := (Err = NERR_SUCCESS);
end;
end;
begin
if CreateWinUser('localhost', 'MyNewUser','ThePassword','MyWindowsGroup') then
Writeln('Ok')
else
Writeln('False');
Readln;
end.

I think the API call you need is NetUserAdd.
First, check if Delphi provides a wrapper for this call. If not, you'll have to write your own. If you don't know how to make Windows API calls from Delphi, you have some more research to do.

Related

Allow users choose hotkeys for icon/shortcut in Inno Setup

I'm looking to allow users, who run an installer made trough Inno Setup, to choose whether to use hot keys or not, and if yes, allow them to choose which hot keys use.
[Icons]
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; \
HotKey: "ctrl+alt+b"
Name: "{autoprograms}\{#MyAppName2}"; Filename: "{app}\{#MyAppExeName2}"; \
HotKey: "ctrl+alt+x"
This is my [Icons] section
The Icons section HotKey parameter cannot be modified at run time. So you will have to create the whole shortcut programmatically. An easy API that allows creating a shortcut with a hot key is WScript.Shell.CreateShortcut.
[Code]
var
App1HotKeyCombo: TNewComboBox;
App2HotKeyCombo: TNewComboBox;
const
NoneHotKey = 'None';
procedure InitializeWizard();
var
HotKeysPage: TWizardPage;
ALabel: TNewStaticText;
begin
Log('InitializeWizard');
HotKeysPage := CreateCustomPage(wpSelectTasks, 'Select your hot keys', '');
App1HotKeyCombo := TNewComboBox.Create(HotKeysPage);
App1HotKeyCombo.Parent := HotKeysPage.Surface;
App1HotKeyCombo.Left := ScaleX(200);
App1HotKeyCombo.Top := 0;
App1HotKeyCombo.Width := ScaleX(100);
App1HotKeyCombo.Style := csDropDownList;
App1HotKeyCombo.Items.Add(NoneHotKey);
App1HotKeyCombo.Items.Add('Ctrl+Alt+A');
App1HotKeyCombo.Items.Add('Ctrl+Alt+B');
App1HotKeyCombo.Items.Add('Ctrl+Alt+C');
App1HotKeyCombo.ItemIndex := 1;
ALabel := TNewStaticText.Create(HotKeysPage);
ALabel.Parent := HotKeysPage.Surface;
ALabel.Top := App1HotKeyCombo.Top + ScaleY(4);
ALabel.Left := 0;
ALabel.Caption := 'Hot key for application 1:';
ALabel.FocusControl := App1HotKeyCombo;
App2HotKeyCombo := TNewComboBox.Create(HotKeysPage);
App2HotKeyCombo.Parent := HotKeysPage.Surface;
App2HotKeyCombo.Left := App1HotKeyCombo.Left;
App2HotKeyCombo.Top := App1HotKeyCombo.Top + App1HotKeyCombo.Height + ScaleY(8);
App2HotKeyCombo.Width := App1HotKeyCombo.Width;
App2HotKeyCombo.Style := csDropDownList;
App2HotKeyCombo.Items.Assign(App1HotKeyCombo.Items);
App2HotKeyCombo.ItemIndex := 2;
ALabel := TNewStaticText.Create(HotKeysPage);
ALabel.Parent := HotKeysPage.Surface;
ALabel.Top := App2HotKeyCombo.Top + ScaleY(4);
ALabel.Left := 0;
ALabel.Caption := 'Hot key for application 2:';
ALabel.FocusControl := App2HotKeyCombo;
end;
procedure CreateShortCut(IconName, Path: string; AppHotKeyCombo: TNewComboBox);
var
WshShell: Variant;
ShellLink: Variant;
Msg: string;
begin
WshShell := CreateOleObject('WScript.Shell');
IconName := ExpandConstant(IconName) + '.lnk';
ShellLink := WshShell.CreateShortcut(IconName)
ShellLink.TargetPath := ExpandConstant(Path);
ShellLink.WindowStyle := SW_SHOWNORMAL;
if AppHotKeyCombo.Text <> NoneHotKey then
ShellLink.Hotkey := AppHotKeyCombo.Text;
ShellLink.Save;
Msg := 'Created "%s" icon pointing to "%s" with "%s" hotkey';
Log(Format(Msg, [IconName, ShellLink.TargetPath, ShellLink.Hotkey]));
end;
procedure CurStepChanged(CurStep: TSetupStep);
begin
if CurStep = ssInstall then
begin
CreateShortCut('{autoprograms}\MyProg1', '{app}\MyProg.exe', App1HotKeyCombo);
CreateShortCut('{autoprograms}\MyProg2', '{app}\MyProg.exe', App2HotKeyCombo);
end;
end;
A more robust API, is IShellLink. That's what Inno Setup uses internally. Though it requires considerably alot more code. For some examples, see Check for existence of a shortcut pointing to a specific target in Inno Setup.
You will also have to ensure that the shortcuts get deleted on uninstall. You can use UninstallDelete section for that.
If you do not want to code the shortcut creation, an alternative approach would be to use preprocessor to generate separate [Icons] entry for each shortcut you want to offer and use Check parameters to activate only the entry corresponding to the shortcut the user selects.

Problem with Delphi 10.3 Community PaintBox Repaint-Function

I am currently making a little Program in Delphi 10.3 Community Version 26.0.34749.6593. No additional components.
Essentially I draw on TPaintBox which is fitted in a Panel. Everything works fine so far, but when the objects are repainted via "PaintBox1.Repaint" the Objects got the wrong BrushStyle (bsSolid when they should have bsClear e.g.) Of course I tried to pin it down, but I got no luck. But I found out that at the following Point something doesn't work:
procedure TForm1.PaintBox1Paint(Sender: TObject);
var
i: Integer;
fig : ^TFigure;
apen: TPenStyle;
abrush: TBrushStyle;
color1,color2: TColor;
begin
aPen := PaintBox1.Canvas.Pen.Style;
aBrush := bsStyle;
color1 := PaintBox1.Canvas.Brush.Color;
color2 := PaintBox1.Canvas.Pen.Color;
for I:=0 to List.Count-1 do
begin
fig := List.Items[i];
case fig.Typ of
f_Kreis : begin
with Paintbox1.Canvas do
begin
pen.Style := fig.Pen;
Brush.Style := fig.Brush;
pen.Color := fig.PenColor;
brush.Color := fig.BrushColor;
Ellipse(fig.X,fig.Y,fig.X2,fig.Y2);
end;
end;
f_Rechteck : begin
with PaintBox1.Canvas do
begin
Pen.Style := fig.Pen;
Brush.Style := fig.Brush;
Pen.Color := fig.PenColor;
Brush.Color := fig.BrushColor;
Rectangle(fig.X,fig.Y,fig.X2,fig.Y2);
end;
end;
f_Line : begin
with PaintBox1.Canvas do
begin
pen.Style := fig.Pen;
brush.Style := fig.Brush;
pen.Color := fig.PenColor;
brush.Color := fig.BrushColor;
MoveTo(fig.X,Fig.Y);
LineTo(fig.X2,fig.Y2);
end;
end;
end;
end;
PaintBox1.Canvas.Pen.Style := aPen;
bsStyle := aBrush;
PaintBox1.Canvas.Brush.Color := color1;
PaintBox1.Canvas.Pen.Color := color2;
end;
So when the "Brush.Style := fig.Brush;"-Line is called, nothing happens. I went step by step and after these Line "Brush.Style" is still "bsSolid" even when "fig.Brush" is "bsClear"
For explanation: TFigure is my own class. It houses information about a drawing, such as a rectangle. It is the parent class.
Do I miss something. I really am out of Ideas. Can anyone tell me, why nothing happens?
Edit:
For testing I added the lines:
if Brush.Style <> fig.Brush then
ShowMessage('Warnung!');
under
Brush.Style := fig.Brush;
and it actually wont set it on false, though Brush.Style is bsSolid and fig.Brush is bsClear.
You have declared fig : ^TFigure;, but class instances are already references (pointers). Thus you are creating a pointer to reference, and using that pointer as if it were the reference.
Remove the pointer operator and declare
fig: TFigure;
I can't verify whether there are other errors in your code

Is there any simple way to use the FTP function in Lazarus code

I am a couple of months new to Lazarus. I have been trying to create a small FTP program that will send a small file after logging in. I have all the gooey stuff done and my only concern is the FTP part. I am getting a whole lot of errors and I have struggled to install the correct packages
My FTP code looks like this
function TModel.Send(LocalFile : string; remoteFile : string; RemoteDir : string) : boolean;
//===========================================================================
// **********************************************************************
// * Send a file to the FTP server *
// **********************************************************************
//---------------------------------------------------------------------------
var
rc : boolean;
begin
// Create the FTP Client object and set the FTP parameters
FTPClient := TFTPSend.Create;
with FTPClient do begin
TargetPort := cFtpProtocol;
TargetHost := fHost; // these were properties set somewhere else
UserName := fUserID;
Password := fPassword;
//-----------------------------------------------------------------------
// bail out if the FTP connect fails
if not LogIn then exit;
//------------------------------------------------------------------------
// Set filename to FTP
DirectFileName := LocalFile;
DirectFile := True;
//------------------------------------------------------------------------
// change directory if requested
if RemoteDir <> '' then ChangeWorkingDir(RemoteDir);
//------------------------------------------------------------------------
// STOR file to FTP server.
rc := StoreFile(RemoteFile,false);
//------------------------------------------------------------------------
// close the connection
LogOut;
//------------------------------------------------------------------------
// free the FTP client object
free;
//------------------------------------------------------------------------
end;
Result := rc;
//===========================================================================
end;
Thanks for your help.
Oh Lazarus XD. I'm not sure if there is any easy way though. I tried to do something similar a while back but i didn't get round to finishing it though.... But i did get the FTP to work take a look at my code below
begin
IdSMTP := TIdSMTP.Create(nil);
try
IdSMTP.Host := 'smtp.jonas.com';
IdSMTP.Port := 587;
IdSMTP.AuthType := satDefault;
IdSMTP.Username := 'server#jonas.com';
IdSMTP.Password := 'TeCat#!';
IdSMTP.Connect;
if IdSMTP.Authenticate then;
begin
IdMessage := TIdMessage.Create(nil);
try
IdMessage.From.Name := 'Jonas Server';
IdMessage.From.Address := 'server#jonas.com';
IdMessage.Subject := subject;
IdMessage.Body.AddStrings(message);
IdEmailAddressItem := IdMessage.Recipients.Add;
IdEmailAddressItem.Address := 'server#jonas.com';
IdSMTP.Send(IdMessage);
finally
IdMessage.Free;
end;
end;
IdSMTP.Disconnect;
finally
IdSMTP.Free;
end;
end;
I see you are using Synapse I can't remember what I used.... Its between indy, lnet or synapse. Just let me know if you need these packages I got them saved on my dropbox :) also check out THIS website it's a whole site dedicated to Laz.....GREAT ( ͡° ͜ʖ ͡°)

Control mplayer via pipe in MacOS with XE4

I finished controlling mplayer using pipe in Windows with -slave command (ftp://ftp2.mplayerhq.hu/MPlayer/DOCS/tech/slave.txt) but I don't know how to do that in MacOS.
Because MacOS developing is new for me, so, I still have Windows concept in my mind. So, I decided to use Pipe in OSX also.
This is my code to create Pipe:
uses
Macapi.ObjectiveC,Macapi.ObjCRuntime,
Macapi.Foundation;
var
LArray: array of Pointer;
LArgs: NSArray;
LTask: NSTask;
LPipe: NSPipe;
fileHandler: NSFileHandle;
function RawStr(const Str: string): Pointer;
begin
Result := TNSString.OCClass.stringWithUTF8String(PAnsiChar(UTF8String(Str)));
end;
procedure runInMacOSX;
var
data : NSData;
nsText : NSString;
begin
LArgs := TNSArray.Create;
setlength(LArray, 2);
LArray[0] := RawStr('/test.avi');
LArray[1] := RawStr('-slave');
LArgs := TNSArray.Wrap(TNSArray.Alloc.initWithObjects(#(LArray[0]), 2));
LTask := TNSTask.Wrap(TNSTask.Alloc.init);
LPipe := TNSPipe.Create;
fileHandler := TNSFileHandle.Create;
LTask.setLaunchPath(NSStr('/mplayer'));
LTask.setArguments(LArgs);
LTask.setStandardOutput((LPipe as ILocalObject).GetObjectID);
fileHandler := LPipe.fileHandleForReading;
LTask.launch;
end;
In button 1, I execute runInMacOSX: "/mplayer -slave /test.avi" and it executed correctly.
But in button 2, I'd like to pause the mplayer by sending "pause" command. I got stuck:
var
a: NSData;
str: NSString;
begin
str := TNSString.Wrap(TNSString.OCClass.stringWithUTF8String(PAnsiChar('pause')));
a := str.dataUsingEncoding(NSUTF8StringEncoding);
LPipe.fileHandleForWriting.writeData(a);
How can I solve this problem?
Thank you

Downloading a file in Delphi

A google search shows a few examples on how to download a file in Delphi but most are buggy and half of the time don't work in my experience.
I'm looking for a simple robust solution which will let me download a single exe (for updating my app) and will hold the execution of the current update thread until the download is done or errors out. The process is already threaded so the download code should hold execution until it's done (hopefully).
Here's two implementations, both seem very complicated
1. http://www.scalabium.com/faq/dct0116.htm
2. http://delphi.about.com/od/internetintranet/a/get_file_net.htm
Why not make use of Indy? If you use the TIdHTTP component, it's simple:
procedure TMyForm.DownloadFile;
var
IdHTTP1: TIdHTTP;
Stream: TMemoryStream;
Url, FileName: String;
begin
Url := 'http://www.rejbrand.se';
Filename := 'download.htm';
IdHTTP1 := TIdHTTP.Create(Self);
Stream := TMemoryStream.Create;
try
IdHTTP1.Get(Url, Stream);
Stream.SaveToFile(FileName);
finally
Stream.Free;
IdHTTP1.Free;
end;
end;
You can even add a progress bar by using the OnWork and OnWorkBegin events:
procedure TMyForm.IdHTTPWorkBegin(ASender: TObject; AWorkMode: TWorkMode;AWorkCountMax: Int64);
begin
ProgressBar.Max := AWorkCountMax;
ProgressBar.Position := 0;
end;
procedure TMyForm.IdHTTPWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
begin
ProgressBar.Position := AWorkCount;
end;
procedure TMyForm.DownloadFile;
var
IdHTTP1: TIdHTTP;
Stream: TMemoryStream;
Url, FileName: String;
begin
Url := 'http://www.rejbrand.se';
Filename := 'download.htm';
IdHTTP1 := TIdHTTP.Create(Self);
Stream := TMemoryStream.Create;
try
IdHTTP1.OnWorkBegin := IdHTTPWorkBegin;
IdHTTP1.OnWork := IdHTTPWork;
IdHTTP1.Get(Url, Stream);
Stream.SaveToFile(FileName);
finally
Stream.Free;
IdHTTP1.Free;
end;
end;
I'm not sure if these events fire in the context of the main thread, so any updates done to VCL components may have to be done using the TIdNotify component to avoid threading issues. Maybe someone else can check that.
The second approach is the standard way of using Internet resources using WinINet, a part of Windows API. I have used it a lot, and it has always worked well. The first approach I have never tried. (Neither is "very complicated". There will always be a few additional steps when using the Windows API.)
If you want a very simple method, you could simply call UrlMon.URLDownloadToFile. You will not get any fine control (at all!) about the download, but it is very simple.
Example:
URLDownloadToFile(nil,
'http://www.rejbrand.se',
PChar(ExtractFilePath(Application.ExeName) + 'download.htm'),
0,
nil);
For people that has later version of delphi, you can use this:
var
http : TNetHTTPClient;
url : string;
stream: TMemoryStream;
begin
http := TNetHTTPClient.Create(nil);
stream := TMemoryStream.Create;
try
url := YOUR_URL_TO_DOWNLOAD;
http.Get(url, stream);
stream.SaveToFile('D:\Temporary\1.zip');
finally
stream.Free;
http.Free;
end;
end;
Using URLMon.
errcode := URLMon.URLDownloadToFile(nil,
PChar('http://www.vbforums.com/showthread.php?345726-DELPHI-Download-Files'),
PChar( 'a:\download.htm'),
0,
nil);
if errcode > 0 then
showmessage('Error while downloading: ' + inttostr(errcode));

Resources