Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CreateFile library error
#11
That's even more confusing. I am also using _unsigned long for functions concerning attribute returns.

Do I have to modify them as well?
Reply
#12
(06-06-2023, 03:43 AM)eoredson Wrote: That's even more confusing. I am also using _unsigned long for functions concerning attribute returns.

Do I have to modify them as well?
You simply need to read the Microsoft documentation and understand the C++ types used. See the CreateFileA description. `DWORD` is a 32-bit integer, which in QB64 is a `LONG`. `LPCSTR` is a string pointer, and translates to a `String` in QB64 (passed by reference, but that's the default). `LPSECURITY_ATTRIBUTES` is a pointer (LP), so it should be an `_Offset`. And `HANDLE` is a typedef for a `void *`, so in QB64 it should be an `_Offset`.

As a separate question, is there a reason you're trying to use `CreateFileA` and other Win32 APIs in the first place? The best way to make your program cross platform is to simply avoid using any `Declare Library` or Win32 APIs, and just use built-in commands like `OPEN` to create files. If you're not comfortable understanding the C++ types then you're likely to just keep running into issues trying to use Win32 APIs. They're all intended for being used in C++ code, not QB64, you can call them but you need C++ knowledge to really do it properly.
Reply
#13
What if I am using the following to get a filename/directory attribute:

Does this throw a 64-bit compilation error as well?

Code: (Select All)
Rem $Dynamic
DefLng A-Z
Declare Dynamic Library "kernel32"
    Function CloseHandle& (ByVal hfile As _Offset)
End Declare

Rem hfind = CreateFile(ASCIIZ, &H180, &H3, 0, 3, 0, 0)
' parameters:
'  (1) pointer to filename
'  (2) access:
'    x80(128)  - read
'    x100(256) - write
'  (3) sharing
'  (4) security attributes
'  (5) create file flag
'  (6) flags (standard OSHA)
'  (7) pointer to template file

' paramater 5
'  0 DEFAULT_OPEN_EXISTING = open only if exists
'  1 CREATE_NEW    = create only if not exist
'  2 CREATE_ALWAYS = always create new file
'  3 OPEN_EXISTING = open only if exists
'  4 OPEN_ALWAYS  = open file always
'  5 TRUNCATE_EXISTING = open/truncate to 0 only if exists
'  6 OPEN_DIRECTORY    = open if directory exists

Declare Library
    Function CreateFileA%& (filename$, Byval access&, Byval sharing&, Byval sec_attr%&, Byval create&, Byval flags&, Byval template%&)
    Function GetFileAttributes& (f$)
End Declare
Dim hfind As _Offset
Dim Attribute As _Unsigned Long

' detect file
Print "Enter filename";
Input f$
If Len(f$) Then
    f$ = f$ + Chr$(0)
    hfind = CreateFileA(f$, &H180, 0, 0, 3, 0, 0)
    If hfind Then
        Print "File exists."
        Print "Handle: "; hfind
        r = CloseHandle(hfind)
        Attribute = GetFileAttributes&(f$)
        If (Attribute And &H1) = &H1 Then Print "  Read-only"
        If (Attribute And &H2) = &H2 Then Print "  Hidden"
        If (Attribute And &H4) = &H4 Then Print "  System"
        If (Attribute And &H10) = &H10 Then Print "  Directory"
        If (Attribute And &H20) = &H20 Then Print "  Archive"
    End If
End If
End
Reply
#14
I need to know because I might have the same problem with GetFileAttribute as I do with CreateFile..

Erik.
Reply
#15
I'd recommend using Declare CustomType Library instead. Or, if you want to avoid compilation errors entirely, declare the DLL that contains that function rather than trying to use the headers.
Schuwatch!
Yes, it's me. Now shut up.
Reply
#16
(06-06-2023, 06:06 AM)Ultraman Wrote: I'd recommend using Declare CustomType Library instead. Or, if you want to avoid compilation errors entirely, declare the DLL that contains that function rather than trying to use the headers.
I would definitely not recommend that. In this case all `CustomType` would do is allow you to ignore the C++ function declaration already present from the header, you should only do that if it's necessary. In this case, the compilation error is because eoredson had a legitimate bug in his program and as a consequence it would not work on 64-bit. Using `CustomType` would have allowed it to compile, but not actually fix the bug. It provides a false sense of security and you shouldn't be using it unless actually necessary.

(06-06-2023, 04:29 AM)eoredson Wrote: I need to know because I might have the same problem with GetFileAttribute as I do with CreateFile..

Erik.
Again, read the Microsoft documentation, it tells you the exact information you're looking for. The GetFileAttributeA function documentation is here. The provided syntax is this:

Code: (Select All)
DWORD GetFileAttributesA(
  [in] LPCSTR lpFileName
);
The return type is a `DWORD` and the parameter is an `LPCSTR`.
Reply
#17
I guess the many, many times I've used Declare CustomType Library with zero issues have all been flukes.
Schuwatch!
Yes, it's me. Now shut up.
Reply
#18
(06-07-2023, 09:44 PM)Ultraman Wrote: I guess the many, many times I've used Declare CustomType Library with zero issues have all been flukes.
I'm not saying CustomType is always the wrong thing to use, there are times when it's necessary, what I'm saying is that it's the wrong thing to use here because it doesn't solve the actual problem. CustomType just lets you ignore the C++ types, it doesn't actually fix your declaration to have the right types. In cases like this one the C++ types are quite helpful because they identify if you're passing the right types at compile time and avoid having to debug runtime issues.

Adding CustomType to eroedson's original code wouldn't have made it work correctly, it would have just allowed it to compile.
Reply
#19
According to Google a DWORD is a unsigned 32-bit which in QB64 is declared as _Unsigned Long
so therefore I don't need to change any calls to Function GetFileAttributes lucky enough!?

Erik.
Reply
#20
Windows 64-bit applications:

Name/Length/QB64
----------------------
WORD/2 bytes/Integer
DWORD/4 bytes/Long or _offset
HANDLE/8 bytes/_Integer64 or _offset
HFILE/4 bytes/Long

https://www.ibm.com/docs/en/ibm-mq/7.5?t...data-types
Reply




Users browsing this thread: 4 Guest(s)