Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a COM object for user32 1

Status
Not open for further replies.

xwb

Programmer
Jul 11, 2002
6,828
GB
I'd like to do something like the following bit of VB without having to write a dll
Code:
Private Declare Function GetSystemMetrics Lib "user32" ( _
      ByVal nIndex As Long _
   ) As Long
Dim x
x = GetSystemMetrics(80)
WScript.Echo "There are " & cstr(x) & " monitors"
It is probably something like
Code:
set objUser32 = CreateObject ([COLOR=red]something[/color])
x = objUser32.GetSystemMetrics (80)
WScript.Echo "There are " & cstr(x) & " monitors"
Only problem is I don't know what something is. Any ideas?
 
This is how to do it via the hack/pass-through vba.
[tt]
dim scode, oxl,owb,omod,nIdx,x

scode="Declare Function GetSystemMetrics Lib ""user32"" (ByVal nIndex As Long) As Long" & vbcrlf

nIdx=clng(80)
scode=scode & "function get_systemmetrics () As Long" & vbcrlf
scode=scode & "get_systemmetrics=GetSystemMetrics(" & nIdx & ")" & vbcrlf
scode=scode & "end function"

set oxl=createobject("excel.application")
set owb=oxl.workbooks.add
set omod=owb.vbproject.vbcomponents.add(1)
omod.codemodule.addfromstring scode
x=oxl.run("get_systemmetrics")
set omod=nothing
owb.saved=true
owb.close
set owb=nothing
oxl.quit
set oxl=nothing
wscript.echo "There is(are) " & cstr(x) & " monitor(s)"
[/tt]
In one of the previous threads, I'd some complementary notes which you might find it useful as well.
 
That's absolutely brilliant. Thanks
 
User32 is a DLL, just not one VBScript can use directly.

Be careful using the trick above in an unattended script. This is only reliable for a script run by a currently logged on user.

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when run in this environment.

- Microsoft's bold, not mine!

Considerations for server-side Automation of Office


The normal solution would be to write an ActiveX DLL wrapping the User32.dll call. This carries its own baggage, of course. Too bad Reg-Free COM doesn't work in WSH.

 
I wouldn't use this technique for unattended automation. More for trying to work out stuff when there is no C++ or VB compiler available (for instance, at the in-laws). Everyone somehow seems to have a version of office. I've tried it across the range: it works from Office 2000 through to 2003 which is fine for me.

I actually use quite a lot of unattended remote automated office stuff at work for testing our GUI/C++/CORBA/SNMP interfaces between servers and clients. It has been OK so far but you never know. It is like having a car. It can be running OK for years and then suddenly it starts going wrong.
 
Just a note for the developing stage of the vba pass-through:
Do not spare to wrap the central calling (running) line with "on error resume next / on error goto 0". The essential is to make sure both owb.close, oxl.quit get a chance to be executed, otherwise you may have orphan oxl piling up during the debugging phase.
After that, one can spare them or keep them there. In this case, I have a bias preferring keeping them.
 
In case you've missed it, there's another nice script host out there for VBScript. Not free, but there's a free demo version.

NSBasic/Desktop

The plus here? Well GUI support without the HTML nightmare of HTAs. Comes with SQLite3 COM if you're a SQLite fan and want to script it. "Compiles to" (wraps your script in) EXEs - fully obfuscated.

Oh, I forgot: it can call standard DLLs like the system APIs.

Tech Note 26: DECLARE: Using API Functions

All very VB-compatible in terms of programming and use, but using VBScript instead of VB itself.


Oops, disclaimer:

I don't work for them or sell NSBasic.
 
NBasic and AutoIt are good alternatives to VBScript but they have to be downloaded and installed before use and that isn't always feasible.

I have my own DLLs to do similar COM type things but they need to be put in the registry and that isn't always feasible either.

If you're not logged in as administrator, you can't normally install stuff or play with the registry.
 
That's one of NSBasic's attractions. The basic runtime piece gets bundled into the EXE it produces. As for components you might use, I believe it will work with registration-free COM in XP SP2 and later but I'm still experimenting with this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top