Solved

Using API function in VBScript

Posted on 2002-05-16
17
1,441 Views
Last Modified: 2012-08-13
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
Comment
Question by:dds110
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 2
  • +1
17 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7014253
:D, just a question, why do you need to do in vbscript?
0
 
LVL 8

Author Comment

by:dds110
ID: 7014273
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 7014386
You must "wrap" the API functions inside a COM dll that you could then utilize from your script.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Author Comment

by:dds110
ID: 7014406
Uuummmm...How?
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 7014427
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
 
LVL 8

Author Comment

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

Thanks

0
 
LVL 28

Expert Comment

by:AzraSound
ID: 7014535
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
 
LVL 8

Author Comment

by:dds110
ID: 7014648
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 7014690
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
 
LVL 1

Expert Comment

by:Moondancer
ID: 7016994
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
 
LVL 8

Author Comment

by:dds110
ID: 7019120
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 7019883
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
 
LVL 8

Author Comment

by:dds110
ID: 7020491
O.K.  Registered it.  Same problem.  Can't create object!

???
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 75 total points
ID: 7021495
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
 
LVL 8

Author Comment

by:dds110
ID: 7029845
I'm not ignoring you. I'll have to get back to you next week.

DS
0
 
LVL 8

Author Comment

by:dds110
ID: 7037847
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
 
LVL 1

Expert Comment

by:Moondancer
ID: 7039262
Thank you for returning and finalizing this.
:) Moondancer - EE Moderator
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month3 days, 17 hours left to enroll

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question