|
-
Mar 23rd, 2008, 02:14 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Comparing a Resource File with Actual File
How would i compare a resource file in my project, with an actual file on the computer? heres my code, but it doesn't work. Anyone got any ideas or fixes?
Code:
Private Sub CheckBinary()
Dim byteRes() as Byte, byteFile() as Byte
Dim xFile as Integer
'Load the resource data to byteRes
byteRes() = LoadResData(101,"CUSTOM")
'Load the computer file onto byteFile
xFile = FreeFile
Open "C:\Program.exe" For Binary Access Read As #xFile
Get #xFile, ,byteFile()
Close #xFile
'Compare both bytes together
If byteRes() = byteFile() Then
Msgbox "They are the same!"
Else
Msgbox "They are NOT the same!"
End If
End Sub
That code doesn't really work for me. Any help?
NOTE THAT BOTH THE RES FILE AND THE ACTUAL FILE ON THE COMPUTER ARE THE SAME FILES
If ive helped, RATE my post.
If your thread is solved, and you got your answer, mark this thread Resolved.
There is no other forum like VBFORUMS.
-
Mar 23rd, 2008, 02:46 PM
#2
Thread Starter
Fanatic Member
Re: Comparing a Resource File with Actual File
I figured out the correct Get code. Here it is. All that is left now, is comparing both files (which still doesn't work even though they are the same, it always turns up that they aren't)
Code:
Private Sub CheckBinary()
Dim byteRes() as Byte, byteFile() as Byte
Dim xFile as Integer
'Load the resource data to byteRes
byteRes() = LoadResData(101,"CUSTOM")
'Load the computer file onto byteFile
xFile = FreeFile
Open "C:\Program.exe" For Binary Access Read As #xFile
Redim byteFile(LOF(xFile))
Get #xFile, ,byteFile()
Close #xFile
'Compare both bytes together
If CStr(byteRes()) = CStr(byteFile()) Then
Msgbox "They are the same!"
Else
Msgbox "They are NOT the same!"
End If
End Sub
If ive helped, RATE my post.
If your thread is solved, and you got your answer, mark this thread Resolved.
There is no other forum like VBFORUMS.
-
Mar 23rd, 2008, 02:52 PM
#3
Re: Comparing a Resource File with Actual File
There a few ways to do this. However, as you learned, you cannot compare arrays that way -- would be nice if you could.
The first thing to do is compare the size of the arrays; if they are not same size; they can't contain same data.
The next thing to do is compare the contents
1. Easy but far less efficient, convert both arrays to Strings using StrConv(). Then compare the strings using StrComp(). Example shown below
Code:
Private Sub Command1_Click()
Dim a() As Byte, b() As Byte
Dim s1 As String, s2 As String
ReDim a(0 To 10)
ReDim b(0 To 10)
a(5) = 1
b(5) = 1
s1 = StrConv(a, vbFromUnicode)
s2 = StrConv(b, vbFromUnicode)
Debug.Print "arrays are equal? "; (StrComp(s1, s2, vbBinaryCompare) = 0)
b(5) = 2
s2 = StrConv(b, vbFromUnicode)
Debug.Print "arrays are equal? "; (StrComp(s1, s2, vbBinaryCompare) = 0)
End Sub
2. Use APIs and compare several bytes at a time, looping thru the array. Recommend a strategy like this if you want to perform checks very often. Can significantly improve performances. I have an example of such a routine, on PSC, that mimics C++ memcmp DLL and surprisingly is nearly as fast. On NT based machines, you have an API that can do it without looping: RTLCompareMemory API. That PSC link shows example of using it.
-
Mar 23rd, 2008, 02:58 PM
#4
Thread Starter
Fanatic Member
Re: Comparing a Resource File with Actual File
Alright, i tested your code and it seems to be what i want.
The thing is, do i leave a(0 to 10) and b(0 to 10) there or was that as a demonstration? why 0 to 10?
If ive helped, RATE my post.
If your thread is solved, and you got your answer, mark this thread Resolved.
There is no other forum like VBFORUMS.
-
Mar 23rd, 2008, 03:04 PM
#5
Re: Comparing a Resource File with Actual File
Just a demonstraton. You would obviously use your own arrays; replace the a & b in the StrConv() function with your arrays.
-
Mar 23rd, 2008, 03:08 PM
#6
Thread Starter
Fanatic Member
Re: Comparing a Resource File with Actual File
i know. but i meant the 0 to 10 thing. but hold on 1 sec. i modified my code to this
Code:
Private Sub CheckBinary()
Dim byteRes() as Byte, byteFile() as Byte
Dim strRes as String, strFile as String
Dim xFile as Integer
'Load the resource data to byteRes
byteRes() = LoadResData(101,"CUSTOM")
'Load the computer file onto byteFile
xFile = FreeFile
Open "C:\Program.exe" For Binary Access Read As #xFile
Redim byteFile(LOF(xFile))
Get #xFile, ,byteFile()
Close #xFile
'Convert byte to string
strRes = StrConv(byteRes, vbFromUnicode)
strFile = StrConv(byteFile, vbFromUnicode)
'Compare both strings together
If strRes = strFile Then
Msgbox "They are the same!"
Else
Msgbox "They are NOT the same!"
End If
End Sub
Now i tested this with a file that isn't the same as the res file, but its saying it is the same. any recommendations to what i should do with it now?
EDIT:
I guess I should give you alittle more background about this so you know what im trying to do.
The program in the resource file is a patched version of 1 of my programs that ive made.
The program in the computer is the unpatched version of 1 of my programs.
im trying to compare the resource file with the computer file and see if they are the same. The patched version has like, 1 or 2 bytes that have been changed comparing to the unpatched version which has nothing unchanged.
First, the file on the computer is unpatched, and it compares it to the patched file. so i want it to patch it if the comparison that we have been working on, returns that they aren't same value in binary. then, lets say i try to patch it again, even though its already patched, i want my program to check if its the same value using that comparison, and if they are both the same value, say that they are the same files.
Last edited by GRPsuper9; Mar 23rd, 2008 at 03:18 PM.
If ive helped, RATE my post.
If your thread is solved, and you got your answer, mark this thread Resolved.
There is no other forum like VBFORUMS.
-
Mar 23rd, 2008, 03:14 PM
#7
Re: Comparing a Resource File with Actual File
2 questions:
1. How do you know they are not the same?
2. If the answer is because one has lower case & the other has uppercase, fix this by using the StrComp() function as I provided in my example.
Also, recommend testing the size of each array before even comparing. Sizes not equal, arrays not equal.
-
Mar 23rd, 2008, 03:18 PM
#8
Re: Comparing a Resource File with Actual File
Should be "vbUnicode" in your StrConv() line.
From byte to string: vbUnicode
From string to byte: vbFromUnicode
So...
Code:
'Convert byte to string
strRes = StrConv(byteRes, vbUnicode)
strFile = StrConv(byteFile, vbUnicode)
Easy to get those 2 confused sometimes...
-
Mar 23rd, 2008, 03:26 PM
#9
Re: Comparing a Resource File with Actual File
 Originally Posted by DigiRev
Should be "vbUnicode" in your StrConv() line.
Good catch. Sorry about that.
-
Mar 23rd, 2008, 03:30 PM
#10
Thread Starter
Fanatic Member
Re: Comparing a Resource File with Actual File
Its not uppercase or lowercase problem (If you are familiar with reverse engineering, i changed a JNZ to a NOP). And i worked around what you gave me and modified a few things, and this is what i came up with. It works perfectly:
Code:
Private Sub CheckBinary()
Dim byteRes() as Byte, byteFile() as Byte
Dim xFile as Integer
'Load the resource data to byteRes
byteRes() = LoadResData(101,"CUSTOM")
'Load the computer file onto byteFile
xFile = FreeFile
Open "C:\Program.exe" For Binary Access Read As #xFile
Redim byteFile(LOF(xFile))
Get #xFile, ,byteFile()
Close #xFile
'Compare both strings together
If StrComp(byteFile, byteRes, vbBinaryCompare) = -1 Then
Msgbox "They are the same!"
Else
Msgbox "They are NOT the same!"
End If
End Sub
on the StrComp, -1 means they are the same value, and 1 means they aren't. just incase you wanted to know.
Let me know what you guys think, and then ill post this as resolved.
If ive helped, RATE my post.
If your thread is solved, and you got your answer, mark this thread Resolved.
There is no other forum like VBFORUMS.
-
Mar 23rd, 2008, 03:33 PM
#11
Re: Comparing a Resource File with Actual File
Only if StrComp() returns zero should they be considered equal strings, dependent upon the comparison method (i.e., vbBinaryCompare).
-
Mar 23rd, 2008, 03:37 PM
#12
Thread Starter
Fanatic Member
Re: Comparing a Resource File with Actual File
Then what does -1 mean and why is it returning that whenever it actually is the same instead of 0, and why does it return 1 when they aren't the same?
fyi, that question is not intended to be aggressive in any way. i just realized it kinda does alittle which i didn't intend to, so im just telling u this
If ive helped, RATE my post.
If your thread is solved, and you got your answer, mark this thread Resolved.
There is no other forum like VBFORUMS.
-
Mar 23rd, 2008, 03:46 PM
#13
Re: Comparing a Resource File with Actual File
Click on the StrComp link I provided in post #11 for description of function & return values. How big are these things. Can you post your res file and the one you are comparing it against; maybe we can take a look-see. If we return the same issues, then StrConv() may not work for what you need; though it should.
Last edited by LaVolpe; Mar 23rd, 2008 at 04:05 PM.
-
Mar 23rd, 2008, 03:58 PM
#14
Thread Starter
Fanatic Member
Re: Comparing a Resource File with Actual File
my Res file is the size of a normal VB compiled exe. and alright, ill have a look at the link you posted too.
If ive helped, RATE my post.
If your thread is solved, and you got your answer, mark this thread Resolved.
There is no other forum like VBFORUMS.
-
Mar 23rd, 2008, 04:01 PM
#15
Re: Comparing a Resource File with Actual File
Hang on a moment. In your sample code, you are not resizing the byteFile correctly: Incorrect: byteFile(LOF(xFile)), Correct: byteFile(LOF(xFile)-1). Strongly recommend checking the sizes of the 2 arrays first.
-
Mar 23rd, 2008, 04:05 PM
#16
Thread Starter
Fanatic Member
Re: Comparing a Resource File with Actual File
there we go, now its returning 0 . good catch
If ive helped, RATE my post.
If your thread is solved, and you got your answer, mark this thread Resolved.
There is no other forum like VBFORUMS.
-
Mar 23rd, 2008, 04:07 PM
#17
Thread Starter
Fanatic Member
Re: Comparing a Resource File with Actual File
alright, id say this is about resolved
thank you 4 ur help m8 
here is the final coding if anyone is interested in the future:
Code:
Private Sub CheckBinary()
Dim byteRes() as Byte, byteFile() as Byte
Dim xFile as Integer
'Load the resource data to byteRes
byteRes() = LoadResData(101,"CUSTOM")
'Load the computer file onto byteFile
xFile = FreeFile
Open "C:\Program.exe" For Binary Access Read As #xFile
Redim byteFile(LOF(xFile)-1)
Get #xFile, ,byteFile()
Close #xFile
'Compare both strings together
If StrComp(byteFile, byteRes, vbBinaryCompare) = 0 Then
Msgbox "They are the same!"
Else
Msgbox "They are NOT the same!"
End If
End Sub
If ive helped, RATE my post.
If your thread is solved, and you got your answer, mark this thread Resolved.
There is no other forum like VBFORUMS.
-
Mar 23rd, 2008, 04:14 PM
#18
Re: [RESOLVED] Comparing a Resource File with Actual File
Glad you got what you were after. But looking at your code more closely...
You are loading a version of your EXE in the resource file? And using that to do a comparison? When you compile your app, the resource file is compiled into the app thus potentially making your EXE pretty big since another EXE is in the res file. Wouldn't it be better to maybe store a CRC value and do a CRC check on the other file to see if something changed? More of a rhetorical question and food for thought.
-
Mar 23rd, 2008, 04:18 PM
#19
Thread Starter
Fanatic Member
Re: [RESOLVED] Comparing a Resource File with Actual File
i have no idea what CRC is even though ive seen it while reverse engineering other software. If it was easier, and took less space, i would most certainly do that instead. You interested in telling me the basics of it?
If ive helped, RATE my post.
If your thread is solved, and you got your answer, mark this thread Resolved.
There is no other forum like VBFORUMS.
-
Mar 23rd, 2008, 04:22 PM
#20
Re: [RESOLVED] Comparing a Resource File with Actual File
Basically a CRC is a Long value, 4 bytes, though there are CRCs of other lengths. I'd jump to the code bank section or simply do a search; there are lots of CRC routines about. A CRC is value that is created by adding/subtracting each byte with a previous value so that it is unique to the bytes used in the calculation. Therefore, if one byte changes anywhere, the CRC when recalculated would be different.
-
Mar 23rd, 2008, 04:27 PM
#21
Re: [RESOLVED] Comparing a Resource File with Actual File
Last edited by LaVolpe; Mar 23rd, 2008 at 04:34 PM.
-
Mar 23rd, 2008, 06:32 PM
#22
Thread Starter
Fanatic Member
Re: [RESOLVED] Comparing a Resource File with Actual File
Thank you, ill check it out and have a look
If ive helped, RATE my post.
If your thread is solved, and you got your answer, mark this thread Resolved.
There is no other forum like VBFORUMS.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|