New Message on dotNET User Group Hyd

Article: Calling managed code(C#.NET) from unmanaged(VB) - Part I

Reply
  Reply to Sender   Recommend Message 1 in Discussion
From: johnsonsmithy2k2


Hi Folks!

 Good day

Today, I'm going to discuss ab't Invoke Managed Code from Unmanaged Code.,how to use managed assembly from VB or VB script. For a start we will first check managed side of story. To expose managed types, properties, fields, or events to COM they must be public. Types can't be abstract and they must have default constructor. It's recommendable to define interface explicitly but it is not necessary, tools provided with .NET Platform are capable of creating one for you. Let's try it.
using System;


public class T

{

public string A(string s)

{

return s+" from C#";

}

}

Copy and paste code in notepad and save it as sample.cs. Open command line and execute the following:
csc /t:library sample.cs

tlbexp sample.dll /out:sample.tlb

regasm sample.dll

Copy sample.dll and sample.tlb to corresponding directory. To check early binding add reference to your library, browse to and select sample.tlb. Insert new module into project and paste the following in code editor:
Sub Test()

Dim o As New T

MsgBox o.A("Hello"), , "VBA test"

End Sub

Sub LBTest()

Dim o

Set o = CreateObject("T")

MsgBox o.A("Hello"), , "VBA late binding test"

End Sub

If you like to do the same from VB script copy sample.dll and sample.tlb to "C:\WINNT\system32" because wscript.exe will need these to execute your script. Create new .VBS file and paste into it the following:
Dim o

Set o = CreateObject("T")

MsgBox o.A("Hello"), , "VBS test"

When you are done with testing don't forget to unregister your sample.dll, to do that execute from command line "regasm sample.dll /unregister". Also deleting sample.dll and str.tlb copies from corresponding path and "C:\WINNT\system32" is a good idea. But some developers will be interested to examine in closer details what is in sample.tlb.

.......................................... to be continued in Part II

Notice: The information contained in this e-mail message and/or attachments to it may contain confidential or privileged information. If you are not the intended recipient, any dissemination, use, review, distribution, printing or copying of the information contained in this e-mail message and/or attachments to it are strictly prohibited. If you have received this communication in error, please notify us by reply e-mail or telephone and immediately and permanently delete the message and any attachments. Thank you

View other groups in this category.

Click here
Also on MSN:
Start Chatting | Listen to Music | House & Home | Try Online Dating | Daily Horoscopes

To stop getting this e-mail, or change how often it arrives, go to your E-mail Settings.

Need help? If you've forgotten your password, please go to Passport Member Services.
For other questions or feedback, go to our Contact Us page.

If you do not want to receive future e-mail from this MSN group, or if you received this message by mistake, please click the "Remove" link below. On the pre-addressed e-mail message that opens, simply click "Send". Your e-mail address will be deleted from this group's mailing list.
Remove my e-mail address from dotNET User Group Hyd.

Reply via email to