.NETGURU
CallWndProc() upgrade problem
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.vb.upgrade.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

The Grim Reaper
Hi all,

I've come across a wonderful bit of code for generating a 32 CRC on a file
at very high speeds - unfortunately, it's in assembly... the code in VB6
works fine, but I can't get the thing to upgrade to .NET :(

VB6 code (very edited);

Private mvCRC32 As Long ' This value starts at 0
and returns from AddBytes with the file CRC
Private mvCRC32Asm() As Byte ' This array is filled with
pre-compiled assembly routine
Private mvCRC32Table(0 To 255) As Long ' This array is also filled with
assembly routine data (hard coded)
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA"
(ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal
wParam As Long, ByVal lParam As Long) As Long

' pByteArray() is the file itself - in bytes as you can no doubt see...
Public Function AddBytes(pByteArray() As Byte) As Variant
Dim vByteSize As Long
vByteSize = UBound(pByteArray) - LBound(pByteArray) + 1
CallWindowProc VarPtr(mvCRC32Asm(0)), VarPtr(mvCRC32),
VarPtr(pByteArray(LBound(pByteArray))), VarPtr(mvCRC32Table(0)), vByteSize
AddBytes = Not(mvCRC32)
End Function

VB.NET upgraded code;

Private mvCrc32 As Long ' This value starts at 0
and returns from AddBytes with the file CRC
Private mvCrc32Assembly() As Byte ' This array is filled with
pre-compiled assembly routine
Private mvCrc32Table(0 To 255) As Long ' This array is also filled with
assembly routine data (hard coded)
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal
lpPrevWndFunc As Integer, ByVal hWnd As Integer, ByVal Msg As Integer, ByVal
wParam As Integer, ByVal lParam As Integer) As Integer

' pByteArray() is the file itself - in bytes as you can no doubt see...
Public Function AddBytes(pByteArray() As Byte) As Integer
Dim vByteSize As Integer = pByteArray.Length
' CallWindowProc VarPtr(mvCRC32Asm(0)), VarPtr(mvCRC32),
VarPtr(pByteArray(LBound(pByteArray))), VarPtr(mvCRC32Table(0)), vByteSize
Return = Not(mvCRC32)
End Function

The majority of the routine upgraded to .NET with no problems as you can
see. The only thing is the CallWindowProc procedure itself. VarPtr was
unsupported in VB6, and obviously was scrapped with .NET. I have NO idea
how to use Marshaling attributes properly - I've read the MSDN over and over
and still can't get my head round it. I had a look at the
System.Windows.Forms.Form.Message structure, which seems very useful;

Imports System.Windows.Forms.Form
Dim vPtrHwnd As New IntPtr(mvCrc)
Dim vPtrWParam As New IntPtr(mvCrcTable(0))
Dim vPtrLParam As New IntPtr(vByteSize)
Dim vMessage As Message = Message.Create(vPtrHwnd, pByteArray(0),
vPtrWParam, vPtrLParam)
gvFormCRCTest.WndProc(vMessage)

I'm very tired, but my eyes still tell me that IntPtr(mvCrcTable(0)) isn't
going to work!! How do you create a pointer to an array in VB?? The
WndProc routine passes the message fine, but on return the vPtrHwnd
parameter isn't set to the file CRC. The Message.Create function doesn't
take the lpPrevWndFunc that the original API did, and this is where the
assembly code is passed in!!

Anyone got any ideas how to properly pass an assembly routine in VB.NET?!?!
I'm really desperate to know now!! But as a temporary measure, I'm going to
compile the working VB6 code to a dll and use that from .NET.... I'll see
how slow it gets... :(
________________________________________
The Grim Reaper

(All VB6 coding credit goes to the original author... whoever they are...!!)

Reply to this message...
 
    
David Williams (VIP)
While I am far from the best, I noticed a few things:

The conversion from

Private mvCrc32 As Long

to

Private mvCrc32 As Long

is wrong. It should be

Private mvCrc32 As Integer

I would also try using DLLImport instead of Declare in this case - it will give you better control. Try

[DllImport("user32", EntryPoint="CallWindowProc")] Public Shared Function CallWindowProcA(int lpPrevWndFunc, int hwnd, int MSG, int wParam, int lParam) As Integer

Also see: http://custom.programming-in.net/articles/art9-1.asp?f=CallWindowProc

--
David Williams, VB.NET MVP

"The Grim Reaper" wrote:

[Original message clipped]

Reply to this message...
 
    
The Grim Reaper
Thanks for your help David. Can you explain how DllImport gives better
control exactly??

This is the code I've got so far;

<DllImport("user32", EntryPoint:="CallWindowProc")> Public Shared Function
CallWindowProcA(ByVal pPrevWndFunc As Integer, ByVal pHWnd As Integer, ByVal
pMesg As Integer, ByVal pWParam As Integer, ByVal pLParam As Integer) As
Integer
End Function
Private mvCrcAssembly() As Byte
Private mvCrc As Integer, mvCrcTable(255) As Integer

Public Function CalcCRC(ByVal pByteArray() As Byte) As Integer
Dim vByteSize As Integer = pByteArray.Length
Try
' Dim vPtrHwnd As New IntPtr(mvCrc)
' Dim vPtrWParam As New IntPtr(mvCrcTable(0))
' Dim vPtrLParam As New IntPtr(vByteSize)
' Dim vMessage As Message = Message.Create(vPtrHwnd, pByteArray(0),
vPtrWParam, vPtrLParam)
Dim vResult As Integer
vResult = CallWindowProcA(mvCrcAssembly(0), mvCrc, pByteArray(0),
mvCrcTable(0), vByteSize)
Return mvCrc
Catch vException As Exception
MessageBox.Show(vException.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try
End Function

I'm aware of 2 problems;

1) The CallWindowProcA() function from the original VB6 code was calling
pointers (VarPtr()) to the variables, passing the variable values
themselves. Should they be passed ByRef?? Or is there an equivalent of
VarPtr() in the Framework??

2) The above code bombs out on the CallWindowProcA() function with the
Exception.Message "Object Reference not set to an instance of an object".
Can anyone explain what the **** it's going on about?!?!

Any more help from anyone on this code would be really ... um.... helpful :D
And appreciated! My last resort is to learn how the assembly code works and
try to replicate that functionality in .NET with the file ByteArray() :(( I
can see that being extremely slow... the assembly's bad enough on a 700 meg
file... lol
__________________________________
The Grim Reaper

"David Williams" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

lParam) As Integer
[Original message clipped]

Reply to this message...
 
 
System.Exception
System.IntPtr
System.Messaging.Message
System.Windows.Forms.Form
System.Windows.Forms.MessageBox
System.Windows.Forms.MessageBoxButtons
System.Windows.Forms.MessageBoxIcon




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification