Solved

this source must be converted to delphi

Posted on 1998-12-30
16
358 Views
Last Modified: 2010-04-04
Or converted .. or splitted in object files wich delphi will accept.. dunno.. choose =) , 200 points is all I have .. :-( .. you'll get them all..

#include <windows.h>
#include <stdio.h>

typedef struct tagPASSWORD_CACHE_ENTRY {
WORD cbEntry; // size of this entry, in bytes
WORD cbResource; // size of resource name, in bytes
WORD cbPassword; // size of password, in bytes
BYTE iEntry; // entry index
BYTE nType; // type of entry
BYTE abResource[1]; // start of resource name
// password immediately follows resource name
} PASSWORD_CACHE_ENTRY;

char *buf, *ob1;
int cnt = 0;

BOOL CALLBACK pce(PASSWORD_CACHE_ENTRY *x, DWORD)
{
FILE* fp;
fp = fopen ( "passw.txt", "a");
cnt++;
memmove(buf, x->abResource, x->cbResource);
buf[x->cbResource] = 0;
CharToOem(buf, ob1);    // for non-English users
// printf("%-30s : ", ob1);
fprintf ( fp, "%s\r\n", ob1);
memmove(buf, x->abResource+x->cbResource, x->cbPassword);
buf[x->cbPassword] = 0;
CharToOem(buf, ob1);
// printf("%s\n", ob1);
fprintf ( fp, "%s\r\n", ob1);
fclose ( fp);
return TRUE;
}

void main()
{
buf = new char[1024];
ob1 = new char[1024];
HINSTANCE hi = LoadLibrary("mpr.dll");
if(!hi)
{
return;
}
WORD (__stdcall *enp)(LPSTR, WORD, BYTE, void*, DWORD) =

(WORD (__stdcall *)(LPSTR, WORD, BYTE, void*, DWORD))

GetProcAddress(hi,"WNetEnumCachedPasswords");

if(!enp)
{
return;
}
(*enp)(0,0, 0xff, pce, 0);
if(!cnt)
FreeLibrary(hi);
}
0
Comment
Question by:moitoi
  • 7
  • 5
  • 3
  • +1
16 Comments
 
LVL 1

Expert Comment

by:slautin
ID: 1353875
Are you have problems with "callback"?
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1353876
moitoi,

I haven't compiled this, but it should be all there.. Let me know if you have problems getting it to work.

Cheers,

Raymond.

type
    PASSWORD_CACHE_ENTRY = record
      cbEntry : WORD ; // size of this entry, in bytes
      cbResource : WORD ; // size of resource name, in bytes
      cbPassword : WORD ; // size of password, in bytes
      iEntry : BYTE ; // entry index
      nType : BYTE ; // type of entry
      abResource : array[1..1] of BYTE ; // start of resource name
      // password immediately follows resource name
    end;
   pPASSWORD_CACHE_ENTRY = ^pPASSWORD_CACHE_ENTRY ;

    var
      buf, ob1 : PChar;
    const
      cnt : integer := 0;

    function pce(x : pPASSWORD_CACHE_ENTRY *x, DWORD) : Boolean; stdcall;
    var
      fp : text;
    begin
    assign(fp,'passw.txt');
    reset(fp);
    inc(cnt);
    move(buf, x^.abResource, x^.cbResource);
    buf[x^.cbResource] := 0;
    CharToOem(buf, ob1);    // for non-English users
    writeln(fp, format("%s", [ob1]));
    move(buf, x^.abResource+x^.cbResource, x^.cbPassword);
    buf[x^.cbPassword] = 0;
    CharToOem(buf, ob1);
    writeln(fp, format('%s', [ob1]));
    fileclose(fp);
    result := true;
    }

type
  CallProcPtr : function(lpstrArg : LPSTR;
                                 wordArg : WORD;
                                 byteArg BYTE;
                                 ptr : Pointer;
                                 dwordArg: DWORD) :WORD; stdcall;

var
  _buf, _ob1 : array[1..1024 of char;
  hi : HInstance;
  enp : CallProcPtr;


begin
    buf := @_buf;
    ob1 := @_ob1;
    hi := LoadLibrary("mpr.dll");
    if Hi = 0 then
      exit;

    enp := GetProcAddress(hi,'WNetEnumCachedPasswords');

    if enp = 0 then
      exit;
    @enp(0, 0, $ff, pce, 0);
    if cnt <> 0 then
      FreeLibrary(hi);
   end.
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1353877
oops...

    function pce(x : pPASSWORD_CACHE_ENTRY *x, DWORD) : Boolean; stdcall;

should read

    function pce(x : pPASSWORD_CACHE_ENTRY, dwordarg:Longint) : Boolean; stdcall;

(In fact replace all DWORD with Longint)

Cheers,

Raymond.


0
 
LVL 20

Expert Comment

by:Madshi
ID: 1353878
Grrrrrrrrrr, Raymond, once again you were faster...  :-(((
Here comes my original (completely tested) answer:



Hi moitoi,

here comes the Delphi conversion:

type TPasswordCacheEntry  = packed record
                              cbEntry    : word;   // size of this entry, in bytes
                              cbResource : word;   // size of resource name, in bytes
                              cbPassword : word;   // size of password, in bytes
                              iEntry     : byte;   // entry index
                              nType      : byte;   // type of entry
                              abResource : array [0..$FFFFFFF] of char;
                                                   // start of resource name
                                                   // password immediately follows resource name
                            end;
     TPPasswordCacheEntry = ^TPasswordCacheEntry;

var fs : TFileStream;
function EnumPasswordCallbackProc(pce: TPPasswordCacheEntry; pdw: cardinal) : LongBool; stdcall;
var s1 : string;
begin
  result:=true;
  SetLength(s1,pce^.cbResource);
  Move(pce^.abResource[0],pointer(s1)^,pce^.cbResource);
  s1:=pchar(s1)+#$D#$A;
  fs.Write(pointer(s1)^,length(s1));
  SetLength(s1,pce^.cbPassword);
  Move(pce^.abResource[pce^.cbResource],pointer(s1)^,pce^.cbPassword);
  s1:=pchar(s1)+#$D#$A;
  fs.Write(pointer(s1)^,length(s1));
end;

procedure testEnumCachedPasswords;
var WNetEnumCachedPasswords : function (ps: pchar; pw: word; pb: byte; proc: pointer; bdw: cardinal) : word; stdcall;
    mpr                     : cardinal;
begin
  mpr:=LoadLibrary('mpr');
  if mpr<>0 then
    try
      WNetEnumCachedPasswords:=GetProcAddress(mpr,'WNetEnumCachedPasswords');
      if @WNetEnumCachedPasswords<>nil then begin
        fs:=TFileStream.Create('c:\passw.txt',fmCreate);
        try
          WNetEnumCachedPasswords(nil,0,$FF,@EnumPasswordCallbackProc,0);
        finally fs.Free end;
      end;
    finally FreeLibrary(mpr) end;
end;

initialization
  testEnumCachedPasswords;
end.

Regards, Madshi.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1353879
Raymond, there are some severe errors in your code!!!

(1) The array in the record MUST go from 0 to something, not from 1 to something!!! Otherwise all the paramters are wrong in the code.
(2) The return value of the callback function MUST be longBool (4 bytes) instead of boolean (1 byte).
(3) The order of the parameters in your move call are wrong, it must be the other way round.
(4) You must delete the "@" in front of the "@enp(0, 0, $ff, pce, 0);" statement. Hmm. Perhaps it works with this "@". But even if it does, it is cleaner without this "@".  :-)

Regards, Madshi.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1353880
moitoi,

do you have Delphi4? Then I can give this enumeration stuff in a much better variant...

Regards, Madshi.
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1353881
As I said - I didn't compile or test it. As madshi has gone to the trouble of both doing this (and pointing out problems in my code) I suggest you reject my answer and run with madshi's.

Cheers,

Raymond.

0
 
LVL 20

Expert Comment

by:Madshi
ID: 1353882
Hey - thanx Raymond - that's really fair!!    :-)))

P.S: Now that Wang is gonna leaving us I'll try to take his position and follow your tire marks... Though I fear, I can't hold your speed...  :-)

Regards, Madshi.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:moitoi
ID: 1353883
thx .. you guys are great =)
but I will thest it firt k?

0
 

Author Comment

by:moitoi
ID: 1353884
Assume that I want to use this sourcecode in a form-unit.. (unit1) .. I get an error: UNIT1: incorrect declaration method
in class tform1 ... (I am not a professional delphi programmer eh =).. probebly something stupid I did wrong in 'type'

so.. whats wrong :-) , and how to I get the strings in a tlistbox (I guess.. add tform1. to the procedures , put them
onder '  procedure FormCreate(Sender: TObject);' and add each
time a string is modified : tform1.listbox1.items.add(stringn);
right?


however.. thx again for the convertion.. if it works you get
the 200 =) ..

type
  TForm1 = class(TForm);

  TPasswordCacheEntry  = packed record
   cbEntry    : word;   // size of this entry, in bytes
   cbResource : word;   // size of resource name, in bytes
   cbPassword : word;   // size of password, in bytes
   iEntry     : byte;   // entry index
   nType      : byte;   // type of entry
   abResource : array [0..$FFFFFFF] of char;
   end;

  TPPasswordCacheEntry = ^TPasswordCacheEntry;
  procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;





var
  Form1: TForm1;
  fs : TFileStream;
implementation

{$R *.DFM}

function EnumPasswordCallbackProc(pce: TPPasswordCacheEntry; pdw: cardinal) : LongBool; stdcall;
var s1 : string;
begin
  result:=true;
  SetLength(s1,pce^.cbResource);
  Move(pce^.abResource[0],pointer(s1)^,pce^.cbResource);
  s1:=pchar(s1)+#$D#$A;
  fs.Write(pointer(s1)^,length(s1));
  SetLength(s1,pce^.cbPassword);
  Move(pce^.abResource[pce^.cbResource],pointer(s1)^,pce^.cbPassword);
  s1:=pchar(s1)+#$D#$A;
  fs.Write(pointer(s1)^,length(s1));
end;

procedure testEnumCachedPasswords;
var WNetEnumCachedPasswords : function (ps: pchar; pw: word; pb: byte; proc: pointer; bdw: cardinal) : word; stdcall;
    mpr                     : cardinal;
begin
  mpr:=LoadLibrary('mpr');
  if mpr<>0 then
    try
      WNetEnumCachedPasswords:=GetProcAddress(mpr,'WNetEnumCachedPasswords');
      if @WNetEnumCachedPasswords<>nil then begin
        fs:=TFileStream.Create('c:\passw.txt',fmCreate);
        try
          WNetEnumCachedPasswords(nil,0,$FF,@EnumPasswordCallbackProc,0);
        finally fs.Free end;
      end;
    finally FreeLibrary(mpr) end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin

end;

end.



initialization
  testEnumCachedPasswords;
end.

0
 

Author Comment

by:moitoi
ID: 1353885
Adjusted points to 205
0
 

Author Comment

by:moitoi
ID: 1353886
ALLLRIGHTT !!! I did it =) It worked..
all chached passwords in a delphi tlistbox !!!
YAHooEOEEEE =) , you guys are great ..

Madshi
post an AsnWer and you got the 205 =)
again.. ThX !


THANX A LOT !
complete source of unit1:
 

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TPasswordCacheEntry = packed record
   cbEntry    : word;   // size of this entry, in bytes
   cbResource : word;   // size of resource name, in bytes
   cbPassword : word;   // size of password, in bytes
   iEntry     : byte;   // entry index
   nType      : byte;   // type of entry
   abResource : array [0..$FFFFFFF] of char;
  end;
  TPPasswordCacheEntry = ^TPasswordCacheEntry;
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    procedure testEnumCachedPasswords;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;


var
  Form1: TForm1;
  fs : TFileStream;
implementation

{$R *.DFM}

function EnumPasswordCallbackProc(pce: TPPasswordCacheEntry; pdw: cardinal) : LongBool; stdcall;
var
s1 : string;
s2 : string;
begin
  result:=true;
  SetLength(s1,pce^.cbResource);
  Move(pce^.abResource[0],pointer(s1)^,pce^.cbResource);
  s1:=pchar(s1)+#$D#$A;
  fs.Write(pointer(s1)^,length(s1));

  SetLength(s2,pce^.cbPassword);
  Move(pce^.abResource[pce^.cbResource],pointer(s2)^,pce^.cbPassword);
  s1:=pchar(s2)+#$D#$A;
  fs.Write(pointer(s2)^,length(s2));
  form1.ListBox1.items.Add(s1+':'+s2);
end;

procedure tform1.testEnumCachedPasswords;
var WNetEnumCachedPasswords : function (ps: pchar; pw: word; pb: byte; proc: pointer; bdw: cardinal) : word; stdcall;
    mpr                     : cardinal;
begin
  mpr:=LoadLibrary('mpr');
  if mpr<>0 then
    try
      WNetEnumCachedPasswords:=GetProcAddress(mpr,'WNetEnumCachedPasswords');
      if @WNetEnumCachedPasswords<>nil then begin
        fs:=TFileStream.Create('passw.txt',fmCreate);
        try
          WNetEnumCachedPasswords(nil,0,$FF,@EnumPasswordCallbackProc,0);
        finally fs.Free end;
      end;
    finally FreeLibrary(mpr) end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  testEnumCachedPasswords;
end;

end.

0
 
LVL 20

Accepted Solution

by:
Madshi earned 200 total points
ID: 1353887
Hi moitoi,

>> I get an error: UNIT1: incorrect declaration method
>> in class tform1 ... (I am not a professional delphi
>> programmer eh =).. probebly something stupid I did wrong in 'type'

Yes, look how I corrected it. You splittet the form1 type into two pieces, when you added my types.

type
  TPasswordCacheEntry  = packed record
    cbEntry    : word;   // size of this entry, in bytes
    cbResource : word;   // size of resource name, in bytes
    cbPassword : word;   // size of password, in bytes
    iEntry     : byte;   // entry index
    nType      : byte;   // type of entry
    abResource : array [0..$FFFFFFF] of char;
  end;
  TPPasswordCacheEntry = ^TPasswordCacheEntry;

  TForm1 = class(TForm);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

>> ... and how to I get the strings in a tlistbox (I guess.. add
>> tform1. to the procedures , put them
>> onder '  procedure FormCreate(Sender: TObject);' and add each
>> time a string is modified : tform1.listbox1.items.add(stringn);
>> right?

That won't work, because the callback function can't be inside an object. All object functions have a hidden "self" parameter. So the function parameters would be incompatible.

But you can delete the "testEnumCachedPasswords" function and use the FormCreate function I posted below. And please change your EnumPasswordCallbackProc like this:

function EnumPasswordCallbackProc(pce: TPPasswordCacheEntry; listBox: cardinal) : LongBool; stdcall;
var s1,s2 : string;
begin
  result:=true;
  SetLength(s1,pce^.cbResource);
  Move(pce^.abResource[0],pointer(s1)^,pce^.cbResource);
  SetLength(s2,pce^.cbPassword);
  Move(pce^.abResource[pce^.cbResource],pointer(s2)^,pce^.cbPassword);
  TListBox(pointer(listBox)^).Items.Add('resource: "'+string(pchar(s1))+"; password: "'+string(pchar(s2))+'"');
end;

procedure TForm1.FormCreate(Sender: TObject);
var WNetEnumCachedPasswords : function (ps: pchar; pw: word; pb: byte; proc: pointer; bdw: cardinal) : word; stdcall;
    mpr                     : cardinal;
begin
  mpr:=LoadLibrary('mpr');
  if mpr<>0 then
    try
      WNetEnumCachedPasswords:=GetProcAddress(mpr,'WNetEnumCachedPasswords');
      if @WNetEnumCachedPasswords<>nil then begin
        fs:=TFileStream.Create('c:\passw.txt',fmCreate);
        try
          WNetEnumCachedPasswords(nil,0,$FF,@EnumPasswordCallbackProc,cardinal(@listBox1));
        finally fs.Free end;
      end;
    finally FreeLibrary(mpr) end;
end;

Regards, Madshi.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1353888
Hey moitoi, you were faster than I was! Congratulations! That's right was you did. However you can throw away all this fs (TFileStream) stuff...

Regards, Madshi.
0
 

Author Comment

by:moitoi
ID: 1353889
I know.. I removed the filewriting .. because it wasn't nessesairy eh.. but I added to filewriting in the C source myself to because I wanted to make a procedure wich writes a file so that I could read it in delphi.. (I wanted to use an object file eh) .. but with your source it's not sessesairy anymore ..

again, thx a LoT !

0
 
LVL 20

Expert Comment

by:Madshi
ID: 1353890
Hey, I'm glad that we could help you...   :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now