Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1697
  • Last Modified:

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
0
dds110
Asked:
dds110
  • 8
  • 6
  • 2
  • +1
1 Solution
 
Richie_SimonettiCommented:
:D, just a question, why do you need to do in vbscript?
0
 
dds110Author Commented:
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
0
 
AzraSoundCommented:
You must "wrap" the API functions inside a COM dll that you could then utilize from your script.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
dds110Author Commented:
Uuummmm...How?
0
 
AzraSoundCommented:
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)
0
 
dds110Author Commented:
I'll have to try this at home (where I have decent software) and award/ not award points to ya later.

Thanks

0
 
AzraSoundCommented:
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
0
 
dds110Author Commented:
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
0
 
AzraSoundCommented:
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.
0
 
MoondancerCommented:
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.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://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.
http://www.experts-exchange.com/questions/Q.20245060.html
http://www.experts-exchange.com/questions/Q.20243479.html
http://www.experts-exchange.com/questions/Q.20252967.html
http://www.experts-exchange.com/questions/Q.20275017.html
http://www.experts-exchange.com/questions/Q.20299257.html
http://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
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed in the link below
http://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
0
 
dds110Author Commented:
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?
0
 
AzraSoundCommented:
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]
0
 
dds110Author Commented:
O.K.  Registered it.  Same problem.  Can't create object!

???
0
 
AzraSoundCommented:
DO you have the ProgID correct?  e.g., The dll name is FormatKB and the Class name is MyFunc??

If you run this same code from a VB program (still using CreateObject function), do you get the same error?
0
 
dds110Author Commented:
I'm not ignoring you. I'll have to get back to you next week.

DS
0
 
dds110Author Commented:
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
0
 
MoondancerCommented:
Thank you for returning and finalizing this.
:) Moondancer - EE Moderator
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now