发生声名错误
如下声明:
Public Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
但是我在运行时VB总提示错误!请高手指正错误!
还有如何使用!
问题点数:20、回复次数:5Top
1 楼zyx29(Taburiss)回复于 2001-01-25 12:11:00 得分 10
GetTempFileName Function
Declare Function GetTempFileName Lib "kernel32.dll" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
Platforms: Win 32s, Win 95/98, Win NT
GetTempFileName generates a filename for a temporary file and optionally creates it for you. Temporary files are used to store data for short periods of time on the hard drive. The full filename, including the path, is put into the string variable passed as lpTempFileName. The format of the generated filename is path\xxxuuuu.TMP. path is the path to put the file in, preferably the temporary directory supplied by Windows, which can be gotten through GetTempPath. xxx is the string specified in lpPrefixString. uuuu is a hexadecimal number between 0000 and FFFF. If wUnique is non-zero, uuuu is the rightmost four digits of the number (in hexadecimal), and the file is not created. This method will work even if a file with that name already exists. If wUnique is zero, uuuu is a random number generated by Windows, and the file is created. Temporary files always have a .TMP extension. The function returns the value used for uuuu if successful, or 0 if an error occured.
lpszPath
The path to put the temporary file into. This is preferably the same path gotten from GetTempPath.
lpPrefixString
The first three characters of the filename.
wUnique
If non-zero, this number's last four digits in hexidecimal are the last four characters in the filename, and the file is not created. If zero, the last four characters are generated by Windows, and the file is created.
lpTempFileName
A string that receives the path and filename of the temporary file.
Example:
' Generate a temporary file (path)\api????.TMP, where (path)
' is Windows's temporary file directory and ???? is a randomly assigned unique value.
' Then display the name of the created file on the screen.
Dim temppath As String ' receives name of temporary file path
Dim tempfile As String ' receives name of temporary file
Dim slength As Long ' receives length of string returned for the path
Dim lastfour As Long ' receives hex value of the randomly assigned ????
' Get Windows's temporary file path
temppath = Space(255) ' initialize the buffer to receive the path
slength = GetTempPath(255, temppath) ' read the path name
temppath = Left(temppath, slength) ' extract data from the variable
' Get a uniquely assigned random file
tempfile = Space(255) ' initialize buffer to receive the filename
lastfour = GetTempFileName(temppath, "api", 0, tempfile) ' get a unique temporary file name
' (Note that the file is also created for you in this case.)
tempfile = Left(tempfile, InStr(tempfile, vbNullChar) - 1) ' extract data from the variable
Debug.Print "Temporary filename: "; tempfile
Top
2 楼haor(一个好人)回复于 2001-01-25 13:55:00 得分 0
错误提示是否为:“常数、固定长度字符串、数组、自定义类型与 Declare 语句不能是对象模块中的 Public 成员”?
请在Module中声明。Top
3 楼haor(一个好人)回复于 2001-01-25 13:55:00 得分 10
使用方法:
GetTempFileName
The GetTempFileName function creates a name for a temporary file. The filename is the concatenation of specified path and prefix strings, a hexadecimal string formed from a specified integer, and the .TMP extension.
The specified integer can be nonzero, in which case, the function creates the filename but does not create the file. If you specify zero for the integer, the function creates a unique filename and creates the file in the specified directory.
UINT GetTempFileName(
LPCTSTR lpPathName, // pointer to directory name for temporary
// file
LPCTSTR lpPrefixString, // pointer to filename prefix
UINT uUnique, // number used to create temporary filename
LPTSTR lpTempFileName
// pointer to buffer that receives the new
// filename
);
Parameters
lpPathName
Pointer to a null-terminated string that specifies the directory path for the filename. This string must consist of characters in the ANSI character set. Applications typically specify a period (.) or the result of the GetTempPath function for this parameter. If this parameter is NULL, the function fails.
lpPrefixString
Pointer to a null-terminated prefix string. The function uses the first three characters of this string as the prefix of the filename. This string must consist of characters in the ANSI character set.
uUnique
Specifies an unsigned integer that the function converts to a hexadecimal string for use in creating the temporary filename.
If uUnique is nonzero, the function appends the hexadecimal string to lpPrefixString to form the temporary filename. In this case, the function does not create the specified file, and does not test whether the filename is unique.
If uUnique is zero, the function uses a hexadecimal string derived from the current system time. In this case, the function uses different values until it finds a unique filename, and then it creates the file in the lpPathName directory.
lpTempFileName
Pointer to the buffer that receives the temporary filename. This null-terminated string consists of characters in the ANSI character set. This buffer should be at least the length, in bytes, specified by MAX_PATH to accommodate the path.
Return Values
If the function succeeds, the return value specifies the unique numeric value used in the temporary filename. If the uUnique parameter is nonzero, the return value specifies that same number.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
The GetTempFileName function creates a temporary filename of the following form:
path\preuuuu.TMP
The following table describes the filename syntax:
Component Meaning
path Path specified by the lpPathName parameter
pre First three letters of the lpPrefixString string
uuuu Hexadecimal value of uUnique
When the system shuts down, temporary files whose names have been created by this function are not automatically deleted.
To avoid problems resulting when converting an ANSI string, an application should call the CreateFile function to create a temporary file.
If the uUnique parameter is zero, GetTempFileName attempts to form a unique number based on the current system time. If a file with the resulting filename exists, the number is increased by one and the test for existence is repeated. Testing continues until a unique filename is found. GetTempFileName then creates a file by that name and closes it. When uUnique is nonzero, no attempt is made to create and open the file.
Top
4 楼blademan(不累得慢)回复于 2001-01-25 18:32:00 得分 0
你应该把Public声明写在模块中。
选择“工程”->“添加模块”即可。Top
5 楼lujianjian(老农)回复于 2001-01-25 20:09:00 得分 0
haor(一个好人) 写得挺清楚的吧,你是不是把它写在Form里了?Top




