Some Useful Windows Calls
I know this information is available in other places on the internet, but I thought it may be appropriate to have it here as well.
I have a few in a library file that I have created. I have gathered these from across the internet and a few other places. I have also uploaded a document that contains the Windows system error codes from Microsofts Platform SDK.
Unfortunatley, I haven't figured out how to upload files to theses pages, but I have uploaded the files here
(You could do it like this: Copy the text to the clipboard with Ctrl-A, then create a new page like WindowsLib, and then just dump it in between <pre> tags.)
MapBasic Example
*******************************************************************************************INCLUDE STATEMENTS include "mapbasic.def" include "menu.def" include "icons.def" include "windows.lib"
*******************************************************************************************TYPE STATEMENTS'******************************************************************************************* 'GLOBAL VARIABLES
*******************************************************************************************DECLARE SUB STATEMENTS DECLARE SUB MAIN
*******************************************************************************************DECLARE FUNCTIONS STATEMENTS
*******************************************************************************************MAIN - Main Subroutine SUB Main dim i, buf, i2, i3, i4,hndl, J, p as integer dim s, s1, s2, CNames(), name as string dim b as Logical dim fileinfo As WIN32_FIND_DATA dim curtime as systemtime dim os as OSVERSIONINFO dim comp as Integer'Clear Screen CLS
'Get Computer Name print "* GetComputerName *" buf = 254 s = string$(buf, dNULL) i = GetComputerName(s,buf) s = rtrim$(s) print "Computer NetBIOS Name is " & s
'Get Computer Name Extended print "" print "* GetComputerNameEx *" buf = 254 s = string$(buf, dNULL) comp = ComputerNameDNSDomain i = GetComputerNameEx(comp,s,buf) s = rtrim$(s) print "Computer DNS Domain Name is " & s comp = ComputerNameNetBIOS s = string$(buf, dNULL) i = GetComputerNameEx(comp,s,buf) s = rtrim$(s) print "Computer NetBIOS Name is " & s comp = ComputerNameDnsHostname s = string$(buf, dNULL) i = GetComputerNameEx(comp,s,buf) s = rtrim$(s) print "Computer DNS Host Name is " & s comp = ComputerNameDnsFullyQualified s = string$(buf, dNULL) i = GetComputerNameEx(comp,s,buf) s = rtrim$(s) print "Computer Fully Qualified DNS Name is " & s comp = ComputerNamePhysicalNetBIOS s = string$(buf, dNULL) i = GetComputerNameEx(comp,s,buf) s = rtrim$(s) print "Computer Physical NetBIOS Name is " & s comp = ComputerNamePhysicalDnsHostName s = string$(buf, dNULL) i = GetComputerNameEx(comp,s,buf) s = rtrim$(s) print "Computer Physical DNS Host Name is " & s comp = ComputerNamePhysicalDnsDomain s = string$(buf, dNULL) i = GetComputerNameEx(comp,s,buf) s = rtrim$(s) print "Computer Physical DNS Domain Name is " & s comp = ComputerNamePhysicalDnsFullyQualified s = string$(buf, dNULL) i = GetComputerNameEx(comp,s,buf) s = rtrim$(s) print "Computer Physical Fully Qualified DNS Name is " & s
'Get User Name print "" print "* GetUserName *" buf = 254 s = string$(buf, dNULL) i = GetUserName(s,buf) s = rtrim$(s) print "Computer User Name is " & s
'Get Computer Free Diskspace print "" print "* GetFreeSpace " buf = 254 s = "C:\" buf = GetFreeSpace(s,i,i2,i3,i4) s = rtrim$(s) print "Drive Space for " & s print "Sectors Per Cluster is " & i print "Bytes Per Sector is " & i2 print "Clusters Available is " & i3 print "Total clusters is " & i4 print "Available Space is " & ((i2/1048576) i i3) & " MBytes" print "Total Space is " & ((i2/1048576) i * i4) & " MBytes"
'Sleep 'Note "Waiting to Run Sleep" print "" print "* Sleep *" i = 0 Print " - Sleeping - " buf = 5000 i3 = 0 call sleep(buf) Print " - Awake - "
'Create a Directory Print "" Print "* CreateDirectory *" buf = 0 s = "C:\KERNEL32" i = CreateDirectory(s, buf) if i <> 0 then Print "Directory " & s & " created." else i2 = GetLastError() if i2 = 183 then 'Directory Alread Exists note "Directory " & s & " already exists." else note "Error = " & i2 end if end if
Copy a File Print "" Print "*** CopyFile ***" s1 = "C:\TEMP\KERNEL.TXT" s2 = "C:\KERNEL32\KERNEL.TXT"Set bFailIfExists to 0 for overwrite existing file and 1 for no overwrite i = CopyFile(s1,s2,0) if i <> 0 then print "File " & s1 & " copied to " & s2 else i2 = getlasterror() if i2 = 80 then note "File " & s2 & " already exists." elseif i2 = 3 then note "Path not found." elseif i2 = 123 then note "File does not exists (could also be Path or Volume)." else note i2 end if end if'wait 'Note "Check that file was created, and then click 'OK'"
File Directory Functions print "" print "*** FindFirstFile, FindNextFile, FindClose ***" name = ApplicationDirectory$() & "*.*" StatusBar Message "Searching " + name hndl = FindFirstFile(name, fileinfo) If hndl <> INVALID_HANDLE_VALUE Then Do If (fileinfo.dwFileAttributes MOD (FILE_ATTRIBUTE_DIRECTORY * 2)) \ FILE_ATTRIBUTE_DIRECTORY <> 1 Then I = ubound(CNames) + 1 Redim CNames(I) CNames(I) = pathtodirectory$(name) + fileinfo.cFileName print cnames(i) end if Loop While FindNextFile(hndl, fileinfo) = TRUE end IfThe call to FindClose() is required to end the search I = FindClose(hndl)'Delete a File Print "" Print "* DeleteFile *" i = DeleteFile(s2) if i <> 0 then print "File " & s2 & " deleted." else i2 = getlasterror() if i2 = 80 then note "File " & s2 & " already exists." elseif i2 = 3 then note "Path not found." elseif i2 = 123 then note "File does not exists (could also be Path or Volume)." else note i2 end if end if
'Remove a Directory Print "" Print "* RemoveDirectory *" s = "C:\KERNEL32" i = RemoveDirectory(s) if i <> 0 then Print "Directory " & s & " removed." else i2 = GetLastError() if i2 = 145 then 'Directory Alread Exists note "Directory " & s & " is not empty." else note "Error = " & i2 end if end if
'Get a Setting from the INI File print "" print "* GetINISetting *" buf = 254 s1 = ApplicationDirectory$() & "ExternalRef.INI" s = string$(buf, dnull) if fileexists(s1) then i = GetINISetting("WINDOWS_LIB", "RUN_VALUE", "0", s, buf, s1) print "This application has been previously run " & rtrim$(s) & " times." i2 = val(s) end if
'Write a Setting from the INI File print "" print "* WriteINISetting *" i2 = i2 + 1 s = str$(i2) s1 = ApplicationDirectory$() & "ExternalRef.INI" i = WriteINISetting("WINDOWS_LIB", "RUN_VALUE", s, s1) print "This application INI file has been updated."
'Get System Time print "" print "* GetSystemTime *" call getsystemtime(curtime) Print "Year is " & curtime.wYear Print "Month is " & curtime.wMonth Print "Day of the Week is " & curtime.wDayOfWeek Print "Day is " & curtime.wDay Print "Hour is " & curtime.wHour Print "Minute is " & curtime.wMinute Print "Second is " & curtime.wSecond Print "Millisecond is " & curtime.wMilliSeconds
'Get OS Version print "" print "* GetVersionEx *" os.dwOSVersionInfoSize = 148
calculated size of the OSVERSIONINFO type i = GetVersionEx(os)read Windows's version information print "Windows version number: " & os.dwMajorVersion & "." & os.dwMinorVersion & chr$(10) & os.szCSDVersion'Lock Workstation Print "" print "* Lock Workstation *" if ask("Lock WorkStation", "Yes", "No") then i = lockworkstation() end if
END SUB
| subtopics: |