Determining .Net method suffix number in VBScript (COM-interop)

You can use .NET methods via COM-interop in VBScript. You have to add a specific suffix number to the method as overloads don't cross a managed / unmanaged boundary. The suffix number doesn't seem to be in any particular order ... how is the suffix number determined?


Dim encoding, bytesthroughdotnet
Set encoding = CreateObject("System.Text.UTF8Encoding")
    bytesthroughdotnet = encoding.GetBytes_4("你好Ğ") 'get bytes
    WScript.Echo LenB(bytesthroughdotnet) 'length
Set encoding = Nothing


Why is _4 used for GetBytes?

(This question follows this answer )


source to share

2 answers

Since VBScript does not support overloaded methods, each overloaded method in a class is named uniquely using numbers appended to their name. They are numbered in the order in which they are defined in the original class. More information in my article Using .Net Interops in VBScript on ASP Free.



Microsoft doc Converting Exported Member - Overloaded methods explain everything already. In short, @ Nilpo's answer is correct, the fastest way is just trial and error .

Overloaded methods

Although .NET supports overloaded methods, the IDispatch interface relies solely on the method name to bind, rather than on the complete method signature. Therefore, it is not capable of supporting overloaded methods. However, to provide access to overloaded methods of type, Tlbexp.exe decorates the overloaded method names with a sequential number so that each method name is unique.

The following managed and unmanaged signatures show the inclusion of numbers:

Managed signature

interface INew { public:
    void DoSomething();
    void DoSomething(short s);
    void DoSomething(short l);
    void DoSomething(float f);
    void DoSomething(double d);


Unmanaged signature

 interface INew {
    void DoSomething();
    void DoSomething_2(short s);
    void DoSomething_3(short l);
    void DoSomething_4(float f);
    void DoSomething_5(double d);


The COM signature for methods appears as a single DoSomething method, followed by a sequence of decorated DoSomething_x methods, where x starts at 2 and increments for each overloaded form of the method. Note that some of the overloaded methods may inherit from the base type. Topics not less, there is no guarantee that the overloaded methods will have the same number as the promotion type version .

Although .NET clients can use the method overloaded form, COM clients must have access to decorated methods. Object browsers display all forms of a decorated method with a method signature so you can choose the correct method. A late-bound client can also call IDispatch :: GetIdsOfNames , passing in a well-formed name, to get the DispID of any overloaded method.



All Articles