Link to home
Start Free TrialLog in
Avatar of dds110
dds110

asked on

Using API function in VBScript

Here is the api call:

Private Declare Function StrFormatByteSize Lib "shlwapi" Alias "StrFormatByteSizeA" (ByVal _
    dw As Long, ByVal pszBuf As String, ByRef cchBuf As Long) As String

Here is the function:

Public Function FormatKB(ByVal Amount As Long) As String
'This function returns the size of the database in friendly readable format.
    Dim Buffer As String
    Dim Result As String
    Buffer = Space$(255)
    Result = StrFormatByteSize(Amount, Buffer, _
        Len(Buffer))
    If InStr(Result, vbNullChar) > 1 Then
        FormatKB = Left$(Result, InStr(Result, _
            vbNullChar) - 1)
    End If
   
End Function

How can I use this in a .vbs file?

Thanks
DDS
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

:D, just a question, why do you need to do in vbscript?
Avatar of dds110
dds110

ASKER

Because my company is too "political" to buy any good software.  I have at my disposal:  MSOffice (which I don't want to use), vbscript, Dos.  Our corporate office has good software that I could do this with, but we are separated by a few 1000 miles.  I want this script to run on startup and check the sizes of several "growing" databases and alert me when they reach a certain size (between x and xx).

DDS
You must "wrap" the API functions inside a COM dll that you could then utilize from your script.
Avatar of dds110

ASKER

Uuummmm...How?
You will need to use a tool like Visual Basic.  If you dont have it, borrow it from someone.  All you need to do is start a new ActiveX DLL project.  For example, say you name the project VBScript_API and the class module within it, APIFunctions.  You would paste the above code into that class, and then do something like this in your script:


Dim objAPI

Set objAPI = CreateObject("VBScript_API.APIFunctions")
strResult = objAPI.FormatKB(lngAmount)
Avatar of dds110

ASKER

I'll have to try this at home (where I have decent software) and award/ not award points to ya later.

Thanks

Or, you can just wrap the API calls so that it is all the COM object contains, e.g.,


'list private declares at top of class module
Private Declare Function StrFormatByteSize Lib "shlwapi" Alias "StrFormatByteSizeA" (ByVal dw As Long, ByVal pszBuf As String, ByRef cchBuf As Long) As String


'wrap private declared functions
Public Function StrFormatByteSize_API(BYVal dw As Long, ByVal pszBuf As String, ByRef cchBuf As Long) As String
    StrFormatByteSize_API = StrFormatByteSize(dw, pszbuf, cchbuf)
End Function



Then use the above function directly in your vbscript, except substitute the API call with your object's function call.



Public Function FormatKB(ByVal Amount)
'This function returns the size of the database in friendly readable format.
   Dim Buffer
   Dim Result
   Dim objAPI

   Set objAPI = CreateObject("VBScript_API.APIFunctions")
   Buffer = Space(255)
   Result = objAPI.StrFormatByteSize_API(Amount, Buffer, _
       Len(Buffer))
   If InStr(Result, vbNullChar) > 1 Then
       FormatKB = Left(Result, InStr(Result, _
           vbNullChar) - 1)
   End If
 
End Function
Avatar of dds110

ASKER

AHA!  I found the formatnumber function which basically does the same thing as the before mentioned api call!

Here it is:

sz=formatnumber((sz/1024)/1024,2,0,0,-1) & " MB"

I'll have to do some finagling to get it to recognize bytes, kbytes, and mbytes but I got it.

Don't fret AzraSound, I'm still going to give your suggestion a once over.

DDS
Sorry, I didn't even pay attention to what your function was doing...I was just concentrating on answering the question "HOw do you use API functions in VBScript".  If that works for you, it is DEFINITELY the way to go.  If you need to use other API functions in the future, consider the method I outlined above.
This question is current, others below are not and need your attention.  ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101, Netminder or Mindphaser will return to finalize these if they are still open in 7 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
https://www.experts-exchange.com/questions/Q.20245060.html
https://www.experts-exchange.com/questions/Q.20243479.html
https://www.experts-exchange.com/questions/Q.20252967.html
https://www.experts-exchange.com/questions/Q.20275017.html
https://www.experts-exchange.com/questions/Q.20299257.html
https://www.experts-exchange.com/questions/Q.20301364.html



*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed in the link below
https://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thanks everyone.
Moondancer
Moderator @ Experts Exchange
Avatar of dds110

ASKER

O.K.  I tried your suggestion and I wind up with an error that says vbs can't create the object.  What could I be doing wrong.  Here is my code:

[code]
Dim objAPI
Set objAPI=CreateObject("FormatKb.MyFunc")
strResult=objAPI.FormatKb(786432000)

msgbox strResult
[/code]

I created a new ActiveX Dll project and pasted the code AS IS into it, then compiled it into FormatKB.dll.  I then placed the dll in my system folder.

What next?
If you did not compile the dll on the machine that is using it, you need to register it.  You can do this via the command line by typing in:


regsvr32 [path to dll]
Avatar of dds110

ASKER

O.K.  Registered it.  Same problem.  Can't create object!

???
ASKER CERTIFIED SOLUTION
Avatar of AzraSound
AzraSound
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dds110

ASKER

I'm not ignoring you. I'll have to get back to you next week.

DS
Avatar of dds110

ASKER

AzraSound,

You have been patient enough.  I still cannot get your suggestion to work but I will keep trying.  I will have to read up more on ActiveX dll's.  

I'll let you know as soon as I get it.

Thanks

DDS
Thank you for returning and finalizing this.
:) Moondancer - EE Moderator