Type Mismatch in Function Call 
Author Message
 Type Mismatch in Function Call

I have some VB code that executes a FindFirstFile, This code is in a
different module so I can execute it from multiple projects. When ever I
call the function I get a type mismatch. I'm sure the definition is the same
in each module. The code being called looks like this:

Private Type FILETIME
     dwLowDateTime As Long
     dwHighDateTime As Long
End Type

Private 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

Function FindFirst(ByVal lpFileName As String, lpFindFileData As
WIN32_FIND_DATA)

Dim lRtnErr As Long

lRtnErr = FindFirstFile(lpFileName, lpFindFileData)

FindFirst = lRtnErr

End Function
--------------------
The code calling the above code looks like this:

Dim lReturn As Long
Dim strFileName As String
Dim uFindData As WIN32_FIND_DATA

strFileName = strCurrentPath & "*.exe"

lReturn = FindFirst(strFileName, uFindData)

If lReturn = -1 Then
    Exit Sub
End If
--------------------
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.

Why would I get this error?

Sorry for the long message, and thanks for the help.

Gary



Fri, 06 Oct 2000 03:00:00 GMT  
 Type Mismatch in Function Call

I am not sure if it will change anything, but try changing the following
line:
lReturn = FindFirst(strFileName, uFindData)

to:
lReturn = FindFirst(byval strFileName, uFindData)

And make sure that strCurrentPath terminates with a "\".

Regards, Kaufman Alex.



Fri, 06 Oct 2000 03:00:00 GMT  
 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



Sat, 07 Oct 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Type mismatch in sub & function calls

2. Type mismatch in VB when calling the Now function

3. Type Mismatch when calling DLL function

4. Passing user data type to function causes type mismatch error

5. 'type mismatch' when calling COM DLL

6. Type mismatch when calling VB6 COM objects from VB.NET

7. VB5 Calling ActiveX method - Type Mismatch error

8. Type Mismatch when calling Sub -- WHY?

9. Type mismatch setting up call to Oracle Stored Proc

10. Type mismatch - Call proc w/ paramaters

11. Type mismatch defining call to Oracle Stored Proc

12. VB5 Calling ActiveX method - Type Mismatch error

 

 
Powered by phpBB® Forum Software