Results 1 to 7 of 7

Thread: GetWindowsDirectory API

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 1999
    Location
    Sydney, NSW, Australia
    Posts
    16

    Post

    I am using this API to find the directory where Windows is booting from to open a file called hotline.inf. It is working fine Win '95 work stations, but our only NT work station is not finding the file. The file is in the C:\WINNT directory and the application is attempting to open the file 'C:\WINNT\hotline.inf' but the open fails.

    Any ideas?

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,175

    Post

    Post some of your code. I use that API under NT and it works fine for me.

    ------------------
    Marty

  3. #3
    Hyperactive Member
    Join Date
    Jul 1999
    Location
    NY, USA
    Posts
    270

    Post

    Make sure that it is opening THAT file and that it is not in use by any other application.

    ------------------
    Tom Young, 14 Year Old
    tyoung@stny.rr.com
    ICQ: 15743470 Add Me ICQ Me
    AIM: TomY10
    PERL, JavaScript and VB Programmer

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 1999
    Location
    Sydney, NSW, Australia
    Posts
    16

    Post

    Thanks for your interest. Here is the declare statement:

    Declare Function GetWindowsDirectory Lib "kernel32" _
    Alias "GetWindowsDirectoryA" ( _
    ByVal lpBuffer As String, _
    ByVal nSize As Long) _
    As Long

    And the following function returns the directory:

    Private Function FindWindows()
    Dim BigString As String * 255
    x = GetWindowsDirectory(BigString, Len(BigString))
    FindWindows = Left$(BigString, InStr(1, BigString, Chr$(0)) - 1)
    End Function

    The function is returning what I am expecting. That is "C:\WINNT" and I append the string "\hotline.inf". Using watch, the full path is what I am expecting, and the file does exist, but the following open command is failing.

    WinPath=FindWindows
    InfFile = WinPath & "\hotline.inf"
    On Error GoTo noInf
    Open InfFile For Input As #1
    On Error GoTo 0

    Regards,

    Alan Todd

  5. #5
    Hyperactive Member
    Join Date
    Jul 1999
    Location
    NY, USA
    Posts
    270

    Post

    I took your code and modified it a little, and works on my machine. Give it a try:

    Module Code:
    Code:
    Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
    
    Public Function FindWindows() As String
        Dim BigString As String * 255
        Call GetWindowsDirectory(BigString, Len(BigString))
        FindWindows = Left$(BigString, InStr(1, BigString, Chr$(0)) - 1)
    End Function
    
    Public Function DoesExist(ByVal FILENAME As String) As Boolean
        If Dir(FILENAME, vbNormal) <> "" Then DoesExist = True Else DoesExist = False
    End Function
    Form Code:
    Code:
    Option Explicit
    Private Const FILENAME = "\hotline.inf"
    Dim WinPath, InfFile As String
    
    Private Sub Command1_Click()
    WinPath = FindWindows
    InfFile = WinPath & FILENAME
    If DoesExist(InfFile) Then
        Open InfFile For Input As #1
        'Do InfFile Stuff Here
        Close #1
    Else
        MsgBox "File: " & InfFile & " does not exist.", vbCritical, "FILE ERROR"
    End If
    End Sub
    Like I said, it works on my machine, but give it a try.

    ------------------
    Tom Young, 14 Year Old
    tyoung@stny.rr.com
    ICQ: 15743470 Add Me ICQ Me
    AIM: TomY10
    PERL, JavaScript and VB Programmer

  6. #6
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892

    Post

    More formal (and two milliseconds faster) code:

    Replace this line:
    If Dir(FILENAME, vbNormal) <> "" Then DoesExist = True Else DoesExist = False
    With this line:
    DoesExist = (Len(Dir(FILENAME, vbNormal)) <> 0)

    ------------------
    Yonatan
    Teenage Programmer
    E-Mail: RZvika@netvision.net.il
    ICQ: 19552879
    AIM: RYoni69

  7. #7
    Hyperactive Member
    Join Date
    Jun 1999
    Posts
    308

    Post

    Hi, there. I am not so expirienced, so may be I am wrong. In a book I've read, that Windows 95 strings (for API) have a null terminator appended, but NT 4.0 - not. So there were example code:Public Function ConvertString(tmpVal As String, KeyValSize _ As Long) As String
    If (Asc(Mid(tmpVal, KeyValSize, 1)) = 0) Then
    'Win95 Null Terminated String, get rid of terminator
    ConvertString = Left(tmpVal, KeyValSize - 1)
    Else
    'WinNT Does NOT Null Terminate String
    ConvertString = Left(tmpVal, _ KeyValSize)
    'Null Not Found, Return String Only
    End If

    End Function

    But it was chapter about programming the Registry in VB. I am not sure if it applies to all API function. I hope some guru can put light on it.

    Larisa

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width