this source must be converted to delphi

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);
}
moitoiAsked:
Who is Participating?
 
MadshiConnect With a Mentor Commented:
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
 
slautinCommented:
Are you have problems with "callback"?
0
 
rwilson032697Commented:
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
rwilson032697Commented:
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
 
MadshiCommented:
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
 
MadshiCommented:
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
 
MadshiCommented:
moitoi,

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

Regards, Madshi.
0
 
rwilson032697Commented:
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
 
MadshiCommented:
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
 
moitoiAuthor Commented:
thx .. you guys are great =)
but I will thest it firt k?

0
 
moitoiAuthor Commented:
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
 
moitoiAuthor Commented:
Adjusted points to 205
0
 
moitoiAuthor Commented:
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
 
MadshiCommented:
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
 
moitoiAuthor Commented:
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
 
MadshiCommented:
Hey, I'm glad that we could help you...   :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.