PDA

Click to See Complete Forum and Search --> : GetWindowsDirectory API


AlanTodd
Nov 18th, 1999, 06:40 AM
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?

MartinLiss
Nov 18th, 1999, 06:48 AM
Post some of your code. I use that API under NT and it works fine for me.

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

Compwiz
Nov 18th, 1999, 07:07 AM
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 (http://wwp.icq.com/15743470) Add Me (http://wwp.icq.com/scripts/search.dll?to=15743470) ICQ Me (http://wwp.icq.com/scripts/contact.dll?msgto=15743470)
AIM: TomY10 (http://www.aol.com/aim/aim30.html)
PERL, JavaScript and VB Programmer

AlanTodd
Nov 18th, 1999, 08:33 AM
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

Compwiz
Nov 18th, 1999, 08:56 AM
I took your code and modified it a little, and works on my machine. Give it a try:

Module 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:
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 (http://wwp.icq.com/15743470) Add Me (http://wwp.icq.com/scripts/search.dll?to=15743470) ICQ Me (http://wwp.icq.com/scripts/contact.dll?msgto=15743470)
AIM: TomY10 (http://www.aol.com/aim/aim30.html)
PERL, JavaScript and VB Programmer

Yonatan
Nov 18th, 1999, 07:31 PM
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 (http://www.icq.com/19552879)
AIM: RYoni69

LG
Nov 19th, 1999, 09:37 PM
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