NetUserGetLocalGroups - how to call it? - delphi

I am using Delphi 2010, latest version (from repository) of JEDI WinAPI and Windows Security Code Library (WSCL).
I don't know how to call the NetUserSetGroups function. The way I am doing it, it is throwing an exception:
Access violation at address 5B8760BE
in module 'netapi32.dll'. Write of
address 00000000.
Following is my code:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, JwaWindows, JwsclSid;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
NetApiStatus: NET_API_STATUS;
dwEntriesRead, dwEntriesTotal: PDWORD;
lgi01: LOCALGROUP_USERS_INFO_0;
username: PChar;
begin
username := 'Elise';
NetApiStatus := NetUserGetLocalGroups(nil, PChar(username), 0, LG_INCLUDE_INDIRECT, PByte(lgi01),
MAX_PREFERRED_LENGTH, dwEntriesRead, dwEntriesTotal);
if NetApiStatus = NERR_SUCCESS then
showmessage('Total groups user belongs to: ' + IntTostr(dwEntriesTotal^));
end;
end.
Would appreciate if someone could kindly show me how I can call this function?
Thanks in advance.

This code works fine for me:
type
LocalGroupUsersInfo0Array = array[0..ANYSIZE_ARRAY-1] of LOCALGROUP_USERS_INFO_0;
PLocalGroupUsersInfo0Array = ^LocalGroupUsersInfo0Array;
procedure TForm3.Button3Click(Sender: TObject);
var
nas: NET_API_STATUS;
PLGUIA: PLocalGroupUsersInfo0Array;
Count: DWORD;
Total: DWORD;
i: Integer;
begin
PLGUIA := nil;
nas := NetUserGetLocalGroups(nil, PChar('rweijnen'), 0, LG_INCLUDE_INDIRECT,
PByte(PLGUIA), MAX_PREFERRED_LENGTH, #Count, #Total);
if (nas = NERR_Success) or (nas = ERROR_MORE_DATA) then
begin
for i := 0 to Count - 1 do
begin
Memo1.Lines.Add(Format('name=%s', [PLGUIA^[i].lgrui0_name]));
end;
if Assigned(PLGUIA) then
NetApiBufferFree(PLGUIA);
end;
end;

Related

try to load powerpoint file but failed

After installed MS Powerpoint Viewer, I run the code below
unit Unit1;
interface
uses Comobj,
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls,Office_TLB, office97, PowerPointXP;//, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Panel1: TPanel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
const
ppShowTypeSpeaker = 1;
ppShowTypeInWindow = 1000;
SHOW_FILE = 'C:\Users\myname\Downloads\practicepowerpoint.ppt';
var
oPPTApp: OleVariant;
oPPTPres: OleVariant;
screenClasshWnd: HWND;
pWidth, pHeight: Integer;
function PixelsToPoints(Val: Integer; Vert: Boolean): Integer;
begin
if Vert then
Result := Trunc(Val * 0.75)
else
Result := Trunc(Val * 0.75);
end;
begin
try
oPPTApp := CreateOleObject('PowerPoint.Application');
except
showmessage('no ppt');;
exit;
end;
oPPTPres := oPPTApp.Presentations.Open(SHOW_FILE, True, True, False);
pWidth := PixelsToPoints(Panel1.Width, False);
pHeight := PixelsToPoints(Panel1.Height, True);
oPPTPres.SlideShowSettings.ShowType := ppShowTypeSpeaker;
oPPTPres.SlideShowSettings.Run.Width := pWidth;
oPPTPres.SlideShowSettings.Run.Height := pHeight;
screenClasshWnd := FindWindow('screenClass', nil);
Windows.SetParent(screenClasshWnd, Panel1.Handle);
end;
end.
the line
oPPTApp := CreateOleObject('PowerPoint.Application');
causes error.
Just wonder if I have to install Powerpoint rather than Powerpoint Viewer before run the code
Your comment welcome
Yes, "Powerpoint.Application" starts Powerpoint. I would not recommend to use the powerpoint viewer because it is a really old program (from 2010 and before). I am not shure if you can automate this program.
The new way to view pptx is Powerpoint online

Incompatible types when add unit in main unit

I'm a new Delphi developer and there are strange happening problems. I have to add an unit called Filters in my main called Unit1 (default name). But, alaways when I try to run the code: [Error] Unit1.pas(48): Incompatible types. If I remore Filters from "uses", the code run. Someone knows how to solve this.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, StdCtrls;
type
ArrayOfInteger = array of integer;
TMain = class(TForm)
MainMenu1: TMainMenu;
options: TMenuItem;
checkResult: TMenuItem;
GerarJogos1: TMenuItem;
exit: TMenuItem;
edtGame: TEdit;
mmoResult: TMemo;
btnConfirm: TButton;
procedure btnConfirmClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Main: TMain;
implementation
uses Utils, Error, Filters;
{$R *.dfm}
procedure TMain.btnConfirmClick(Sender: TObject);
var utils: TUtils;
var filter: TFilters;
var error: Errors;
var num: ArrayOfInteger;
var nstr, str: string;
begin
SetLength(num, 6);
FillChar(str, SizeOf(str), #0);
FillChar(nstr, SizeOf(nstr), #0);
utils := TUtils.Create;
filter := TFilters.Create;
error := Errors.Create;
str := edtGame.Text;
num := utils.strInput(str);
Line 48: num := utils.strInput(str);
unit Filters;
interface
uses Classes, SysUtils, Math;
type
ArrayOfInteger = array of integer;
TFilters = class
private
protected
public
Constructor Create;
// function isPair(number: integer): Boolean;
//function fSum(numbers: ArrayOfInteger): Boolean;
//function fNLNPNO(numbers: ArrayOfInteger): integer;
end;
implementation
Constructor TFilters.Create;
begin
Inherited Create;
end;
You have ArrayOfInteger defined in two different units, which is causing this error. Remove the definition from one of the units or specify in var num: ArrayOfInteger which definition you want you use, like: TMain.ArrayOfInteger

delphi using TWebBrowser without using the TForm

I want to develop a COM DLL in delphi that will internally create a window or form and then display the TWebBrowser navigation on that. The reason of this is I don't want to use the TWebbrowser control to be drag on each of my client app. This client app simply use this DLL because this DLL will also has some other logic that is not relevent to mention here.
Please help me how to achieve this
You should heed the reservations of the other posters, but if you want a dll that launches a TWebBrowser this should get you started. It compiles and runs but has only been very briefly tested.
Hope that helps.
library BrowserDLL;
uses
ShareMem,
SysUtils,
Classes,
Forms,
Windows,
DLLMainForm in 'DLLMainForm.pas' {MainForm};
{$R *.RES}
function ShowBrowserForm(AHandle: THandle; const AURL : String): Longint; stdcall;
begin
Application.Handle := AHandle;
result := TMainForm.ShowForm(AURL);
end;
exports
ShowBrowserForm;
var
DLLApplication : TApplication;
procedure DLLHandler(Reason: Integer);
begin
case Reason of
DLL_PROCESS_DETACH:
begin
Application := DLLApplication;
end;
end;
end;
begin
DLLApplication := Application;
DLLproc:=#DLLHandler;
end.
unit DLLMainForm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
OleCtrls, SHDocVw, ExtCtrls;
type
TMainForm = class(TForm)
wb1: TWebBrowser;
private
FURL: string;
procedure SetUrl(const Value: string);
public
class function ShowForm(const AURL: String): Longint;
property URL : string read FURL write SetUrl;
end;
implementation
{$R *.DFM}
{ TBrowserForm }
procedure TMainForm.SetUrl(const Value: string);
begin
if FURL <> Value then begin
FURL := Value;
wb1.Navigate(Value);
end;
end;
class function TMainForm.ShowForm(const AURL : String): Longint;
var
form: TMainForm;
begin
form := Create(Application);
try
form.URL := AURL;
form.ShowModal;
Result := LongInt(form);
finally
FreeAndNil(form);
end;
end;
end.
unit LauncherMainform;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, OleCtrls, SHDocVw;
type
TShowDllForm = function(AHandle : THandle; const AUrl : String) : LongInt; stdcall;
TMainForm = class(TForm)
edt1: TEdit;
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
LibHandle : THandle;
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
procedure TMainForm.btn1Click(Sender: TObject);
var
DLLProc : TShowDllForm;
begin
LibHandle := LoadLibrary(PChar('BrowserDLL.dll'));
if LibHandle <> 0 then begin
#DLLProc := GetProcAddress(LibHandle,'ShowBrowserForm');
if (#DLLProc <> nil) then try
DLLProc(Application.Handle, edt1.Text);
except
on E:Exception do
ShowMessage('Error Running dll.' + #13#10 + E.Message);
end;
end else
ShowMessage('Error Loading dll');
end;
end.

Delphi 6 indy connection hanging

I am having small issue. After I make a connection between the server and client. I would close the client, and then the server, but the server hangs and sends me a "program has crashed" I think the problem I am having is that the server doesn't recognize a client has disconnected, and still thinks the client is active. Here is the source code:
client:
unit client;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, Winsock, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient;
type
TForm1 = class(TForm)
IdTCPClient1: TIdTCPClient;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function GetIpFromDns(HostName: string): string;
type
tAddr = array[0..100] of PInAddr;
pAddr = ^tAddr;
var
I: Integer;
WSA: TWSAData;
PHE: PHostEnt;
P: pAddr;
begin
Result := HostName;
WSAStartUp($101, WSA);
try
PHE := GetHostByName(pChar(HostName));
if (PHE <> nil) then
begin
P := pAddr(PHE^.h_addr_list);
I := 0;
while (P^[i] <> nil) do
begin
Result := (inet_nToa(P^[i]^));
Inc(I);
end;
end;
except
end;
WSACleanUp;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
IdTCPClient1.Host := GetIpFromDns('example.no-ip.org');
IdTCPClient1.Port := 9000;
IdTCPClient1.Connect;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if IdTCPclient1.Connected = True then
IdTCPClient1.Disconnect
else
end;
end.
Server:
unit server;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, IdBaseComponent, IdComponent, IdTCPServer;
type
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
procedure IdTCPServer1Disconnect(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
showmessage('client connected');
end;
procedure TForm1.IdTCPServer1Disconnect(AThread: TIdPeerThread);
begin
showmessage('client disconnected');
end;
end.
it may not look like I set up a listening port for indy, but I did in the object inspector page. For some reason if I put IdTCPServer.DefaultPort and Active in the form create it throws more errors.
I also tried IdTCPClient1.DisconnectSocket but no luck there either.
Do I need to create something on the server side to check the connections periodically? if so, what would be best way to do that?

LsaAddAccountRights not working for me

Using: Delphi 2010 and the JEDI Windows API and JWSCL
I am trying to assign the Logon As A Service privilege to a user using LsaAddAccountRights function but it does not work ie. after the function returns, checking in Group Policy Editor shows that the user still does not have the above mentioned privilege.
I'm running the application on Windows XP.
Would be glad if someone could point out what is wrong in my code:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, JwaWindows, JwsclSid;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function AddPrivilegeToAccount(AAccountName, APrivilege: String): DWORD;
var
lStatus: TNTStatus;
lObjectAttributes: TLsaObjectAttributes;
lPolicyHandle: TLsaHandle;
lPrivilege: TLsaUnicodeString;
lSid: PSID;
lSidLen: DWORD;
lTmpDomain: String;
lTmpDomainLen: DWORD;
lTmpSidNameUse: TSidNameUse;
lPrivilegeWStr: String;
begin
ZeroMemory(#lObjectAttributes, SizeOf(lObjectAttributes));
lStatus := LsaOpenPolicy(nil, lObjectAttributes, POLICY_LOOKUP_NAMES, lPolicyHandle);
if lStatus <> STATUS_SUCCESS then begin
Result := LsaNtStatusToWinError(lStatus);
Exit;
end;
try
lTmpDomainLen := DNLEN; // In 'clear code' this should be get by LookupAccountName
SetLength(lTmpDomain, lTmpDomainLen);
lSidLen := SECURITY_MAX_SID_SIZE;
GetMem(lSid, lSidLen);
try
if LookupAccountName(nil, PChar(AAccountName), lSid, lSidLen, PChar(lTmpDomain),
lTmpDomainLen, lTmpSidNameUse) then begin
lPrivilegeWStr := APrivilege;
lPrivilege.Buffer := PChar(lPrivilegeWStr);
lPrivilege.Length := Length(lPrivilegeWStr) * SizeOf(Char);
lPrivilege.MaximumLength := lPrivilege.Length;
lStatus := LsaAddAccountRights(lPolicyHandle, lSid, #lPrivilege, 1);
Result := LsaNtStatusToWinError(lStatus);
end
else
Result := GetLastError;
finally
FreeMem(lSid);
end;
finally
LsaClose(lPolicyHandle);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
AddPrivilegeToAccount('Sam', 'SeServiceLogonRight');
end;
end.
Thanks in advance.
To be able to use LsaAddAccountRights you should open policy handle with additional POLICY_CREATE_ACCOUNT flag (POLICY_CREATE_ACCOUNT | POLICY_LOOKUP_NAMES) in LsaOpenPolicy or use MAXIMUM_ALLOWED instead of both flags.

Resources