Valhalla Legends Forums Archive | .NET Platform | [C#] Plugin usable in COM?

AuthorMessageTime
DDA-TriCk-E
Does anyone know if it is possible to create a C# plugin which can be used in Visual Basic 6.0 using CreateObject()? If so, point me to a documentation on it please...

Thanks.
July 5, 2007, 12:41 AM
Myndfyr
[quote author=Chriso link=topic=16848.msg170721#msg170721 date=1183596072]
Does anyone know if it is possible to create a C# plugin which can be used in Visual Basic 6.0 using CreateObject()? If so, point me to a documentation on it please...

Thanks.
[/quote]

A plugin?  For what?

MBNCSUtil exposes objects to COM.  See: http://www.jinxbot.net/CodeBrowse/MBNCSUtil_1_3_1_8/Com/ICdKey.cs.aspx and http://www.jinxbot.net/CodeBrowse/MBNCSUtil_1_3_1_8/CdKey.cs.aspx.  Doing this, once you register the type library using regasm, you can use code along the lines of CreateObject("MBNCSUtil.CdKey").

July 5, 2007, 1:00 AM
DDA-TriCk-E
Thanks, thats what I needed :)
July 5, 2007, 8:38 PM
DDA-TriCk-E
One more question, how would I pass a COM object to .NET? I want to give the C# plugin access to the main form object in Visual Basic 6.0.
July 5, 2007, 8:47 PM
Myndfyr
[quote author=Chriso link=topic=16848.msg170752#msg170752 date=1183668466]
One more question, how would I pass a COM object to .NET? I want to give the C# plugin access to the main form object in Visual Basic 6.0.
[/quote]
I'm not sure that's possible.  I don't know that a VB Form implements COM.
July 5, 2007, 10:35 PM
dRAgoN
[quote author=Chriso link=topic=16848.msg170752#msg170752 date=1183668466]
One more question, how would I pass a COM object to .NET? I want to give the C# plugin access to the main form object in Visual Basic 6.0.
[/quote]
FrmMain.Hwnd?
July 5, 2007, 11:19 PM
DDA-TriCk-E
If I pass the handle to the plugin, would C# be able to convert it to an object and use its attributes?

C# calls the object System.__ComObject when it is sent to the plugin from Visual Basic, therefore it knows it is a COM element, one would assume you could access its attributes or create an Interface (maybe?) which contains the attributes you want to use.
July 7, 2007, 8:54 AM
DDA-TriCk-E
[quote author=MyndFyre[vL] link=topic=16848.msg170761#msg170761 date=1183674910]
[quote author=Chriso link=topic=16848.msg170752#msg170752 date=1183668466]
One more question, how would I pass a COM object to .NET? I want to give the C# plugin access to the main form object in Visual Basic 6.0.
[/quote]
I'm not sure that's possible.  I don't know that a VB Form implements COM.
[/quote]
Would I be able to do it with a class declared in Visual Basic?
July 7, 2007, 8:55 AM
Imperceptus
Not to be rude, I am curious why you want to do this from your first post.
July 7, 2007, 7:04 PM
Myndfyr
[quote author=Chriso link=topic=16848.msg170792#msg170792 date=1183798539]
[quote author=MyndFyre[vL] link=topic=16848.msg170761#msg170761 date=1183674910]
[quote author=Chriso link=topic=16848.msg170752#msg170752 date=1183668466]
One more question, how would I pass a COM object to .NET? I want to give the C# plugin access to the main form object in Visual Basic 6.0.
[/quote]
I'm not sure that's possible.  I don't know that a VB Form implements COM.
[/quote]
Would I be able to do it with a class declared in Visual Basic?
[/quote]
As long as the class was an ActiveX object.
July 7, 2007, 8:45 PM
DDA-TriCk-E
[quote author=MyndFyre[vL] link=topic=16848.msg170806#msg170806 date=1183841121]
As long as the class was an ActiveX object.
[/quote]
Hmm I assume VB6 Classes are ActiveX...?
July 8, 2007, 12:18 AM
Barabajagal
...no... ActiveX Classes are ActiveX. When creating a new project, you have the option to make a Standard EXE, ActiveX EXE, ActiveX DLL, or ActiveX Control (plus a lot of others). I'm fairly certain you can make an ActiveX DLL (which is basically a standalone Class) and then use it like a COM Plugin... I don't know, I've never dealt with plugins.. but it's starting to interest me due to a project I'm thinking of writing.
July 8, 2007, 12:28 AM
DDA-TriCk-E
True, I was pretty hungover when I wrote that lol.

Yeah I was just trying to extend my application to use C# plugins, I have got it working with all other type declarations except object's.
July 8, 2007, 2:19 AM
Barabajagal
I don't think objects are interchangeable between languages... but I might be wrong.
July 8, 2007, 2:42 AM
Myndfyr
[quote author=Andy link=topic=16848.msg170820#msg170820 date=1183862544]
I don't think objects are interchangeable between languages... but I might be wrong.
[/quote]
You are in fact, like I pointed out in my first reply to this thread.
July 8, 2007, 10:08 AM
Barabajagal
Maybe I'm misusing the word object... I mean like if you send a textbox to a function to, say, get its handle and visible status... Something like
[code]Public Sub Example(Obj as Object)
Dim ObjWnd as Long
Dim ObjVis as Boolean
    ObjWnd = Obj.hWnd
    ObjVis = Obj.Visible
    MsgBox "The object " & Obj.hWnd & " is " & IIf(ObjVis, "", "not ") & "visible", vbInformation
End Sub[/code]
If that was written in a VB6 ActiveX DLL, could a C# program send it a text box?
July 8, 2007, 10:36 AM
Myndfyr
For your example to work, the textbox that C# would be sending would need to be a COM-visible class that implemented IDispatch (that is, it should be marked with the [InterfaceType] attribute with the values ComInterfaceType.InterfaceIsIDispatch or ComInterfaceType.InterfaceIsDual; and the class interface would also need to expose an hWnd and Visible property.  .NET's control objects do not have an hWnd property, but they do have a Handle property that would make it really easy to implement.  For instance, if you did the following:
[code]
[ComVisible(true)]
public class ComVisibleTextBox : System.Windows.Forms.TextBox, IComTextBox
{
    public IntPtr hWnd { get { return Handle; } }
    //no need to implement Visible since the parent class already does so.
}

[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[ComVisible(true)]
public interface IComTextBox
{
    IntPtr hWnd { get; }
    bool Visible { get; set; }
}
[/code]
July 8, 2007, 11:22 AM
Barabajagal
So objects can be sent through inter-language systems, it's just messy unless it's .NET?
I really don't have any use for this information, it's just somewhat interesting.
July 8, 2007, 11:25 AM
Myndfyr
[quote author=Andy link=topic=16848.msg170829#msg170829 date=1183893902]
So objects can be sent through inter-language systems, it's just messy unless it's .NET?
I really don't have any use for this information, it's just somewhat interesting.
[/quote]
Yes.  The premise behind .NET is language-interoperability; the Common Language Runtime is a definition for how the multiple languages expose the Common Type System.  C# and VB.NET seem very much alike right now because the CTS hadn't really evolved by the time they were released.  On the other hand, there's stuff like IronPython now that can be expressed in the CLR.

COM was Microsoft's real first attempt at exposing reusable components to different languages.  In C++, this is achieved with the help of ATL and Win32 API functions like CoInitialize.  Visual Basic exposed these through ActiveX objects.  COM had a lot of nightmarish programming tasks - registration in the registry, generating type libraries and interface IDs, and versioning, the name a few.  It doesn't HAVE to be messy, but it frequently is.
July 8, 2007, 11:34 AM
DDA-TriCk-E
That being said would it be possible to send a Visual Basic Class to C# via the object type declaration? Couldn't you do something like you did with the textbox example to "expose" the members of the Visual Basic Class in C#?
July 8, 2007, 11:45 AM
Barabajagal
Just turn the vb class into an ActiveX DLL, reference it in C# like you would any other DLL, and call its subs/functions and use its variables and constants? If the class is part of a VB EXE, just make it do the same thing ;)
July 8, 2007, 8:07 PM
Myndfyr
[quote author=Chriso link=topic=16848.msg170831#msg170831 date=1183895154]
That being said would it be possible to send a Visual Basic Class to C# via the object type declaration? Couldn't you do something like you did with the textbox example to "expose" the members of the Visual Basic Class in C#?
[/quote]

To utilize C# with *any* kind of cleanliness, you have to use early binding, and C# needs to see the type library.

There are ways to incorporate late binding with C#, using the Activator class specifically, and this involves using Reflection to get information about the parameters exposed by the object.  That would mean doing something along the lines of:
[code]
Type vbType = Type.GetTypeFromProgID("MyVBBot.SomeClass");
object vbObj = Activator.CreateInstance(vbType);
vbType.InvokeMember("Visible", BindingFlags.SetProperty, null, vbObj, new object[] { true });
[/code]

Do you get the idea?  VERY ugly.
July 8, 2007, 9:00 PM
DDA-TriCk-E
Yeah I get the idea, thanks. :)
July 9, 2007, 1:02 AM

Search