
Type Mismatch in Function Call
Quote:
> ... when ever I execute the above code I get the following error:
>"ByRef argument type mismatch". The error is on the uFindData definition.
>Both modules are using the same definition of WIN32_FIND_DATA ( I copied
>from one to the other). I also copied the FILETIME definition.
Hehe, well it SEEMS that you've got THE SAME definition, but you DON'T,
because it's a different piece of code ...
When compiling the compiler generates some sort of references to these
definitions. When it compiler the module, it generates a reference to the
structure in the module ... let's call it "ref1" and assigns it to the
variable in the function in the module. When you COPY (even exactly) the
definition structure to your form module, where you want to call it ... the
compiler generates an NEW reference, because it would take him too much time
:))) to check if it might not be the same text you've written ... so he
calls the new structure, though it's the same ... "ref2" ... Then when he
come to the call to the module function, he gets :
lReturn = FindFirst(strFileName, uFindData)
where uFindData is defined as "ref2"
and his FindFirst function (in the module) is defined with
uFindData is defined as "ref1"
So they don't match and the compiler issues an error ...
That's the theoretical part of the answer ... now what you probably are more
interested in is ...
how to solve it ... well pretty simple ... let both the module as well as
the form module use THE SAME definitions ...
so in stead of using the Private keyword, indication that only the module
can see the definition, you use the Public keyword, which makes the
definitions also visible in the form module...
resulting in the following code ...
< --- code for module begins here --- >
Public Const MAX_PATH = 260
Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Public Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA"
(ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Function FindFirst(ByVal lpFileName As String, lpFindFileData As
WIN32_FIND_DATA)
Dim lRtnErr As Long
lRtnErr = FindFirstFile(lpFileName, lpFindFileData)
FindFirst = lRtnErr
End Function
< --- code for module ends here --- >
on a form with a button ...
< --- code for form begins here --- >
Private Sub Command1_Click()
Dim lReturn As Long
Dim strFileName As String
Dim uFindData As WIN32_FIND_DATA
strFileName = strCurrentPath & "*.exe"
lReturn = FindFirst(strFileName, uFindData)
' now you can do whatever you like ... :)
End Sub
< --- code for form ends here --- >
This solution frees you also from copying the same structures over and over
again, ad gives you the possibility to just call you function, without
having to worry about the definitions, ... and isn't that what modules are
for ?! ...
hope this solves your problem
greetz
Christophe