how to use windows api under xojo

Below there’s a code for VB6 for using a Windows API.
How do I use it under Xojo ? I already checked the documentation but there are no examples.

Especially the “private declare function” line where does it go exactly ?
Thanks


Private Declare Function GetVolumeInformation Lib "kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As Long

Private Const BUFFER As Long = 256

_____________________________________________________________________________________

Private Function GetSerial() As String

    Dim strVolumeName       As String * 256
    Dim strFileSystem       As String * 256
    Dim lngSerialNumber     As Long
    Dim lngReturn           As Long
    
    Dim drivename As String
    drivename = Left(App.Path, 3)
    lngReturn = GetVolumeInformation(drivename, strVolumeName, BUFFER, lngSerialNumber, 0&, 0&, strFileSystem, BUFFER)
                       
    If lngReturn Then

        GetSerial = Hex(Trim(lngSerialNumber))

    Else

       GetSerial = "NO INFO"

    End If

End Function

Searching for that api call gets you another recent thread that answers your question and gives you the source you need.

https://forum.xojo.com/7484-anybody-got-plugin-less-code-to-get-windows-hd-serial/0#p52378

thanks the last reply on that thread works perfectly !

[quote=77891:@Bob Coleman]Searching for that api call gets you another recent thread that answers your question and gives you the source you need.

https://forum.xojo.com/7484-anybody-got-plugin-less-code-to-get-windows-hd-serial/0#p52378[/quote]

This example is better: https://forum.xojo.com/10826-getting-volume-serial-number

I Noticed a bug in the other link will correct it

  1. I had to remove the “exception err as oleexception” because it causes a syntax error.

  2. The example is a SUB, but where and how do you use SUBs on Xojo ? I used a Method. In fact I also had to remove the “end sub” line

When you copy a method in the left of the IDE and paste that into text, you get Sub [ Name of the method ] and end sub at the bottom.

If your application will need to be cross-platform, here is a Mac & Windows function for retrieving the Serial Number

Function SystemSerialNumber() As String
  dim sh as New Shell
  Dim y as String
  
  #If TargetWin32 Then
    sh.TimeOut = 3000
    sh.execute("wmic bios get serialnumber")
    return trim(NthField( sh.Result, "SerialNumber", 2 ))
  #ElseIf TargetMacOS Then
    sh.Execute("ioreg -l | grep IOPlatformSerialNumber|awk '{print $4}'")
    y = sh.result
    y = replace(y,"<"+chr(34),"")
    y = replace(y,chr(34) + ">","")
    return y
  #EndIf
  
  If sh.ErrorCode = 0 then return (sh.Result)
End Function

@Matthew Combatti is this the same as the manifacturers serial? And can this be changed (probably it is by an expericenced user, but I suspect not by a novice one).

[quote=77989:@Matthew Combatti]If your application will need to be cross-platform, here is a Mac & Windows function for retrieving the Serial Number

[code]
Function SystemSerialNumber() As String
dim sh as New Shell
Dim y as String

#If TargetWin32 Then
sh.TimeOut = 3000
sh.execute(“wmic bios get serialnumber”)
return trim(NthField( sh.Result, “SerialNumber”, 2 ))
#ElseIf TargetMacOS Then
sh.Execute(“ioreg -l | grep IOPlatformSerialNumber|awk ‘{print $4}’”)
y = sh.result
y = replace(y,"<"+chr(34),"")
y = replace(y,chr(34) + “>”,"")
return y
#EndIf

If sh.ErrorCode = 0 then return (sh.Result)
End Function

[/code][/quote]

WINDOWS:
Instead of executing vmic through a shell, you can also uses WMI classes with OLEObject.

See link: https://forum.xojo.com/4447-shell-issue/p1#p30972

Strange I don’t have any problems with that. tested on RS2012R2.1 And Xojo 2014r1

In Xojo:

  1. Add to Window1 an Untitled Method
  2. Copy all the code. Including 1st line Sub and last line End Sub
  3. Paste code into the Untitled Method Code Editor.

A new method is created with Method name: ShowDriveType

Thanks very appreciated both functions work, BUT they return two different results…
the first one returns a long numeric, the second one returns an alfanumeric shorter string.
How is that ?

DISREGARD my last msg (I’m trying to delete it but cant figure out how on this forums)

No need to delete them as it’s part of the conversation
If you find you did something that changed your expectations or results noting what that was in the thread may be useful to someone else later