Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2002-12

How to copy a lot of data?

16 messages · 8 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Judison

Hi all,

I have a C function like this:

void GetData(long x; unsigned char **data; unsigned long *rsize);
procedure GetData(x: longint; data: PPbyte; rsize: Pdword);

that returns the data I want (based on x), and rsize that indicate the size of data (in 4 bytes units) and, after I need to free the data,

void FreeData(unsigned char *data);
procedure FreeData(data: PByte);

I need to make a Pascal procedure, to get this Data, and must be like this:

procedure MyGetData(x: integer; var data; var rsize: integer);
var
  lData: Pointer;
begin
  GetData(x, @ldata, @rsize);
  {copy lData^ to Data, (rsize div 4) bytes}
  FreeData(lData);
end;

What is the correct way?

A)
  Data := lData^; // this will copy rsize * 4 bytes?
B)
  for I := 0 to NItems -1 do
    PInteger(@Data)[I] := PInteger(lData)[I];

C) none of above ;)

If you ask "why?" it's because I need to call:

GetMyData(1, MyRecord, rsize);
GetMyData(2, MyArray, rsize);
GetMyData(3, MyInteger, rsize);

TIA
Your friend,
Judison

Reply by email

Jonas Maebe
to Judison
Judison wrote 6 lines
procedure MyGetData(x: integer; var data; var rsize: integer); var lData: Pointer; begin GetData(x, @ldata, @rsize); {copy lData^ to Data, (rsize div 4) bytes}
     move(ldata^,data,rsize div 4);
FreeData(lData); end;

Jonas

Reply by email

Unknown author
Show quoted text 5 lines
If you ask "why?" it's because I need to call: GetMyData(1, MyRecord, rsize); GetMyData(2, MyArray, rsize); GetMyData(3, MyInteger, rsize);

Judison,

which platform? In win32 API I would suggest:

VOID CopyMemory (PVOID Destination, CONST VOID * Source,DWORD Length);

which is (or should be) defined in 'windosw.pas' and has a Pascal form:

procedure CopyMemory(Destination: Pointer; Source: Pointer; Length: DWORD);

You could also try:

procedure Move( const Source; var Dest; count : Integer );

which is part of Delphi's RTL (it's in system.pas) so I would hope that FPC implements it and that most platforms have it.

Matt

---------------------------------------------
This message was sent using Mistral WebMail.
http://www.mistral.co.uk/

Reply by email

Jonas Maebe
to Unknown author
Show quoted text 7 lines
You could also try: procedure Move( const Source; var Dest; count : Integer ); which is part of Delphi's RTL (it's in system.pas) so I would hope that FPC implements it and that most platforms have it.

It's indeed part of the system unit, so it's available on all platforms.

Jonas

Reply by email

MPDJ
to Jonas Maebe: On woensdag, dec 4, 2002, at 11:19 Europe/Brussels, It's indeed part of the system unit,…

Hi,

I'm trying to shut down windows. I know it should be simple, but for some reason, I can't get my application to have shutdown privileges. The procedure I'm using is this...

Procedure Shut_Down_Windows;
Var
   hToken    : Handle;
   tkp       : TOKEN_PRIVILEGES;
   h         : PWORD;
begin
   If OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, hToken) then Begin
      LookupPrivilegeValue(nil, SE_SHUTDOWN_NAME, tkp.Privileges[0].Luid);
      tkp.PrivilegeCount:=1;
      tkp.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;
      h:=0;
      AdjustTokenPrivileges(hToken, False, @tkp, SizeOf(tpk), nil ,@h);
      ExitWindowsEx (EWX_SHUTDOWN, 0);
   End;
end.

OpenProcessToken returns TRUE, so does LookUpPriviliegeValue and AdjustTokenPrivileges.

Problem is, ExitWindowsEX returns FALSE with the error that I don't have the ShutDown privilege. I can't for the life of me spot the problem. I'm sure it's going to be something stupid I've missed, but I'm blind to it. It's failed on both my Win2000 and XP machines.

If anyone has a spare few seconds to see if they can spot my mistake, I'd be very grateful.

Andy

Reply by email

A.J. Venter
to Unknown author: which platform? In win32 API I would suggest: VOID CopyMemory (PVOID Destination, CONST…

Hi,

I am extremely embarassed to be asking this, but I haven't worked with typed files in so long that frankly I cannot remember.

Say I have this code:

Type someRec = record
		a :String;
		I : Integer;
	end;
Var FoS : File of SomeRec;
Now lets say FoS contains three entries and I wish to remove the first one.
A simple shift up loop will technically remove it,
entry1 := entry2;
entry2 := entry3;

But how do I then get rid of the duplicate entry two ?

In other words, next time a record is added how do I ensure that it is added at the third position in the file and not the fourth ?

Ciao
A.J.

Reply by email

Jonas Maebe
to A.J. Venter
A.J. Venter wrote 7 lines
Now lets say FoS contains three entries and I wish to remove the first one. A simple shift up loop will technically remove it, entry1 := entry2; entry2 := entry3; But how do I then get rid of the duplicate entry two ?

Use truncate() to cut off the file.

Jonas

Reply by email

Unknown author

The following works in Delphi. Compile it as a Win32 console app. It's a brute force shutdown, no apps get a chance to complain, and the data under NT/2k etc is not backed up. It exits just about as fast as possible, and closes down windows in about 30 seconds on my machine. Don't be surprised if shortcuts, run menu last command ect and settings are lost between boots, they are not backed up because of the reason I mentioned above.

Matt

program shutdown;
uses
  SysUtils, Windows;
function SetPrivilege(
  sPrivilegeName : string;
  bEnabled : boolean )
    : boolean;
var
  TPPrev,
  TP         : TTokenPrivileges;
  Token      : THandle;
  dwRetLen   : DWord;
begin
  Result := False;
  OpenProcessToken(
    GetCurrentProcess,
    TOKEN_ADJUST_PRIVILEGES
    or TOKEN_QUERY,
    Token );
  TP.PrivilegeCount := 1;
  if( LookupPrivilegeValue(
        Nil,
        PChar( sPrivilegeName ),
        TP.Privileges[ 0 ].LUID ) )then
  begin
    if( bEnabled )then
    begin
      TP.Privileges[ 0 ].Attributes  :=
        SE_PRIVILEGE_ENABLED;
    end else
    begin
      TP.Privileges[ 0 ].Attributes  :=
        0;
    end;
    dwRetLen := 0;
    Result := AdjustTokenPrivileges(
                Token,
                False,
                TP,
                SizeOf( TPPrev ),
                TPPrev,
                dwRetLen );
  end;
  CloseHandle( Token );
end;
//
// iFlags:
//
//  one of the following must be
//  specified
//
//   EWX_LOGOFF
//   EWX_REBOOT
//   EWX_SHUTDOWN
//
//  following attributes may be
//  combined with above flags
//
//   EWX_POWEROFF
//   EWX_FORCE    : terminate processes
//
function WinExit( iFlags : integer ) : boolean;
begin
  Result := True;
  if( SetPrivilege( 'SeShutdownPrivilege', True ) )then
  begin
    if( not ExitWindowsEx( iFlags, 0 ) )then
    begin
      // handle errors...
      Result := False;
    end;
    SetPrivilege( 'SeShutdownPrivilege', False )
  end else
  begin
    // handle errors...
    Result := False;
  end;
end;
procedure doexit;
begin
  if not WinExit(EWX_FORCE + EWX_POWEROFF + EWX_SHUTDOWN) then begin
    writeln('windows refused to cooperate.');
    readln;
  end;
end;
begin
  doexit;
end.

---------------------------------------------
This message was sent using Mistral WebMail.
http://www.mistral.co.uk/

Reply by email

A.J. Venter
to Jonas Maebe: Use truncate() to cut off the file. Jonas
Jonas Maebe wrote 13 lines
On vrijdag, dec 6, 2002, at 07:57 Europe/Brussels, A.J. Venter wrote: > Now lets say FoS contains three entries and I wish to remove the first > one. > A simple shift up loop will technically remove it, > entry1 := entry2; > entry2 := entry3; > > But how do I then get rid of the duplicate entry two ? Use truncate() to cut off the file. Jonas

Thanks, I knew it was a simple command.

Ciao
A.J.

Programming n. First defined by Shakespear as: "Bloody instructions which, being taught return to plague their inventor" (Macbeth, Act 1, Scene 7)

Reply by email

MPDJ
to Unknown author: The following works in Delphi. Compile it as a Win32 console app. It's a brute force…

Thanks for the Shutdown routine. I'm starting to get really annoying with this now as I compiled the routine and ran it and exactly the same problem appeared with the same error message stating I don't have the correct privilege.

I added a CheckPrivilege call before and after the AdjustTokenPrivileges and it appears the "SeShutdownPrivilege" has not been altered.

Is there something strange about the Privilege settings with Windows that I'm missing somehow? I've searched Google and all the routines are very similar to each other yet despite tweaking the function the error remains. I'll have to try compiling the function in Delphi just to prove to myself I'm not going mad!

Friday, December 6, 2002, 11:12:32 AM, you wrote:

micu> The following works in Delphi. Compile it as a Win32 console app. It's a brute micu> force shutdown, no apps get a chance to complain, and the data under NT/2k etc micu> is not backed up. It exits just about as fast as possible, and closes down micu> windows in about 30 seconds on my machine. Don't be surprised if shortcuts, run micu> menu last command ect and settings are lost between boots, they are not backed micu> up because of the reason I mentioned above.

micu> Matt

micu> program shutdown;
micu> uses
micu>   SysUtils, Windows;
micu> function SetPrivilege(
micu>   sPrivilegeName : string;
micu>   bEnabled : boolean )
micu>     : boolean;
micu> var
micu>   TPPrev,
micu>   TP         : TTokenPrivileges;
micu>   Token      : THandle;
micu>   dwRetLen   : DWord;
micu> begin
micu>   Result := False;

micu> OpenProcessToken(
micu> GetCurrentProcess,
micu> TOKEN_ADJUST_PRIVILEGES
micu> or TOKEN_QUERY,
micu> Token );

micu> TP.PrivilegeCount := 1;
micu> if( LookupPrivilegeValue(
micu> Nil,
micu> PChar( sPrivilegeName ),
micu> TP.Privileges[ 0 ].LUID ) )then
micu> begin
micu> if( bEnabled )then
micu> begin
micu> TP.Privileges[ 0 ].Attributes :=
micu> SE_PRIVILEGE_ENABLED;
micu> end else
micu> begin
micu> TP.Privileges[ 0 ].Attributes :=
micu> 0;
micu> end;

micu> dwRetLen := 0;
micu> Result := AdjustTokenPrivileges(
micu> Token,
micu> False,
micu> TP,
micu> SizeOf( TPPrev ),
micu> TPPrev,
micu> dwRetLen );
micu> end;

micu>   CloseHandle( Token );
micu> end;

micu> //
micu> // iFlags:
micu> //
micu> // one of the following must be
micu> // specified
micu> //
micu> // EWX_LOGOFF
micu> // EWX_REBOOT
micu> // EWX_SHUTDOWN
micu> //
micu> // following attributes may be
micu> // combined with above flags
micu> //
micu> // EWX_POWEROFF
micu> // EWX_FORCE : terminate processes
micu> //
micu> function WinExit( iFlags : integer ) : boolean; micu> begin
micu> Result := True;
micu> if( SetPrivilege( 'SeShutdownPrivilege', True ) )then micu> begin
micu> if( not ExitWindowsEx( iFlags, 0 ) )then micu> begin
micu> // handle errors...
micu> Result := False;
micu> end;
micu> SetPrivilege( 'SeShutdownPrivilege', False ) micu> end else
micu> begin
micu> // handle errors...
micu> Result := False;
micu> end;
micu> end;

micu> procedure doexit;
micu> begin
micu>   if not WinExit(EWX_FORCE + EWX_POWEROFF + EWX_SHUTDOWN) then begin
micu>     writeln('windows refused to cooperate.');
micu>     readln;
micu>   end;
micu> end;

micu> begin
micu> doexit;
micu> end.

micu> --------------------------------------------- micu> This message was sent using Mistral WebMail. micu> http://www.mistral.co.uk/

micu> _______________________________________________ micu> fpc-pascal maillist -
micu> http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Florian Klaempfl
to MPDJ
MPDJ wrote 15 lines
Thanks for the Shutdown routine. I'm starting to get really annoying with this now as I compiled the routine and ran it and exactly the same problem appeared with the same error message stating I don't have the correct privilege. I added a CheckPrivilege call before and after the AdjustTokenPrivileges and it appears the "SeShutdownPrivilege" has not been altered. Is there something strange about the Privilege settings with Windows that I'm missing somehow? I've searched Google and all the routines are very similar to each other yet despite tweaking the function the error remains. I'll have to try compiling the function in Delphi just to prove to myself I'm not going mad!

I'am not really familiar with the Win32 privilege system. But is the user under which you ran the program allowed to shutdown the system? Did you try to run the program with administrator privileges?

Reply by email

Matt Emson
to MPDJ: Thanks for the Shutdown routine. I'm starting to get really annoying with this now as I…

Which version of Windows are you using? I assume a NT 4.X/NT 5.X variant. If it's NT 4, which service pack? if it's less than 4 I'd try SP5 or SP6 first. Just to be sure.

If you're using a Win9x variant, you probably don't need all the security stuff because it's only NT that needs all of that.

The code was taken from Delphi, so make sure you've compiled using the Delphi compatibility mode, just to be sure that FPC isn't ignoring something.

Hope that helps,

Matt

Show quoted text 14 lines
Thanks for the Shutdown routine. I'm starting to get really annoying with this now as I compiled the routine and ran it and exactly the same problem appeared with the same error message stating I don't have the correct privilege. I added a CheckPrivilege call before and after the AdjustTokenPrivileges and it appears the "SeShutdownPrivilege" has not been altered. Is there something strange about the Privilege settings with Windows that I'm missing somehow? I've searched Google and all the routines are very similar to each other yet despite tweaking the function the error remains. I'll have to try compiling the function in Delphi just to prove to myself I'm not going mad!

Reply by email

MPDJ
to Matt Emson

I'm using Windows 2000 SP3 and I compiled under both Delphi and fpc. The function is due to be used under Windows 95/98 and Win2000 so I'd like to get the privileges sorted so I can shut either OS down.

I've tried under administrator but this has made no difference. I was under the impression that application privileges are independent of the user logged in at the time?

I'll dig out an old copy of Delphi and compile it just to be sure there has been no change in the API functions, though I did double-check online with MSDN and all looked OK.

Again, many thanks for all the suggestions and help so far.

Andy

Sunday, December 8, 2002, 10:36:24 AM, you wrote:

ME> Which version of Windows are you using? I assume a NT 4.X/NT 5.X variant. If ME> it's NT 4, which service pack? if it's less than 4 I'd try SP5 or SP6 first. ME> Just to be sure.

ME> If you're using a Win9x variant, you probably don't need all the security ME> stuff because it's only NT that needs all of that.

ME> The code was taken from Delphi, so make sure you've compiled using the ME> Delphi compatibility mode, just to be sure that FPC isn't ignoring ME> something.

ME> Hope that helps,

ME> Matt

Show quoted text 14 lines
> Thanks for the Shutdown routine. I'm starting to get really annoying > with this now as I compiled the routine and ran it and exactly the > same problem appeared with the same error message stating I don't have > the correct privilege. > > I added a CheckPrivilege call before and after the > AdjustTokenPrivileges and it appears the "SeShutdownPrivilege" has not > been altered. > > Is there something strange about the Privilege settings with Windows > that I'm missing somehow? I've searched Google and all the routines > are very similar to each other yet despite tweaking the function the > error remains. I'll have to try compiling the function in Delphi just > to prove to myself I'm not going mad!

ME> _______________________________________________ ME> fpc-pascal maillist -
ME> http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Unknown author
I'm using Windows 2000 SP3 and I compiled under both Delphi and fpc. The function is due to be used under Windows 95/98 and Win2000 so I'd like to get the privileges sorted so I can shut either OS down.

The code I sent was compiled under Delphi 5 with all updates (the service pack plus the ADO and stuff like that, not that I expect the ADO to make much difference.)

The code has been run on both NT4 with SP6a (IIRC) or SP5, and a hokey Win2k which probably has no SP's at all, or at most SP1 (the site I'm on only just uses Win2k!! Some users are stull on NT4.)

I have local admin rights on my machine... I've never tried it on a machine that has no admin rights, but as you mention, I thought that wouldn't make a difference.

I've tried under administrator but this has made no difference. I was under the impression that application privileges are independent of the user logged in at the time?

Ditto (as stated above.)

I'll dig out an old copy of Delphi and compile it just to be sure there has been no change in the API functions, though I did double-check online with MSDN and all looked OK.

Delphi 5 would be a good one to try. I can send you the project if you would find that easier, but it should build as is. There were no extra files other than the body of source I posted.

Again, many thanks for all the suggestions and help so far.

NP. If it does work in Delphi, it could be a quirk of FPC. Or even, shock horror, a bug ;-P Then again, it could just be user permissions. I've no easy way of testing. I could send you my exe if that helps.. that way you'd know if my version fails without having to dig out Delphi.

Matt

---------------------------------------------
This message was sent using Mistral WebMail.
http://www.mistral.co.uk/

Reply by email

MPDJ
to Unknown author

micu> NP. If it does work in Delphi, it could be a quirk of FPC. Or even, shock micu> horror, a bug ;-P Then again, it could just be user permissions. I've no easy micu> way of testing. I could send you my exe if that helps.. that way you'd know if micu> my version fails without having to dig out Delphi.

First problem I've had with FPC and Windows API. I'm sure it's me and I'll kick myself when I finally spot it ;-)

If you could send me the EXE file, I'd be very grateful. I had to uninstall Delphi as I was getting tight for spare drive space and never got around to reinstalling it as I'm using FPC now. If it proves too much of a problem, don't worry, I'll compile it here and let you know how it goes.

Andy

you wrote
> I'm using Windows 2000 SP3 and I compiled under both Delphi and fpc. > The function is due to be used under Windows 95/98 and Win2000 so I'd > like to get the privileges sorted so I can shut either OS down.

micu> The code I sent was compiled under Delphi 5 with all updates (the service pack micu> plus the ADO and stuff like that, not that I expect the ADO to make much micu> difference.)

micu> The code has been run on both NT4 with SP6a (IIRC) or SP5, and a hokey Win2k micu> which probably has no SP's at all, or at most SP1 (the site I'm on only just micu> uses Win2k!! Some users are stull on NT4.)

micu> I have local admin rights on my machine... I've never tried it on a machine micu> that has no admin rights, but as you mention, I thought that wouldn't make a micu> difference.

> I've tried under administrator but this has made no difference. I was > under the impression that application privileges are independent of > the user logged in at the time?

micu> Ditto (as stated above.)

> I'll dig out an old copy of Delphi and compile it just to be sure > there has been no change in the API functions, though I did > double-check online with MSDN and all looked OK.

micu> Delphi 5 would be a good one to try. I can send you the project if you would micu> find that easier, but it should build as is. There were no extra files other micu> than the body of source I posted.

> Again, many thanks for all the suggestions and help so far.

micu> NP. If it does work in Delphi, it could be a quirk of FPC. Or even, shock micu> horror, a bug ;-P Then again, it could just be user permissions. I've no easy micu> way of testing. I could send you my exe if that helps.. that way you'd know if micu> my version fails without having to dig out Delphi.

micu> Matt

micu> --------------------------------------------- micu> This message was sent using Mistral WebMail. micu> http://www.mistral.co.uk/

micu> _______________________________________________ micu> fpc-pascal maillist -
micu> http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email