How to use a DLL/TLB from VB6 in VB.Net - vb.net

I have a DLL and its TLB which works in VB6. I added a reference to the DLL and code to use it. The program builds without an error but gives me a runtime error:
The type initializer for "the dll name" threw an exception.
I am guessing I need to register the DLL or something but do not know how to proceed. BTW, both the DLL and TLB are in my project directory.
Any advice is greatly appreciated.
TIA,
John
Thanks for your response Hans. I may not have been clear. I am not programming in VB6. I inherited the DLL (and do not have the source code) and the client wants me to use it in my VB.Net application. I do not have a way to debug the DLL unless I can do that in VS2008.
Thanks,
John

You're going to need to create an Interop layer between your.Net app and the VB DLL.
Take a look at:
http://msdn.microsoft.com/en-us/library/z6tx9dw3(v=vs.85).aspx

Related

Create VB6 application using a class in a DLL, then swap out that DLL after build?

so my question is relatively simple, can I create VB6 application that references a class in a dll, and then substitute that dll for another at runtime?
Now my intial guess is... no chance in VB6.
So my thoughts turned to a VB.net interop dll. Could I do it in here, and then call the interop dll from the VB?
Again, my guess would be no.... but I'd be happy if someone knew differently.
The only thing that I think would actually work would be DI in .Net, but I'm limited to .net 2, or 3.5 at a big push, so I dont know if that is possible.
So for the background....
I have a dll that a specific site uses, but we dont want to ship that out to everyone. Instead, we want to build a clone dll which just has the interfaces setup so that the VB6 build will complete.
When it gets to the site that needs it, they want to replace the dummy dll, and drop in their version instead.
Note: We do use RegFreeCOM when its gets installed, so I do have the manifest files that I could play around with if needed.
Any ideas would be much appreciated.
Nick
Its a COM dll so its not statically linked to the VB6 exe, so long as the clsids and interface ids are the same in the type library for both DLLs, you can swap them around as you see fit. (If its a VB6 dll this is trivial to do with the 'binary compatibility' build option)
You could also use late binding instead and instead of making a reference directly in your VB6 code, you would create an object and then set that object to an instance.
Examples and information:
MVPS
Microsoft

link dll functions at runtime

My program requires some function in a dll at runtime. How do I tell where the dll is? I can copy the dll into the program directory or system 32. Is there other neat way of doing this? Thank you. Also I am working in C++.
Are you using native code (C/C++/Delphi) or .NET? You should see my answer to Call Function from Dynamic Library for an example of how to do this with a native (not .NET) DLL.

Writing .dll in C++ for VB6

I am trying to migrate a VB6-based .ocx into C++. The migration path that I'm settling on is as follows:
Migrate the current .ocx into a regular COM .dll (it doesn't really have any user interface components, and I'm not sure why it's an .ocx anyway).
Write a C++ version of the COM .dll, and use that as my replacement.
My questions are:
a. Does this seem like the right way to go?
b. How does one go about creating a COM .dll in C++ in Visual Studio 2010? I tried creating a simple .dll, using tlbexp to generate a .tlb file, but this doesn't seem to work. I can pull my .tlb into the VB5 project, but there are no classes to instantiate. If I try to use regsvr32 to register the .dll, I get the "no entry point" error.
I have seen references to an option in VS 2005 to "Register for COM Interop", but I haven't been able to find an equivalent option in VS 2010.
Thanks in advance for your help!
You have to create an ATL project:
Then add the COM objects to your component. The missing entry point you receive is because astandard dll does not have the correct entry ppoints for registration, an ATL project does this for you.

change interop dll version created in vb6 when using in .net

I'm facing problem while reference vb6 dll in .net project. When ever i refer a vb6 dll in .net project the interop dll is created with same version(1.2.0.0). Its creating problem for me as the setup used to deploy the application at client side does not replace the dll if the version is same as before. I want to change the version of the interop dll (created using vb6 dll). I read it cam done using tlbIMP but how to create tlb file for vb6 dll. Or is there is any other way to achieve this.
Thanks
Saurabh
Here's a small batch file we use to create our interop dll so it's strongly named:
sn.exe -i MichiganLTAP.pfx MagicContainerName
tlbimp.exe ourVb6.dll /out:Our.Strongnamed.Interop.dll /asmversion:7.1.0.0 /keycontainer:MagicContainerName /machine:X86 /namespace:Our.Strongnamed /verbose /sysarray
sn.exe -d MagicContainerName
The important switch from your perspective is: /asmversion:7.1.0.0
You should be able to set that to whatever you want. Check out the MSDN Library page for more info on the switches available.
It makes very little sense to make the version number of the interop library different from the version number of the type library that was created by VB6. There is a one-to-one mapping between what's in the interop library vs the code you wrote in VB6. The interop library simply contains IL declarations for the VB6 COM interfaces, there is no actual code. The CLR uses it to quickly generate the RCW for the interface.
Change the type library version number in VB6 with Project + Properties, Make tab, Version number. Major and minor is what counts. Do this only when you make a change in the publicly visible VB6 classes. Doing so is required btw, it avoids DLL Hell.

Using VS 2008 (vb.net) I need to create an object I can use in Classic ASP with CreateObject

I am very new to VB.net. I have written these objects in VB6 before. I'm just lost in VB.net, but (kicking and screaming) I have to learn how to do this. I've been googling for hours with only minor steps forward. Can anyone post a link that explains start to finish how to do this?
I have managed to write the class object, What I can not tell is how to register it and where the name1 and name2 in the CreateObject("Name1.Name2") come from.
Regsrv32 will not work. It says "Entry Point not found" and will not register it. Also, I can not drop it into the Assemblies directory. I read something about a regasm command one uses, but I can't seem to make this work either.
Thanks in advance for any assistance.
I am going to assume you are not trying to write a COM DLL but rather a complete project that call various sub assemblies like a VB6 EXE call a ActiveX DLL. If you can be more specific about what you are trying to do it would help me better.
Several points about VB.NET versus VB6.
1) For .NET only projects there is no registration. If a EXE or DLL references another .NET DLL the only requirement is that the DLLs be present in the parent's directory.
2) You can do a COM style registration for .NET apps only by registering the .NET assembly in the GAC. However there are several requirements for doing this. Do a search on the .NET GAC and it will give you the scoop on how to do this.
3) You can setup the .NET assembly to use COM in which case it will operate by the rules of COM including registration with regsvr.
You will find for .NET only project that #2, or #3 only come in rare instances. #1 will apply for 90% of your DLL assemblies. Of This is dependent on your project.
A common use for CreateObject is allow for plug-ins or installable libraries. .NET handles this through the Reflection API. With the reflection API you can look in a directory, go through each .NET DLL and see what them and create objects from what you find. Search for .NET Reflection to read up on this.
If your project is .NET only then I recommend that you create a Assembly that is reference by both the master assembly and the individual sub assembly that define the interfaces of the objects you are creating. This when you use the reflection API and determine the Object type you can assign it to a variable of that interface and code it noramlly with intellisense and other aids.
if you have old COM ActiveX Controls or DLLs .NET will generate a wrapper class that exposes the ActiveX Objects to .NET. I would spend some time learning how .NET does this. What I do create a dummy project and have .NET reference the ActiveX stuff I need. I then find the wrapper projects and DLL it made and move them into a central area. That why when I work on subsquent projects using the same ActiveX stuff I know where all the wrappers are.
You have to go to your class library properties and select the option "Register for COM interop". This will make your assembly available to COM.
You want to create what's called a COM Callable Wrapper (aka CCW) for your .NET component. This basically entails setting up some COM interfaces with some GUIDs and either enabling "Register for COM Interop" in the project properties (as mentioned) or using regasm.exe.