Re: Problem with passing parameter

Tech-Archive recommends: Speed Up your PC by fixing your registry



Thank you for your response, but I cannot do the most of the things you are
suggesting, Randy.

1.
Appending alias to API
================

When I change declaration from:
Declare Function WriteFile Lib "kernel32.dll" (ByVal hFile As Long, lpBuffer
As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long,
lpOverlapped As OVERLAPPED) As Long
to
Declare Function WriteFile Lib "kernel32.dll" Alias "WriteFileA" (ByVal
hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long,
lpNumberOfBytesWritten As Long, lpOverlapped As OVERLAPPED) As Long

I have Run-ime error '453':
Can't find DLL entry point WriteFileA in kernel32.dll

which tells me I cannot append Alias to that API.

Please notice, I am using OVERLAPPED structure, because the object (comport)
I am writing to is opened with FILE_FLAG_OVERLAPPED flag.

2.
To make it simpler I redesigned my code.
Declare Function WriteFile Lib "kernel32.dll" (ByVal hFile As Long, lpBuffer
As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long,
lpOverlapped As OVERLAPPED) As Long
Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As
Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long,
lpOverlapped As OVERLAPPED) As Long

Public sPagerChr As String

sPagerChr = Chr(13)

Public Sub WriteData(ByRef RecData As String)
Dim BytesWrite As Long
Dim b As OVERLAPPED
Dim indata(255) As Byte
Dim BytesRead As Long
Debug.Print "sPagerChr: " & sPagerChr & " " & Len(sPagerChr)
rtn = WriteFile(ComHwnd, ByVal sPagerChr, Len(sPagerChr),
BytesWrite, b)
If rtn = 0 Then
rtn = GetLastError 'rtn = GetLastError
Debug.Print "WriteFile_LastError: " & rtn
' If rtn = 997 Then '997 = ERROR_IO_PENDING
rtn = GetOverlappedResult(ComHwnd, b, BytesWrite, True) '
(does not return until completed)
Debug.Print "WriteFile_BytesWrite: " & BytesWrite
End If
End If
rtn = ReadFile(ComHwnd, indata(0), BytesRead, BytesRead, b)
If rtn = 0 Then
rtn = err.LastDllError 'rtn = GetLastError
Debug.Print "ReadFile_LastError: " & rtn
If rtn = 997 Then 'ERROR_IO_PENDING
rtn = GetOverlappedResult(ComHwnd, b, BytesRead, True)
'True=asynchr, False=synchr
Debug.Print "ReadFile_BytesRead: " & BytesRead
For i = 0 To BytesRead - 1
RecData = RecData & Chr(indata(i))
Next
Debug.Print "1: " & RecData
End If
End If
End Sub
============================
Having code as above, I do not have any error indication.
What do you think about it? Any other recommendation to improve the
performance?
I appreciate your advice.
Thanks,
Frank
"Randy Birch" <rgb_removethis@xxxxxxxx> wrote in message
news:OoyjML1DGHA.2956@xxxxxxxxxxxxxxxxxxxxxxx
> OK OK...
>
> 1) Your declare for WriteFile is incorrect. For all intents and purposes,
> you can believe that all APIs that receive strings as parameters (or
> return
> strings for that matter) must be aliased to either the "A" (ANSI)
> declaration or the "W" (wide [Unicode]) declaration (W receives byte
> arrays
> as strings). Since you're using strings you'd alias this API with an "A".
> The API viewer correctly tries to help by showing that WriteFile has an
> alias, but the viewer incorrectly forgets to append the "A" to the alias
> name. In VB, if an API alias name is the same as the API name itself, VB
> removes the alias. So pasting the data from the API viewer:
>
> Private Declare Function WriteFile Lib "kernel32" Alias "WriteFile" (ByVal
> .....
>
> ... results in the incorrect declaration:
>
> Private Declare Function WriteFile Lib "kernel32" (ByVal .....
>
> ... whereas the correct declare is:
>
> Private Declare Function WriteFile Lib "kernel32" Alias "WriteFileA"
> (ByVal
> .....
>
>
> 2) You can *not* pass a variant, even if it's type is string, to an API
> expecting a string data type - the layout of the data in memory is
> different
> and to the API you passed garbage. Declare the Data parameter "as String"
> in
> your WriteData declare, and if you need to pass numerical data convert
> that
> to a string before passing to WriteData.
>
> 3) Also, the numeric parameters are longs, so declare Length "as Long" in
> the WriteData declare, not "as integer".
>
> 4) If your variable in the API declare is "as Any", don't declare it
> ByVal.
> As Any implies you will be using the variable for any number of data
> types,
> in which case you use ByVal in the call to the API when required....
>
> Declare Function WriteFile Lib "kernel32" Alias "WriteFileA" (ByVal hFile
> As
> Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long,
> lpNumberOfBytesWritten As Long, lpOverlapped As Any) As Long
>
> Public Sub WriteData(Data As String, Length As Long, RecData As String)
>
> rtn = WriteFile(ComHwnd, ByVal Data, Length, BytesWrite, ByVal 0&)
>
> 5) You don't show your CreateFile declaration and call, but if you are
> passing FILE_FLAG_OVERLAPPED as part of the dwFlagsAndAttributes member,
> remove the FILE_FLAG_OVERLAPPED flag it as you don't want to indicate you
> want an overlapped file when you are passing ByVal 0& as a null for the
> OVERLAPPED member.
>
> 6) Stop using Data as a variable name -- Data is a reserved keyword in VB
> that refers to a member of the OLE container properties. Personally I'd
> also
> drop using Length as well, as this is bound to conflict with something at
> some point. (If you use a variable name that at one point in development
> appears to work OK but later starts to give you problems, chances are that
> a
> control or reference you've added to the project is using that same name
> as
> one of its reserved words. You're better off with a bit of prefixing to
> the
> variables to indicate their data type (and I mean a bit!!); I use 's' for
> strings, 'dw' for Longs (dw=DWORD=VB Long), etc. So you could have sData
> and dwLength, etc. Do avoid going overboard though, nothing worse than
> reading though code where the drug use^H^H^H^H^H^H^Hdeveoper used names
> like
> mod_glob_str_Data or some other bastardization of common sense.)
>
> --
>
> Randy Birch
> MS MVP Visual Basic
> http://vbnet.mvps.org/
>
> Please reply to the newsgroups so all can participate.
>
>
>
>
> "Frank" <replyto@newsgroup> wrote in message
> news:OM8HqTyDGHA.3820@xxxxxxxxxxxxxxxxxxxxxxx
> :I still have the problems.
> : I tried all:
> : Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, ByVal
> : lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long,
> lpNumberOfBytesWritten
> : As Long, lpOverlapped As Any) As Long
> :
> :
> : when I do this:
> : Public Sub WriteData(ByVal Data As Variant, ByVal Length As Integer,
> ByRef
> : RecData As String)
> :
> : rtn = WriteFile(ComHwnd, ByVal Data, Length, BytesWrite, b)
> :
> : visual basic is complaining about bad dll convertion
> :
> : but when I do this:
> : rtn = WriteFile(ComHwnd, Data, Length, BytesWrite, b)
> : visual basic terminates (no error indication) after 3rd time executing
> that
> : sub.
> :
> :
> : Frank
> :
> : "Randy Birch" <rgb_removethis@xxxxxxxx> wrote in message
> : news:uv6mjkjDGHA.3752@xxxxxxxxxxxxxxxxxxxxxxx
> : > Data is a variant -- type it properly in the WriteData declare. Also,
> Chr
> : > returns a variant of type string; Chr$ returns a string directly. And
> : > since
> : > lpBuffer is defined As Any in the WriteFile declare, you have to pass
> your
> : > strings using ByVal in the API call, e.g. ByVal Data. Finally, unless
> : > you're
> : > using the Overlapped structure you can pass ByVal 0& as lpOverlapped
> with
> : > the same effect as your passing an empty OVERLAPPED structure.
> : >
> : > --
> : >
> : > Randy Birch
> : > MS MVP Visual Basic
> : > http://vbnet.mvps.org/
> : >
> : > Please reply to the newsgroups so all can participate.
> : >
> : >
> : >
> : >
> : > "Frank" <replyto@newsgroup> wrote in message
> : > news:eV0fbTjDGHA.412@xxxxxxxxxxxxxxxxxxxxxxx
> : > : Hello,
> : > : I have problem with passing parameter from one subroutine
> to
> : > the
> : > : function in another sub.
> : > : When I insert Chr(13) directly into WriteFile() function like that:
> : > : rtn = WriteFile(ComHwnd, Chr(13), 1, BytesWrite, b)
> : > : I do not have problem.
> : > : However when running the code below the Chr(13) is not being
> written.
> : > : Your thoughts please.
> : > :
> : > : Here is the code:
> : > :
> : > : Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long,
> lpBuffer
> : > As
> : > : Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As
> : > Long,
> : > : lpOverlapped As Any) As Long
> : > :
> : > :
> : > : Public Sub tmrTapProt_Timer
> : > :
> : > : dim sPagerChr as String
> : > : sPagerChr = Chr(13)
> : > : Call WriteData(sPagerChr, Len(sPagerChr), lpString)
> : > : ............
> : > : End Sub
> : > :
> : > : Public Sub WriteData(ByVal Data, ByVal Length As Integer, ByRef
> RecData
> : > As
> : > : String)
> : > : Dim BytesWrite As Long
> : > : Dim b As OVERLAPPED
> : > : rtn = WriteFile(ComHwnd, Data, Length, BytesWrite, b)
> : > : ............
> : > : End Sub
> : > :
> : > :
> : >
> :
> :
>


.


Quantcast