NO

Author Topic: vb6 snippet to c  (Read 8650 times)

Dav3

  • Guest
vb6 snippet to c
« on: December 28, 2011, 03:54:07 PM »
Hi! i´m  newbie in c ( so i really like pelles ), and i found a vb6 snippet that i can't translate to c. Someone can help me? thank you.

Private Function GetPassword()

    On Error GoTo ErrHand

    Dim Access2000Decode As Variant
   
    Dim fFile       As Integer
    Dim bCnt        As Integer
   
    Dim retXPwd(17) As Integer
    Dim wkCode      As Integer
    Dim mgCode      As Integer
   
   
    Access2000Decode = Array(&H6ABA, &H37EC, &HD561, &HFA9C, &HCFFA, _
                      &HE628, &H272F, &H608A, &H568, &H367B, _
                      &HE3C9, &HB1DF, &H654B, &H4313, &H3EF3, _
                      &H33B1, &HF008, &H5B79, &H24AE, &H2A7C)

    If Len(File) > 0 Then
   
        fFile = FreeFile
   
        Open File For Binary As #fFile
            Get #fFile, 67, retXPwd
            Get #fFile, 103, mgCode
        Close #fFile
       
        mgCode = mgCode Xor Access2000Decode(18)

        str2000 = vbNullString

        For bCnt = 0 To 17

            wkCode = retXPwd(bCnt) Xor Access2000Decode(bCnt)
           
            If wkCode < 256 Then
                str2000 = str2000 & Chr(wkCode)
            Else
                str2000 = str2000 & Chr(wkCode Xor mgCode)
            End If
           
        Next bCnt
       
    Else
   
       str2000 = "No file Selected"
   
    End If
   
Exit Function
ErrHand:
    MsgBox "Error with opening file", vbCritical, App.Title


End Function

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: vb6 snippet to c
« Reply #1 on: December 28, 2011, 06:46:15 PM »
Something like this:
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#pragma comment(lib, "user32.lib")

char *GetPassword(char *szFile)
{
unsigned short aAccess2000Decode[] = {
0x6ABA, 0x37EC, 0xD561, 0xFA9C, 0xCFFA, 0xE628, 0x272F, 0x608A,
0x0568, 0x367B, 0xE3C9, 0xB1DF, 0x654B, 0x4313, 0x3EF3, 0x33B1,
0xF008, 0x5B79, 0x24AE, 0x2A7C };
static char str2000[19];
HANDLE hFile;
unsigned short retXPwd[17];
unsigned short wkCode, mgCode;
DWORD nRead;

str2000[0] = 0;
str2000[18] = 0;
hFile = CreateFile(szFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
SetFilePointer(hFile, 66, NULL, FILE_BEGIN); // 67 -> 43h
ReadFile(hFile, &retXPwd, sizeof(short)*17, &nRead, NULL);
SetFilePointer(hFile, 102, NULL, FILE_BEGIN); // 103 -> 67h
ReadFile(hFile, &mgCode, sizeof(short), &nRead, NULL);
CloseHandle(hFile);
mgCode ^= aAccess2000Decode[18];
for (int iCnt = 0; iCnt <= 17; iCnt++) {
wkCode = retXPwd[iCnt] ^ aAccess2000Decode[iCnt];
if (wkCode < 256)
str2000[iCnt] = (char)wkCode;
else
str2000[iCnt] = (char)(wkCode ^ mgCode);
}
}
return str2000;
}

int main(int argc, char **argv)
{
MessageBox(0, GetPassword("test.bin"), 0, MB_OK);
return 0;
}
May the source be with you

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: vb6 snippet to c
« Reply #2 on: December 28, 2011, 07:28:15 PM »
Well, timovjl beat me to it.

However, as a general note, this routine works only for the database password of Access 2000 database files (.MDB), not for any user password in the database.

And if this routine you posted in VB works, then I finally know why my function that I have in Pascal (Delphi) doesn't work (slight differences in the XOR constants, but that could be Access version dependend).

Ralf

Dav3

  • Guest
Re: vb6 snippet to c
« Reply #3 on: December 28, 2011, 10:15:18 PM »
Good job. It seems that works, but no for me. I tried to compile with pelles and a lot of errors appears. if i would to compile it as console application, what should i do/change? Thank You.
« Last Edit: December 28, 2011, 10:19:11 PM by Dav3 »

Dav3

  • Guest
Re: vb6 snippet to c
« Reply #4 on: December 28, 2011, 10:28:54 PM »
maybe with fseek and fread?? ...

CommonTater

  • Guest
Re: vb6 snippet to c
« Reply #5 on: December 28, 2011, 10:31:56 PM »
Good job. It seems that works, but no for me. I tried to compile with pelles and a lot of errors appears. if i would to compile it as console application, what should i do/change? Thank You.

It would probably help if we knew what errors you're getting...
Is it 32 or 64 bit code?
Are you creating a project before compiling?
etc.



Dav3

  • Guest
Re: vb6 snippet to c
« Reply #6 on: December 28, 2011, 10:46:36 PM »
if i create a project before, it says a lot of errors in console, win32,win64.

in console:

C:\Program Files\PellesC\Include\Win\basetsd.h(53): error #2001: Syntax error: expected ';' but found 'INT64'.
C:\Program Files\PellesC\Include\Win\basetsd.h(53): warning #2099: Missing type specifier; assuming 'int'.
C:\Program Files\PellesC\Include\Win\basetsd.h(57): error #2120: Redeclaration of '__int64', previously declared at C:\Program Files\PellesC\Include\Win\basetsd.h(53); expected 'int' but found 'unsigned int'.
C:\Program Files\PellesC\Include\Win\basetsd.h(57): error #2001: Syntax error: expected ';' but found 'UINT64'.
C:\Program Files\PellesC\Include\Win\basetsd.h(57): warning #2099: Missing type specifier; assuming 'int'.
C:\Program Files\PellesC\Include\Win\winnt.h(558): fatal error #1014: #error: "No target architecture".

in win32 and 64:

POLINK: error: Unresolved external symbol '_WinMain@16'.



If i compile first, only 1 error:

POLINK: error: Unresolved external symbol 'WinMain'.
POLINK: fatal error: 1 unresolved external(s).

Dav3

  • Guest
Re: vb6 snippet to c
« Reply #7 on: December 28, 2011, 10:48:52 PM »
so it will be great if works in console mode.

CommonTater

  • Guest
Re: vb6 snippet to c
« Reply #8 on: December 28, 2011, 11:00:55 PM »
Ok, copy your source to a safe place and wipe out the project you have now...

Open POIDE (Pelles IDE) and click...
File -> New -> Project

From the dialog select "Win32 Console Program" ... not the wizard version, the empty project version
Give your project a name then click OK...
Click the New icon on the task bar to create a new source file
Now type in your source or copy/paste into the editor
Click save when done and give it a name...
It will ask you if you want to add it to the project say Yes.

Now... if you are including windows.h ...
Go to Project-> Project Options -> Compiler -> Enable Microsoft Extensions = checked.

Now it should build without most of those errors.

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: vb6 snippet to c
« Reply #9 on: December 28, 2011, 11:03:41 PM »
The code will only work as a 32bit executable, as the "int" makes the assumption that it is a 32bit integer, or else the XOR constants won't match. So you can definitely forget it as a 64bit executable...

Ralf
« Last Edit: December 28, 2011, 11:08:44 PM by Bitbeisser »

Dav3

  • Guest
Re: vb6 snippet to c
« Reply #10 on: December 28, 2011, 11:06:29 PM »
Ok, copy your source to a safe place and wipe out the project you have now...

Open POIDE (Pelles IDE) and click...
File -> New -> Project

From the dialog select "Win32 Console Program" ... not the wizard version, the empty project version
Give your project a name then click OK...
Click the New icon on the task bar to create a new source file
Now type in your source or copy/paste into the editor
Click save when done and give it a name...
It will ask you if you want to add it to the project say Yes.

Now... if you are including windows.h ...
Go to Project-> Project Options -> Compiler -> Enable Microsoft Extensions = checked.

Now it should build without most of those errors.


You are right!! It works!! Thank you for help, and to timovjl for code.

Really thank you. A long way to use pelles correctly :P

CommonTater

  • Guest
Re: vb6 snippet to c
« Reply #11 on: December 28, 2011, 11:09:08 PM »
You are right!! It works!! Thank you for help, and to timovjl for code.

Really thank you. A long way to use pelles correctly :P

Ok... now, for your own benefit... spend some time in the help file... Seriously, it's well worth exploring.
Also be aware that putting the text cursor on any coloured keyword and pressing F1 brings up keyword specific help...


CommonTater

  • Guest
Re: vb6 snippet to c
« Reply #12 on: December 28, 2011, 11:16:40 PM »
The code will only work as a 32bit executable, as the "int" makes the assumption that it is a 32bit integer, or else the XOR constants won't match. So you can definitely forget it as a 64bit executable...

Ralf

Type int and windows INT are still 32 bit values in x64 programs, Ralph ...
Pointers and addresses are 64 bit... the rest is pretty much the same.

Dav3

  • Guest
Re: vb6 snippet to c
« Reply #13 on: December 28, 2011, 11:20:36 PM »
Thank you, i will do it. And Read http://en.wikibooks.org/wiki/C_Programming will be good idea :). i have troubles with pinters, array pointers,... i'm from vb6 :S.

Really thank you

CommonTater

  • Guest
Re: vb6 snippet to c
« Reply #14 on: December 29, 2011, 04:13:55 AM »
Pointers are the #1 thing people trip over in C ... including myself, Dav.  I came from a Pascal background and like VB6, pointers were seldom used since almost everything could be passed around without them.  Not so in C...

C doesn't have a real "pass by reference" mechanism.  Everything is "pass by value" so you are always working from copies of variables.  The whole pointer thing is a way to get around that by passing in a copy of the address of something so you can manipulate it beyond the current function.

* means "contents at"
& means "address of"

The simple analogy is to a post office...  The pointer has the mailbox number.  The dereference operator (the *) is the key that opens it.  The data is the mail in the box.  You need the key to get your mail from the box.

Code: [Select]
int mail;              // mail in your mailbox
int *key;            // your mailbox key (a pointer to an int)
key = &mail;      // assign the key to the the mailbox;
 
mail = 42;         // you've got mail!
             
int read;           // you.

read = *key;   // get your mail (contents at key)

printf("%d\n", read);  // reading the mail.

Yes it's more complex than that, but it should get you started. If you're like me... It will be confusing as all get out until one day you'll discover you've been using them effortlessly for a while and although you really can't give a good explaination of how it works, you do understand how to use them.
« Last Edit: December 29, 2011, 04:17:58 AM by CommonTater »