How to print QR Code on ESC/POS Printer by using Delphi? - delphi

this is a test code for printing on EPSON Thermal Printer.
All is working fine except the QR code part.
Printer just get stuck and I need to reset it manually.
I am following the documentation from this page: https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=143
I am using Delphi 10.3.
What am I doing wrong?
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls ,
Vcl.Printers,
WinProcs, WinTypes;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure setupPrinter(const printerName: string);
procedure PrintTest;
function DirectToPrinter(S: AnsiString; NextLine: Boolean): Boolean;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
TPrnBuffRec = packed record
bufflength: Word;
Buff_1: array[0..255] of AnsiChar;
end;
function tform1.DirectToPrinter(S: AnsiString; NextLine: Boolean): Boolean;
var
Buff: TPrnBuffRec;
TestInt: Integer;
i: integer;
Device: PChar;
Driver: PChar;
Port: PChar;
begin
TestInt := PassThrough;
if Escape(Printer.Handle, QUERYESCSUPPORT, SizeOf(TESTINT), #testint, nil) > 0 then
begin
if NextLine then S := S + #13 + #10;
StrPCopy(Buff.Buff_1, S);
Buff.bufflength := StrLen(Buff.Buff_1);
Escape(Printer.Canvas.Handle, Passthrough, 0, #buff, nil);
Result := True;
end
else
Result := False;
end;
Procedure tform1.PrintTest;
var
store_len: integer;
store_pl: Byte;
store_ph : Byte;
qrData: string;
Begin
setupPrinter('EPSON TM-T88V Receipt');
Printer.BeginDoc;
DirectToPrinter(Chr(27)+Chr(64), false); //init
DirectToPrinter(Chr(27)+chr(97)+chr(49), false);
// DirectToPrinter(Chr(27)+chr(33)+Chr(8), false); //font select
DirectToPrinter(Chr(27)+chr(45)+Chr(49), false); //underline
DirectToPrinter('Test!', true);
DirectToPrinter(Chr(27)+chr(100)+Chr(5), false); //feed 5 lines
DirectToPrinter(Chr(27)+chr(45)+Chr(48), false); //underline
DirectToPrinter(Chr(27)+chr(97)+chr(48), false); //align-center
DirectToPrinter('Hello world!', true);
DirectToPrinter(
chr(29)+chr(72)+chr(49),
False
); //show content aboce barcode
//
DirectToPrinter(
chr(29)+chr(102)+chr(48),
False
); //font A
// DirectToPrinter(
// chr(29)+chr(104)+chr(50),
// False
//
// ); //height
// DirectToPrinter(
// chr(29)+chr(119)+chr(50),
// False
//
// ); //width
DirectToPrinter(
chr(29)+chr(107)+chr(4)+'*0001443AB*',
true
); //ean 39
DirectToPrinter(
chr(29)+chr(107)+chr(73)+chr(13)+chr(123)+Chr(65)+'8600123456789',
true
); // ean 128
DirectToPrinter(
chr(29)+chr(107)+chr(67)+Chr(12)+'860012345678',
true
); // ean 13
//*************************QR CODE ****************/
qrData := 'https://www.stackoverflow.com';
store_len:= Length(qrData)+3;
store_pl := store_len mod 256;
store_ph := Trunc(store_len / 256);
DirectToPrinter(
Chr(29)+chr(40)+Chr(107)+char(3)+chr(0)+Chr(49)+Chr(65)+
Chr(49)+Chr(0),false
); //QR Code: Select the model
DirectToPrinter(
Chr(29)+chr(40)+Chr(107)+chr(3)+Chr(0)+Chr(49)+Chr(67)+
Chr(2),false
); //QR Code: Set the size of module
//QR Code: Select the error correction level
DirectToPrinter(
Chr(29)+chr(40)+Chr(107)+chr(3)+Chr(0)+Chr(49)+Chr(69)+
Chr(48),false
);
//QR Code: Store the data in the symbol storage area
DirectToPrinter(
Chr(29)+chr(40)+Chr(107)+
chr(store_pl)+Chr(store_ph)+
Chr(49)+Chr(80)+Chr(48)+qrData,
false
);
// QR Code: Print the symbol data in the symbol storage area
DirectToPrinter(
Chr(29)+chr(40)+Chr(107)+chr(store_pl)+chr(store_ph)+Chr(49)+Chr(81)+Chr(48),true
);
DirectToPrinter(Chr(27)+Chr(112)+Chr(48)+Chr(60)+Chr(120), false); //drawer pulse
Printer.EndDoc;
End;
procedure TForm1.setupPrinter(const printerName: string);
var
Buff: TPrnBuffRec;
TestInt: Integer;
i: integer;
Device: PChar;
Driver: PChar;
Port: PChar;
HDeviceMode: THandle;
begin
Printer.PrinterIndex := -1;
GetMem(Device, 255);
GetMem(Driver, 255);
GetMem(Port, 255);
for I := 0 to Printer.Printers.Count - 1 do
begin
if Printer.Printers[I] = printername then
begin
printer.PrinterIndex := I;
printer.getprinter(Device, Driver, Port, HdeviceMode);
StrCat(Device, ',');
StrCat(Device, Driver);
StrCat(Device, Port);
WriteProfileString('windows', 'device', Device);
StrCopy(Device, 'windows');
SendMessage(HWND_BROADCAST, WM_WININICHANGE,
0, Longint(#Device));
break;
end;
end;
FreeMem(Device, 255);
FreeMem(Driver, 255);
FreeMem(Port, 255);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
PrintTest;
end;
end.

The length is specified incorrectly in the command to print the QR code.
The same data length as the command that stores the QR code, which is the previous command, is specified, but this is a fixed value of 3 bytes.
GS ( k <Function 181>
This part:
// QR Code: Print the symbol data in the symbol storage area
DirectToPrinter(
Chr(29)+chr(40)+Chr(107)+chr(store_pl)+chr(store_ph)+Chr(49)+Chr(81)+Chr(48),true
);
Please change to here:
// QR Code: Print the symbol data in the symbol storage area
DirectToPrinter(
Chr(29)+chr(40)+Chr(107)+chr(3)+chr(0)+Chr(49)+Chr(81)+Chr(48),true
);

You have a problem with function 165: QR Code: Select the model
You put: Chr(29)+chr(40)+Chr(107)+char(3)+chr(0)+Chr(49).....
And is: Chr(29)+chr(40)+Chr(107)+chr(4)+chr(0)+Chr(49).....

Related

EnumResouceNames and .RC files

In a Delphi package, I have a .RC file, with following content:
STRINGTABLE
BEGIN
1000, "First line."
1001, "Second line"
1002, "Last line"
END
The .RC file was included in .dpk source, and the following command is fully functional:
LoadStr(1000) {returns "First line."}
I want to get a complete list of lines in string table.
Using EnumResourceNames, it just returns content of default .res file (forms contents, etc). No content of .RC returned using EnumResourceNames.
Here is the code calling EnumResourceNames:
unit uxbResources;
interface
uses
System.SysUtils, System.Classes, WinApi.Windows;
type
TxbResources = class
private
FItems: TStringList;
public
constructor Create;
destructor Destroy; override;
procedure Load(const AHandle: THandle);
property Items: TStringList read FItems;
end;
implementation
function EnumResNamesProc( module: HMODULE; restype, resname: PChar; list: TStrings): Integer; stdcall;
begin
if HiWord( Cardinal(resname) ) <> 0 then
list.add( ' '+resname )
else
list.add( format(' #%d',[loword(cardinal(resname))]));
result := 1;
end;
function StockResourceType( restype: PChar ): string;
const
restypenames: array [1..22] of string = (
'RT_CURSOR', // = MakeIntResource(1);
'RT_BITMAP', // = MakeIntResource(2);
'RT_ICON', // = MakeIntResource(3);
'RT_MENU', // = MakeIntResource(4);
'RT_DIALOG', // = MakeIntResource(5);
'RT_STRING', // = MakeIntResource(6);
'RT_FONTDIR',// = MakeIntResource(7);
'RT_FONT', // = MakeIntResource(8);
'RT_ACCELERATOR',// = MakeIntResource(9);
'RT_RCDATA', // = MakeIntResource(10);
'RT_MESSAGETABLE',// = MakeIntResource(11);
// DIFFERENCE = 11;
'RT_GROUP_CURSOR',// = MakeIntResource(DWORD(RT_CURSOR + DIFFERENCE));
'UNKNOWN', // 13 not used
'RT_GROUP_ICON', // = MakeIntResource(DWORD(RT_ICON + DIFFERENCE));
'UNKNOWN', // 15 not used
'RT_VERSION', // = MakeIntResource(16);
'RT_DLGINCLUDE', // = MakeIntResource(17);
'UNKNOWN',
'RT_PLUGPLAY', // = MakeIntResource(19);
'RT_VXD', // = MakeIntResource(20);
'RT_ANICURSOR', // = MakeIntResource(21);
'RT_ANIICON' // = MakeIntResource(22);
);
var
resid: Cardinal absolute restype;
begin
if resid In [1..22] then
result := restypenames[resid]
else
result := 'UNKNOWN';
end;
function enumResTypesProc( module: HMODULE; restype: PChar; list: TStrings): Integer; stdcall;
var
s: string;
begin
if HiWord( cardinal(restype) ) <> 0 then
s := restype
else
s := format('Stock type %d: %s',[LoWord(cardinal(restype)), StockResourcetype( restype )]);
if (Pos('stringtable', Lowercase(s)) > 0)
or (Pos('documento', Lowercase(s)) > 0) then
begin
sleep(1);
end;
list.Add(s);
EnumResourceNames( module, restype, #enumResNamesProc, Integer(list));
Result := 1;
end;
constructor TxbResources.Create;
begin
inherited;
FItems := TStringList.Create;
end;
destructor TxbResources.Destroy;
begin
FItems.Free;
inherited;
end;
procedure TxbResources.Load(const AHandle: THandle);
var
s: string;
begin
if not Enumresourcetypes(AHandle, #EnumResTypesProc, Integer(FItems)) then
SysErrorMessage(GetLastError);
end;
...
with TxbResources.Create do
Load(LoadPackage('mypackage.bpl');
My question is: How could I get all lines from STRINGTABLE, since I do not know the id?
Thanks

Force Application Window to Foreground from Background (via msg from other process)

I am using this code with Mutex and custom Message to force the 1st instance of application to appear on screen if the user tries to start a 2nd instance. There must be only 1 instance of my app running.
It seems that this code is not working properly under Win10, it makes the Application Icon to flick on TaskBar, but the actual Window is not appearing on top of other Windows.
function ForceForeground(AppHandle:HWND): boolean;
const
SPI_GETFOREGROUNDLOCKTIMEOUT = $2000;
SPI_SETFOREGROUNDLOCKTIMEOUT = $2001;
var
ForegroundThreadID: DWORD;
ThisThreadID : DWORD;
timeout : DWORD;
OSVersionInfo : TOSVersionInfo;
Win32Platform : Integer;
begin
if IsIconic(AppHandle) then ShowWindow(AppHandle, SW_RESTORE);
if (GetForegroundWindow = AppHandle) then Result := true else
begin
Win32Platform := 0;
OSVersionInfo.dwOSVersionInfoSize := SizeOf(OSVersionInfo);
if GetVersionEx(OSVersionInfo) then Win32Platform := OSVersionInfo.dwPlatformId;
{ Windows 98/2000 doesn't want to foreground a window when some other window has keyboard focus}
if ((Win32Platform = VER_PLATFORM_WIN32_NT) and (OSVersionInfo.dwMajorVersion > 4)) or
((Win32Platform = VER_PLATFORM_WIN32_WINDOWS) and ((OSVersionInfo.dwMajorVersion > 4) or
((OSVersionInfo.dwMajorVersion = 4) and (OSVersionInfo.dwMinorVersion > 0)))) then
begin
Result := false;
ForegroundThreadID := windows.GetWindowThreadProcessID(GetForegroundWindow,nil);
ThisThreadID := windows.GetWindowThreadPRocessId(AppHandle,nil);
if AttachThreadInput(ThisThreadID, ForegroundThreadID, true) then
begin
BringWindowToTop(AppHandle);
SetForegroundWindow(AppHandle);
AttachThreadInput(ThisThreadID, ForegroundThreadID, false);
Result := (GetForegroundWindow = AppHandle);
end;
if not Result then
begin
SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, #timeout, 0);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(0), SPIF_SENDCHANGE);
BringWindowToTop(AppHandle);
SetForegroundWindow(AppHandle);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(timeout), SPIF_SENDCHANGE);
Result := (GetForegroundWindow = AppHandle);
if not Result then
begin
ShowWindow(AppHandle,SW_HIDE);
ShowWindow(AppHandle,SW_SHOWMINIMIZED);
ShowWindow(AppHandle,SW_SHOWNORMAL);
BringWindowToTop(AppHandle);
SetForegroundWindow(AppHandle);
end;
end;
end else
begin
BringWindowToTop(AppHandle);
SetForegroundWindow(AppHandle);
end;
Result := (GetForegroundWindow = AppHandle);
end;
end;
I have managed to make a complete demo program that shows my suggestion in the 2nd comment above. Create a new VCL application. Rename the form to MainForm, place a TListBox on it, align it to client, rename it to ListBox, then make empty events for the form's OnCreate and OnDestroy.
Then copy/paste this PASCAL source into your main form's PAS file from right after "interface", overwriting the code already there:
uses
Winapi.Windows, Winapi.Messages,
System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
CONST
WM_PEEK = WM_USER+1234;
type
TMainForm = class(TForm)
ListBox: TListBox;
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
Running : HWND;
PROCEDURE PEEK(VAR MSG : TMessage); MESSAGE WM_PEEK;
PROCEDURE CopyData(VAR MSG : TMessage); MESSAGE WM_COPYDATA;
PROCEDURE BringForward(Sender : TObject);
PROCEDURE SendString(H : HWND ; CONST S : STRING ; E : TEncoding);
FUNCTION CommandLine : STRING;
FUNCTION MakeAtomName(H : HWND) : STRING;
FUNCTION FindGlobalAtom(CONST S : STRING) : ATOM;
FUNCTION AddGlobalAtom(CONST S : STRING) : ATOM;
FUNCTION GetGlobalAtomName(H : ATOM) : STRING;
FUNCTION AtomNameToHandle(CONST S : STRING) : HWND;
FUNCTION DeleteGlobalAtom(A : ATOM) : DWORD;
public
{ Public declarations }
PROCEDURE LOG(CONST S : STRING);
end;
var
MainForm: TMainForm;
implementation
USES System.Character;
{$R *.dfm}
PROCEDURE TMainForm.FormDestroy(Sender : TObject);
VAR
S : STRING;
A : ATOM;
BEGIN
S:=MakeAtomName(0);
REPEAT
A:=FindGlobalAtom(S);
IF A=0 THEN BREAK;
UNTIL DeleteGlobalAtom(A)<>ERROR_SUCCESS
END;
FUNCTION TMainForm.AddGlobalAtom(CONST S : STRING) : ATOM;
BEGIN
Result:=WinAPI.Windows.GlobalAddAtom(PChar(S))
END;
FUNCTION TMainForm.MakeAtomName(H : HWND) : STRING;
CONST
L = 8*SizeOf(POINTER); // 32 or 64 (number of bits in a handle)
VAR
S : STRING;
I : Cardinal;
C : CHAR;
BEGIN
Result:=ChangeFileExt(ExtractFileName(ParamStr(0)),''); S:='';
FOR C IN Result DO IF CharInSet(C,['A'..'Z','a'..'z']) THEN S:=S+C;
WHILE LENGTH(S)<L DO S:=S+S;
SetLength(S,L);
Result:='';
FOR I:=1 TO L DO BEGIN
IF H AND $01<>0 THEN C:=S[I].ToUpper ELSE C:=S[I].ToLower;
Result:=C+Result; H:=H SHR 1
END
END;
FUNCTION TMainForm.AtomNameToHandle(CONST S : STRING) : HWND;
VAR
C : CHAR;
BEGIN
Result:=0;
FOR C IN S DO BEGIN
Result:=Result SHL 1;
IF CharInSet(C,['A'..'Z']) THEN Result:=Result OR 1
END
END;
PROCEDURE TMainForm.BringForward(Sender : TObject);
BEGIN
SetForegroundWindow(Running);
SendString(Running,CommandLine,TEncoding.UTF8);
ExitProcess(0)
END;
FUNCTION TMainForm.CommandLine : STRING;
BEGIN
Result:=GetCommandLine
END;
PROCEDURE TMainForm.CopyData(VAR MSG : TMessage);
VAR
CDS : PCopyDataStruct;
S : STRING;
B : TBytes;
BEGIN
CDS:=PCopyDataStruct(MSG.LParam);
SetLength(B,CDS.cbData);
MOVE(CDS.lpData^,POINTER(B)^,LENGTH(B));
S:=TEncoding.UTF8.GetString(B);
LOG('Child['+IntToHex(MSG.WParam)+']: '+S)
END;
FUNCTION TMainForm.DeleteGlobalAtom(A : ATOM) : DWORD;
BEGIN
SetLastError(ERROR_SUCCESS);
WinAPI.Windows.GlobalDeleteAtom(A);
Result:=GetLastError
END;
FUNCTION TMainForm.FindGlobalAtom(CONST S : STRING) : ATOM;
BEGIN
Result:=WinAPI.Windows.GlobalFindAtom(PChar(S))
END;
PROCEDURE TMainForm.FormCreate(Sender : TObject);
VAR
A : ATOM;
H : HWND;
S,T : STRING;
BEGIN
S:=MakeAtomName(Handle);
REPEAT
A:=FindGlobalAtom(S);
IF A=0 THEN BREAK;
T:=GetGlobalAtomName(A); H:=AtomNameToHandle(T);
IF H<>Handle THEN
IF SendMessage(H,WM_PEEK,NativeInt(A),NativeInt(H))=NativeInt(A)+NativeInt(H) THEN BREAK
UNTIL DeleteGlobalAtom(A)<>ERROR_SUCCESS;
IF A=0 THEN BEGIN
A:=AddGlobalAtom(S);
LOG('Main['+IntToHex(Handle)+'] : '+CommandLine)
END ELSE BEGIN
Running:=H; OnDestroy:=NIL; OnActivate:=BringForward;
BorderStyle:=TFormBorderStyle.bsNone;
SetBounds(-10000,-10000,10,10)
END
END;
FUNCTION TMainForm.GetGlobalAtomName(H : ATOM) : STRING;
BEGIN
SetLength(Result,255);
SetLength(Result,WinAPI.Windows.GlobalGetAtomName(H,#Result[LOW(Result)],LENGTH(Result)))
END;
PROCEDURE TMainForm.LOG(CONST S : STRING);
BEGIN
ListBox.ItemIndex:=ListBox.Items.Add(S)
END;
PROCEDURE TMainForm.PEEK(VAR MSG : TMessage);
BEGIN
MSG.Result:=NativeInt(MSG.WParam)+MSG.LParam
END;
PROCEDURE TMainForm.SendString(H : HWND ; CONST S : STRING ; E : TEncoding);
VAR
B : TBytes;
CDS : TCopyDataStruct;
BEGIN
B:=E.GetBytes(S);
CDS.dwData:=1;
CDS.cbData:=LENGTH(B);
CDS.lpData:=POINTER(B);
SendMessage(H,WM_COPYDATA,Handle,NativeInt(#CDS));
END;
end.
When you initially run the application, it'll show the command line in the ListBox. If you then run it again, it'll detect the other window already exists (using a bit-encoded Global Atom to signify the initial instance's main form Handle) and move it to the foreground (after placing its own window out-of-screen, and thus being an invisible foreground window). It'll then use WM_COPYDATA to send the new instance's command line to the initial instance, and the initial instance will then log the received command line to the listbox.
Caveats:
It's the MAIN form that is brought to front, receives and processes the command line. If you have child forms open, the behaviour is undefined (as in: I haven't tested this).
The Atom name is a 32- (or 64-) character long name, consisting of a repeated pattern of the program executable's A-Z characters. If your application doesn't have A-Z character in its name, this will fail.
To test if the Window decoded from the Global Atom is one we recognize, I call a WM_PEEK message on that window. This could lead to an unexpected message call into a foreign application, if your main instance is allowed to start (and create the Atom) and then not terminate properly (so that the Atom is deleted in FormDestroy).

How to get all of the registered file formats from VCL.Graphics... but 64bit

In my 32bit application I'm using the FindRegisteredPictureFileFormats unit provided by Cosmin Prund => (How to get all of the supported file formats from Graphics unit?).
I need the same but for 64bit. David Heffernan replied it had already a 64bit version. Can this code be made public ?
Thanks a lot !!
I believe that this unit does what you are looking for. I've testing it on 32 bit and 64 bit Windows, with runtime packages and without. I've not tested it with top-down memory allocation, but I don't believe that there are pointer truncation bugs.
unit FindRegisteredPictureFileFormats;
{$POINTERMATH ON}
interface
uses Classes, Contnrs;
// Extracts the file extension + the description; Returns True if the hack was successful,
// False if unsuccesful.
function GetListOfRegisteredPictureFileFormats(List: TStrings): Boolean;
// This returns the list of TGraphicClass registered; True for successful hack, false
// for unsuccesful hach
function GetListOfRegisteredPictureTypes(List: TClassList): Boolean;
implementation
uses Graphics;
type
TRelativeCallOpcode = packed record
OpCode: Byte;
Offset: Integer;
end;
PRelativeCallOpcode = ^TRelativeCallOpcode;
TLongAbsoluteJumpOpcode = packed record
OpCode: array [0 .. 1] of Byte;
Destination: Cardinal;
end;
PLongAbsoluteJumpOpcode = ^TLongAbsoluteJumpOpcode;
TReturnTList = function: TList;
// Structure copied from Graphics unit.
PFileFormat = ^TFileFormat;
TFileFormat = record
GraphicClass: TGraphicClass;
Extension: string;
Description: string;
DescResID: Integer;
end;
function FindFirstRelativeCallOpcode(StartOffset: NativeUInt): NativeUInt;
var
Ram: ^Byte;
i: Integer;
PLongJump: PLongAbsoluteJumpOpcode;
begin
Ram := nil;
PLongJump := PLongAbsoluteJumpOpcode(#Ram[StartOffset]);
if (PLongJump^.OpCode[0] = $FF) and (PLongJump^.OpCode[1] = $25) then
{$IF Defined(WIN32)}
Result := FindFirstRelativeCallOpcode(PNativeUInt(PLongJump^.Destination)^)
{$ELSEIF Defined(Win64)}
Result := FindFirstRelativeCallOpcode(PNativeUInt(PLongJump^.Destination + StartOffset + SizeOf(PLongJump^))^)
{$ELSE}
{$MESSAGE Fatal 'Architecture not supported'}
{$ENDIF}
else
begin
for i := 0 to 64 do
if PRelativeCallOpcode(#Ram[StartOffset + i])^.OpCode = $E8 then
Exit(StartOffset + i + PRelativeCallOpcode(#Ram[StartOffset + i])
^.Offset + 5);
Result := 0;
end;
end;
procedure FindGetFileFormatsFunc(out ProcAddr: TReturnTList);
var
Offset_from_RegisterFileFormat: NativeUInt;
Offset_from_RegisterFileFormatRes: NativeUInt;
begin
Offset_from_RegisterFileFormat := FindFirstRelativeCallOpcode(NativeUInt(#TPicture.RegisterFileFormat));
Offset_from_RegisterFileFormatRes := FindFirstRelativeCallOpcode(NativeUInt(#TPicture.RegisterFileFormatRes));
if (Offset_from_RegisterFileFormat = Offset_from_RegisterFileFormatRes) then
ProcAddr := TReturnTList(Pointer(Offset_from_RegisterFileFormat))
else
ProcAddr := nil;
end;
function GetListOfRegisteredPictureFileFormats(List: TStrings): Boolean;
var
GetListProc: TReturnTList;
L: TList;
i: Integer;
begin
FindGetFileFormatsFunc(GetListProc);
if Assigned(GetListProc) then
begin
Result := True;
L := GetListProc;
for i := 0 to L.Count - 1 do
List.Add(PFileFormat(L[i])^.Extension + '=' + PFileFormat(L[i])
^.Description);
end
else
Result := False;
end;
function GetListOfRegisteredPictureTypes(List: TClassList): Boolean;
var
GetListProc: TReturnTList;
L: TList;
i: Integer;
begin
FindGetFileFormatsFunc(GetListProc);
if Assigned(GetListProc) then
begin
Result := True;
L := GetListProc;
for i := 0 to L.Count - 1 do
List.Add(PFileFormat(L[i])^.GraphicClass);
end
else
Result := False;
end;
end.

List all users of an AD group in Delphi

How can I list all users of an AD group in Delphi 7?
One of the options, as I know, is to use a string LDAP. I got a LDAP string, but how to use it?
I tried to use WinAPI, example from internet that i search
function TSequrity.DomainUsers: String;
var
EntiesRead: DWORD;
TotalEntries: DWORD;
UserInfo: lpUSER_INFO_1;
lpBuffer: Pointer;
ResumeHandle: DWORD;
Counter: Integer;
NetApiStatus: LongWord;
w:WideString;
begin
ResumeHandle := 0;
w:=Domain;
NetApiStatus := NetUserEnum(#w[1], 1, 0, lpBuffer, 0, EntiesRead, TotalEntries, ResumeHandle);
NetApiBufferFree(lpBuffer);
NetApiStatus := NetUserEnum(#w[1], 1, 0, lpBuffer, TotalEntries*TotalEntries, EntiesRead, TotalEntries, ResumeHandle);
UserInfo := lpBuffer;
for Counter := 0 to EntiesRead - 1 do
begin
Result:=Result+WideCharToString(UserInfo^.usri1_name)+#13#10;
Inc(UserInfo);
end;
NetApiBufferFree(lpBuffer);
end;
It find local users. But im need to find users of domain group.
Here's an example using "NetGroupGetUsers". Please be aware that this does not work with nested groups (groups containing other groups).
{$WARN SYMBOL_PLATFORM OFF}
program DomainGroupGetUsersTest;
{$APPTYPE CONSOLE}
uses
SysUtils, Windows, Classes;
const
netapi32lib = 'netapi32.dll';
type
PGroupUsersInfo0 = ^TGroupUsersInfo0;
_GROUP_USERS_INFO_0 = record
grui0_name: LPWSTR;
end;
TGroupUsersInfo0 = _GROUP_USERS_INFO_0;
GROUP_USERS_INFO_0 = _GROUP_USERS_INFO_0;
NET_API_STATUS = DWORD;
LPBYTE = ^BYTE;
function NetApiBufferFree (Buffer: Pointer): NET_API_STATUS; stdcall;
external netapi32lib;
function NetGroupGetUsers (servername: LPCWSTR; groupname: LPCWSTR;
level: DWORD; var bufptr: LPBYTE; prefmaxlen: DWORD; var entriesread: DWORD;
var totalentries: DWORD; ResumeHandle: PDWORD): NET_API_STATUS; stdcall;
external netapi32lib;
function DomainGroupGetUsers (const sGroup: WideString;
const UserList: TStrings;
const sLogonServer: WideString) : Boolean;
{ "sLogonServer" must be prefixed with "\\".
"sGroup" must contain the group name only. }
type
TaUserGroup = array of TGroupUsersInfo0;
const
PREF_LEN = 1024;
var
pBuffer : LPBYTE;
i : Integer;
Res : NET_API_STATUS;
dwRead, dwTotal : DWord;
hRes : DWord;
begin
Assert (sGroup <> '');
Assert (sLogonServer <> '');
Assert (UserList <> NIL);
UserList.Clear;
Result := true;
hRes := 0;
repeat
Res := NetGroupGetUsers (PWideChar (sLogonServer), PWideChar (sGroup),
0, pBuffer, PREF_LEN, dwRead, dwTotal,
PDWord (#hRes));
if (Res = Error_Success) or (Res = ERROR_MORE_DATA) then
begin
if (dwRead > 0) then
for i := 0 to dwRead - 1 do
with TaUserGroup (pBuffer) [i] do
UserList.Add (grui0_name);
NetApiBufferFree (pBuffer);
end { if }
else Result := false;
until (Res <> ERROR_MORE_DATA);
end; { DomainGroupGetUsers }
var
UserList : TStringList;
iIndex : Integer;
begin
UserList := TStringList.Create;
try
DomainGroupGetUsers ('Domain Users', UserList,
GetEnvironmentVariable ('LOGONSERVER'));
for iIndex := 0 to UserList.Count - 1 do
WriteLn (UserList [iIndex]);
finally
UserList.Free;
end; { try / finally }
if (DebugHook <> 0) then
begin
WriteLn;
Write ('Press [Enter] to continue ...');
ReadLn;
end; { if }
end.

Delphi HID in Delphi7 and Delphi XE2

I made a small program in Delphi 7 to show some details of all attached HID devices. I only used system files like SetupAPI, Moduleloader and HID.pas. This works perfect.
When I take the same code and compile it in Delphi XE2 or (2010 for that matter), it fails to produce the required output.
Probably this has something to do with pointer casting or so, but I cannot find the root cause.
Can anyone help.
This is my code:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes,
Graphics, Controls, Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
Uses
SetUpAPI, HID;
{$R *.dfm}
Type
THIDUSBDeviceInfo = Record { contains interface level information of each device}
SymLink : String;
BufferSize : Word;
Handle : THandle;
VID : DWord;
PID : DWord;
VersionNumber : Word;
ManufacturerString : String;
ProductString : String;
SerialNumberString : String;
end;
THIDDeviceList = Array of THIDUSBDeviceInfo;
Const
HIDUSB_COUNTOFINTERRUPTBUFFERS = 64; // Count of buffers for interrupt data
Procedure ScanForHIDdevices( Var DeviceList : THIDDeviceList;
TargetVID, TargetPID : DWord);
Var
HID_GUIid : TGUID;
spdid : TSPDeviceInterfaceData;
pSpDidd : PSPDEVICEINTERFACEDETAILDATAA;
spddd : TSPDevInfoData;
HIDinfo : HDEVINFO;
CurIdx : Integer;
dwSize : DWord;
SymbolicLink : String;
DevHandle : THandle;
HidAttrs : THIDDAttributes;
FoundIdx : Integer;
Info : THIDUSBDeviceInfo;
Function GetHidDeviceInfo( Symlink : PChar) : THIDUSBDeviceInfo;
Var
pstr : pWideChar;
preparsedData : PHIDPPreparsedData;
hidCaps : THIDPCaps;
Begin
FillChar(Result, SizeOf( Result), 0);
Result.SymLink := SymLink+ #0;
GetMem( pstr, 512);
DevHandle := CreateFile( Symlink,
GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE,
nil,
OPEN_EXISTING,
0,
0);
If DevHandle <> INVALID_HANDLE_VALUE then
begin
If HidD_GetAttributes( DevHandle,
HidAttrs) then
begin
result.VID := HidAttrs.VendorID;
result.PID := HidAttrs.ProductID;
result.VersionNumber := HidAttrs.VersionNumber;
end;
If HidD_GetManufacturerString( DevHandle, pstr, 512) then
Result.ManufacturerString := pStr;
If HidD_GetProductString( DevHandle, pstr, 512) then
Result.ProductString := pStr;
If HidD_GetSerialNumberString( DevHandle, pstr, 512) then
Result.SerialNumberString := pStr;
{ Set Input buffer size }
HidD_SetNumInputBuffers( DevHandle,
HIDUSB_COUNTOFINTERRUPTBUFFERS);
{ Get capabilities }
HidD_GetPreparsedData( DevHandle, preparsedData);
if (preparsedData) then
begin
HidP_GetCaps( preparsedData, hidCaps);
Result.BufferSize := hidCaps.OutputReportByteLength;
end
else
Result.BufferSize := 11;
closeHandle( DevHandle);
end;
FreeMem( pStr);
End;
Begin
FoundIdx := 0;
DeviceList := Nil;
{ Get GUID of hid class }
HidD_GetHidGuid( HID_GUIid);
{ Get a list of devices belonging to HID class }
HIDinfo := SetupDiGetClassDevs( #HID_GUIid,
nil,
GetDesktopWindow(),
DIGCF_DEVICEINTERFACE or DIGCF_PRESENT);
{ Go through list of devices }
If thandle(HIDinfo) <> INVALID_HANDLE_VALUE then
begin
CurIdx := 0;
spdid.cbSize := SizeOf(spdid);
While SetupDiEnumDeviceInterfaces( HIDinfo,
nil,
HID_GUIid,
curIdx,
spdid) do
begin
dwSize := 0;
{ Get device path for Createfile calls }
SetupDiGetDeviceInterfaceDetail( HIDinfo,
#spdid,
nil,
dwSize,
#dwSize,
nil);
If dwSize > 0 then
begin
GetMem(pSpDidd, dwSize);
pSpDidd^.cbSize := SizeOf( TSPDEVICEINTERFACEDETAILDATAA);
spddd.cbSize := SizeOf(spddd);
If SetupDiGetDeviceInterfaceDetail( HIDinfo,
#spdid,
pSpDidd,
dwSize,
#dwSize,
#spddd) then
begin
SymbolicLink := PChar( #(pSpDidd^.DevicePath));
{ Get information about the device (Vendor and
Product IDs, Strings, ...) }
FillChar(info, SizeOf(Info), 0);
Info := GetHidDeviceInfo( #(pSpDidd^.DevicePath));
Info.Handle := INVALID_HANDLE_VALUE;
{ check if VID/PID match targets }
If (Info.VID = TargetVID) AND
(Info.PID = TargetPID) then
begin
{ Add Devices to result list }
SetLength(DeviceList, FoundIdx + 1);
DeviceList[foundIdx] := Info;
Inc(FoundIdx);
end
else // list all HID devices if no target is specified
If (TargetVID = 0) AND (TargetPID = 0) then
begin
{ Add Devices to result list }
SetLength( DeviceList, FoundIdx + 1);
DeviceList[FoundIdx] := Info;
Inc(FoundIdx);
end;
end;
FreeMem( pSpDidd);
end;
inc(CurIdx);
end;
SetupDiDestroyDeviceInfoList( HIDinfo);
end;
End;
procedure TForm1.Button1Click(Sender: TObject);
Var
DeviceList : THIDDeviceList;
I : Integer;
begin
ScanForHIDdevices( DeviceList, 0, 0);
Memo1.Lines.Clear;
Memo1.Lines.Add(IntToStr(Length(DeviceList)) + ' device(s) found');
If Length(DeviceList) > 0 then
For I := 0 to Length(DeviceList)-1 do
With DeviceList[I] do
begin
Memo1.Lines.Add('Device Number : ' + IntToStr(I));
Memo1.Lines.Add('Symbolic link : ' + SymLink);
Memo1.Lines.Add('Handle : 0x' + IntToHex(Handle, 1));
Memo1.Lines.Add('Buffer size : ' + IntToStr(BufferSize));
Memo1.Lines.Add('VID : 0x' + IntToHex(VID, 4));
Memo1.Lines.Add('PID : 0x' + IntToHex(PID, 4));
Memo1.Lines.Add('Version : ' + IntToStr(VersionNumber));
Memo1.Lines.Add('Manufacturer : ' + ManufacturerString);
Memo1.Lines.Add('Product name : ' + ProductString);
Memo1.Lines.Add('Serial number : ' + SerialNumberString);
Memo1.Lines.Add(' ');
end;
Memo1.SetFocus;
end;
You have to load the DLL module before you call any of these functions:
LoadHid();
LoadSetupApi();
I suppose that we have to unload the DLL when finished with the program. Then use this code:
UnloadHid;
UnloadSetupApi

Resources