In the VB programming environment, if you want to call the API, you will need to declare, otherwise we need to call the API in the system library to get the computer's name, let us open the API documentation. Review the information about getComputerName is as follows: Function prototype: BOOL GetComputerName (LPTSTR lpBuffer, // address of name buffer LPDWORD nSize // address of size of name buffer); parameters: lpBuffer: Points to a buffer to receive the null-terminated character string containing the computer name.
nSize: Points to a variable that specifies the maximum size, in characters, of the buffer This value should be large enough to contain MAX_COMPUTERNAME_LENGTH 1 characters Return Value:.. If the function succeeds, the return value is nonzero and the variable represented by the nSize parameter contains the number of characters copied to the destination buffer, not including the terminating null character. If the function fails, the return value is zero. to get extended error information, call GetLastError. click on the back of the document Quick Info button to see Related Information: I know that the function is required to be in the operating system of Win32S or later. And the function exists in kernel32.lib (C library file name, which can be seen in kernel32.dll). After the above information, open the API Viewer search getComputerName, check it to the program later as follows: private declare function getcomputername lib "kernel32" Alias "getComputernamea" (Byval Lpbuffer AS String, nsize as long) As long in VB Newly built a project and place Command a string string string str = String (255, chr $ (0)) 'to save the computer name str = string (255, chr $ (0))' to use this string 255 air fills getcomputername STR, 255 'Call the API function, or Call getComputername (STR, 255) STR = Left $ (Str, INSTR (1, Str, Chr $ (0))))' Intercept the Non-empty part of the return string Msgbox Strend Sub Compilation The program appears in the pop-up dialog box;