Does the CreateObject method follow any specific syntax? - vba

For instance, at first I thought that what went in the parentheses was the library and class separated by a period (Ex: "Scripting.Dictionary", "Scripting.FileSystemObject". However, when you use it for internet explorer it is "InternetExplorer.Application" instead of "SHDocVw.InternetExplorer". Is the syntax not standard and something you just need to look up every time? I will use early binding anyways but I'm just curious.

The string used as a parameter to CreateObject() is what's known as a programmatic identifier or "ProgId". COM classes identify themselves using a CLSID, or what is essentially a GUID. But rather than having to remember and type the CLSID for the FileSystemObject COM class whenever you need to instantiate the COM object (which is {0D43FE01-F093-11CF-8940-00A0C9054228}, by the way), COM allows for the specification of a ProgId string.
You can examine the contents of the HKEY_CLASSES_ROOT\CLSID registry key to see all COM classes available on your PC. Here's the CLSID for the FileSystemObject class, showing the ProgID:
Tools like OleView or NirSoft's ActiveXHelper make finding ProgIds much easier. Other languages require you to jump through hoops to create COM objects. But the VB family of languages is "COM aware" and provides the necessary utilities to easily create and work with COM components without having to worry about IIDs, CLSIDs, Type Libraries, and other "COM" mumbo jumbo. In VB, when you add a "Reference", you're referencing a Type Library. When you call CreateObject(<string>), you're telling Windows to look up the CLSID of the COM class with a ProgId of <string>. They're just simpler, cleaner ways to interface with the COM world.
To (try to) answer your question, a ProgId can really be anything the creator of the COM object chooses. It's typically "<COM Library>.<COM Class>" but that's not always the case. Often version numbers are included. "MSMXL2.DOMDocument.6.0", for example, includes a version number for both the library and the class. Other times, there's no library at all (CreateObject("htmlfile"), for example). So, unfortunately, there's no perfect standard here.

It depends on how you are accessing the library.
NOTE: This will work in Visual Basic or VBScript
fso = CreateObject("Scripting.FileSystemObject")
Scripting is the name of the type library, and FileSystemObject is the name of the object of which you want to create an instance.
If you have the library referenced, you don't need to go through the scripting library.
NOTE: This will only work in Visual Basic.
Dim fso As New FileSystemObject

Related

Accessing Windows Contacts (pre Win10) from JScript (or any ActiveScripting)

I want to use the COM object with the progID Windows.Contact.1 via ActiveScripting (JScript, VBScript, Python, etc).
This COM resides in C:\Program Files (x86)\Common Files\System\wab32.dll. It seems, there is no TypeLib available for it. The COM delivers, amongst others, IContact for the "Windows Address Book" (storing contacts as XML in folders, as in Windows 7). IContact is documented here.
In JScript I did:
var co = new ActiveXObject("Windows.Contact.1");
typeof co; // results in: unknown
Since it results in unknown, I have the suspicion, that this COM could not be usable for scripting. Somewhere I read, that everything, that inherits from IUnknown can not be used for scripting, instead it must inherit from IDispatch. But I am unsure, as to how much of this is valid, and whether there are workarounds.
I would like to ask for acknowledgement of my suspicions (since I am new to all of this and have no C++ or C# background) or to ask for a way, as to how to use Windows.Contact.1 from scripting, including a way, to find out, which methods/objects I can use, without resorting to a TypeLib.
I have access to pages like Programming Windows Contacts and related ones, but first I need to get an instance in ActiveScript (JScript, VBScript, Python, Lua will do). I also have access to applications like "MS OLE View" and "OLEView DotNet". Thank you.
There are entire books on the subject, but here's a very simplified story. There are basically 3 "categories" of COM interfaces:
Interfaces deriving from IUnknown
aliases for programming against: early binding, (custom) vtable binding
the simplest way to implement a COM "server"
it's only a binary contract (methods layout, method signature, parameters behavior like in/out for cross-apartment/process support, ...)
you need to somewhow tell your callers what is this binary contract you support (you can use .idl, .tlb or anything that your caller can understand)
there are some official ways of documenting your IUnknown-derived interfaces: .idl -> .h and .tlb is the most standard one
only supported by a certain class of languages (for example C/C++, .NET, Delphi), those who understand .tlb (or .idl, or equivalent .h), or those who allow redefining layout manually (like .NET). You can perfectly define a language that can do that w/o ever using .tlb. That's the beauty of COM, it's just a binary contract.
if your language doesn't support it, you just can't use it, you'll have to write or use a wrapper with a language that supports it and exposes it in a way your language supports. Powershell for example doesn't support IUnknown-derived interfaces (I'm not 100% sure) but supports .NET so it can use .NET as a "super wrapper".
IDispatch interface
only requires one IUnknown well-known interface implementation: IDispatch
aliases for programming against: late binding, OLE automation, COM automation, or simply Automation (not to confuse with UI Automation)
invented for higher level languages (VB/VBA first, ActiveScripting a bit later)
only supported by a certain class of language, and the way it's supported varies (for example it's supported in C++ of course but it's not super easy w/o wrappers or tooling like Visual Studio's C++ #import directive). JScript and VBScript don't exactly support the same set of features with regards to Automation.
you're supposed to use only a predefined list of types "Automation-Compatible types":
these types where initially very related to VB/VBA (VARIANT, SAFEARRAY, BSTR which means "Basic String"...)
from the higher level language, it really makes COM much transparent and easier as that was the whole point (and can make it harder from the lower level ones...), it also allows "syntactic sugar" niceties
note the IDispatch implementation can be very dynamic and really late-bound at runtime (get id of name -> invoke) but most available programming tooling quite freezes the list of ids/names at compile time (ex: .NET) because they support Dual interfaces.
Dual interfaces:
interfaces that implement a custom IDispatch-derived interface and implement IDispatch itself to match the custom interface (both implementations supposedly being "equivalent" of course). Have a look at the link below, it has nice images.
because of IDispatch, you're supposed to use only Automation compatible data types in the IDispatch-derived method.
it's more work to implement (so it's usually done by programming tools, for ex: ATL)
a bit easier for native (C/C++, etc.) callers (no need to use IDispatch wrappers) but you still have to digest automation data types
IMHO, one of the best 1-page introduction to COM is here: Introduction to COM

How to cast a VB.net generic type instance to a COM type?

I have a number of functions that look basically like…
Public Function Keys() As Dictionary(Of Integer, myData).KeyCollection
I'm trying to expose this class to COM so I can use it in Excel, but VS complains that…
Warning: Type library exporter encountered a generic type instance in a signature. Generic code may not be exported to COM.
I think this means I have to copy this to a type that can be exported to COM, I suspect a SAFEARRAY of strings?
Anyone have an example code of this?
You cannot expose generic types to COM, it has no support for it whatsoever. The reason that Dictionary doesn't have the [ComVisible(true)] attribute. Somewhat intuitively obvious, languages like C++, scripting languages like VBScript or Javascript, VB6 and VBA, etc, don't know beans about .NET generics. Only a .NET program can ever use it, you don't need COM interop for that.
If you want to expose a dictionary then you'll need to create your own or simply fall back to the olden Hashtable. Which is [ComVisible], your type library will automatically include mscorlib.tlb

What is an "automation-compatible language"?

I have found many references to this term on the web, especially on Microsoft's MSDN website, and even lists of such languages. However, I can't find a definition of this term. (Is it something as simple as a language for which a COM interface has been implemented?)
Please let me know if you know of a link to a definition.
Thanks.
Stripped down to bare essentials, to understand COM you have to understand GUIDs and IUnknown. The equivalent for Automation is ProgIDs and IDispatch.
A ProgID helps you create a COM coclass. A typical ProgID is "Word.Automation", the progid for Microsoft Word. You'll find them listed in the Registry under HKEY_CLASSES_ROOT. A typical name for a helper function in your language is CreateObject(). You pass it the ProgID, optionally a machine name, and you get back an interface reference. Which you can then use to make method calls and get/set properties.
The language runtime uses the IDispatch interface (retrieved with IUnknown::QueryInterface) to discover the names and parameters of the methods that are implemented by the COM server. This is called late-binding, the way any scripting language uses Automation. It has only 4 methods:
IDispatch::GetTypeInfoCount(), returns 1 if the server can provide type info
IDispatch::GetTypeInfo(), returns type information, helpful to make type-safe calls
IDispatch::GetIDsOfNames(), maps an identifier name to a number
IDispatch::Invoke(), calls a numbered method or property getter/setter.
That's all it takes.
In Microsoft Windows applications
programming, OLE Automation (later
renamed by Microsoft to just
Automation,1[2] although the old
term remained in widespread use), is
an inter-process communication
mechanism based on Component Object
Model (COM) that was intended for use
by scripting languages – originally
Visual Basic – but now are used by
languages run on Windows.[3] It
provides an infrastructure whereby
applications called automation
controllers can access and manipulate
(i.e. set properties of or call
methods on) shared automation objects
that are exported by other
applications.
From Wikipedia entry

What is COM?

I searched hard, but was unable to grasp the whole idea. Can anyone tell me:
What COM actually is?
How do GUIDs work, and how are they used by COM?
How does COM resolve the issues of different DLL versions.
Or at least, point me to a good article somewhere that explains these concepts?
Thanks!
COM is "Component Object Model". It is one of the first technologies designed to allow "binary reuse" of components... Originally, it was the rewrite of what was, in Microsoft Office circa 1988-1992 time frame, referred to as Dynamic Data Exchange (DDE), a technology designed to allow the various Office applications to talk to one another. The first attempt to rewrite it was called OLE-Automation (Object Linking and Embedding). But when they got done they renamed it to COM.
How it works:
Essentially, before COM, when a client component wanted to use a component (written as a C++ library), it had to be compiled WITH the library, so it could know exactly how many bytes into the compiled binary file to find each method or function call.
With COM, there is a defined mechanism as to how these methods will be structured, and then the compiler produces a separate file (called a type library or an Interface Definition Language (IDL) file, that contains all this function offset data.
Then, as a user of the component, you have to "register" it, which writes all this information (Keyed off of GUIDs) into the OS Registry, where any client app can access it, and by reading the data from the registry, it can know where in the binary file to find each method or class entry point.
Your question is a little large for a full explanation here. A quick high-level introduction to COM can be found in the book Understanding ActiveX and OLE. A more detailed but still introductory introduction is Inside COM. The best book on the subject is Don Box's Essential COM.
A couple of quick answers:
COM is a binary interface standard for objects. It allows various programs to write to interfaces without all having to have been written in the same langauge with the same compiler. There are also related services available.
GUIDs are globally unique numbers that COM uses to identify interfaces.
COM doesn't resolve different DLL version problems. It only allows a single DLL to be registered for each GUID.
COM enables reusable software. Like building blocks, you can create COM objects (or now Assemblies in .NET) to provide functionality to a larger piece of software. I have used COM to provide DB integration for Excel and MS BizTalk. Software like MS BizTalk use COM/Assemblies to extend the processing capabilities of a standard process; you can insert a COM into the message workflow to do more processing than is implemented by Microsoft. COM also allows use of Component Services providing built in object pooling, security, and control interface.
Wikipedia has a good definition of GUID. Note that Microsoft has a formatting that is not necessarly used in the rest of development community.
COM by itself does not resolve DLL version issues. It enables you to extend software incrementally if you use the COM versioning capability. So if you have an application that uses a COM to convert XML to Text (for example) and you want to enhance, you can create a new version (2.0) which you can roll-out slowly as you update the source application to use the new COM. This way you could (if need be) have a switch statement that can still use the old COM if required by system limitations, or use the new one (they would be different DLLs).
COM is a lot of different things. I recommend Don Box's book, Essential COM as a good way to learn.
At a bare minimum, a COM object is an object that exposes a single interface, IUnknown. This interface has 3 methods, AddRef, Release, and QueryInterface. AddRef/Release enables the object to be reference counted, and automatically deleted when the last reference is released. QueryInterface allows you to interrogate the object for other interfaces it supports.
Most COM objects are discoverable. They are registered in the registry under HKEY_CLASSES_ROOT with an identifying GUID, called a CLSID (class ID). This enables you to call CoCreateInstance to create an instance of a registered object if you know a GUID. You can also query the registry via a COM API for the CLSID that backs a ProgId (program id), which is a string that identifies the object.
Many COM objects have typelibs that specify the interfaces and methods the object supports, as well as IDispatch which has a method, Invoke, that allows you to dynamically call methods on the object. This enables the object to be used from scripting languages that don't support strong typing.
Some objects support being run in a different process, on a different thread, or on a different machine. COM supports marshalling for these types of objects. If possible, a standard marshaller can use the object's typelib to marshal calls to the object, but custom marshallers can be provided as well.
And there's a whole lot more to COM objects, I'm barely scratching the surface.
10,000 foot view:
COM is the communication mechanism for software components. Example, you can interact with COM interfaces (COM interop in .NET) to use functionality not exposed through a common interface (.NET assembly).
GUIDs are explained fairly decent on Wikipedia http://en.wikipedia.org/wiki/Globally_Unique_Identifier
I always understood LIB files to be object files for the C++ linker. They contain the code for all objects in a cpp file. The compiler optimizes when it links disregarding portions of the object file that it doesn't need.
Someone please clarify as I am sure I butchered some of this.
COM is Microsoft's Component Object Model, a binary-compatible interface for programs written in various languages to interoperate with each other. It is the "evolutionary step" between the OLE and .NET technologies.
If you want to learn about COM from the C++ perspective, take a look at Don Box's Essential COM, or ATL Internals by Rector and Sells.
The group microsoft.public.vc.atl is probably the best place to ask questions you can't get answers for here. It's primarily an ATL newsgroup, but it seems to be the newsgroup with the most traffic for general COM questions as well. (just be prepared for the usual newsgroup curtness & impatience)
COM is a method to develop software components, small binary exe, that provides services for applications, OS and other components. Developing custom COM comnponent is like developing Object oriented API. GUID is a Global unique ID and used to identify a COM component uniquely.
You can refer a very good book by Dale Rogerson for more details. Inside COM

When To Change a GUID on a Type Library

I know that when you add/change/remove methods in a COM interface you're supposed to change the interface/coclass GUID but what about type libraries. When should you change the type library's GUID? Do you change it if a GUID inside the type library has changed? Or should you only change it when something that doesn't have its own GUID within the type library changes.
The basic principle is that COM interfaces and Type Libraries should be immutable (that is, they shouldn't ever change). If you change one item inside a COM interface, then the new version needs to be a completely separate entity from the previous version. The only way to do this is to change the GUID for every interface in the library and the GUID for the type library itself. It's also a good idea (for your own personal sanity) to change the name of the type library.
Ideally you shouldn't ever change a COM interface. Instead create a new derived COM interface and publish in a new type library.
I've got a similar question.
I had an original control with CLSID_A that implemented interface IID_A in some 1.0 type library with GUID_A
Later on, I decided to add a new interface to the original control. It would then implement both IID_A and IID_B interfaces. I figured that I should probably keep the same CLSID but didn’t knew much what to do with the typelib itself. I was mostly doing VC++ programmatic-by-the-book stuff which involved QueryInterface and didn’t cared much about versioning and typelib. You wanted to create an object with a specific CLSID, you just asked CoCreated instance...and then Queried interface for potential support of the new interface...
Now when I get into fancier environments like LabVIEW or design-time drop-in development environments like Microsoft .NET, MFC stuff that seems to break.
You are mentioning in your answer to change all of the GUID. Is the whole paradigm of adapting an application based on available functionality dead, that a newer application could still use its basic functionality with the older version of a control? Maybe I didn’t catch the later wave that is: No point in adapting an application to run using old control version, it simply requires a specific control version. That would be reason M$ also came out with the ASSEMBLY thing.