Solved

Getting a DLL written in Visual Basic to run in VB.NET

Posted on 2011-02-27
21
626 Views
Last Modified: 2012-05-11
Hello Experts -

I am trying to get a DLL written in Visual Basic to work in VB.NET. Since I've never written a DLL before, I am wondering whether I need to include GETs and LETs to each variable to make it work. I think I read something like that years ago, but that was before NET was introduced.

As it stands right now I have a VB.NET Project with Interop functions defined to read and write to an INI fie with Windows APIs. These are in a Class called Ini. Then I have another Class called Automan that includes the functions I'd like to call from outside the DLL that put wrappers on the APIs and cut out all the details of getting what I want from the INI.

Is this sufficient?  Or, do I have to define/declare the functions in some other way?

At present, I can't communicate with the DLL from a VB executable project.
0
Comment
Question by:gbmcneil
  • 11
  • 5
  • 2
  • +3
21 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 34993024
What IDE are you using?
If VB 2005, 2008 or 2010 then just add a reference to the project of your DLL and access its methods.
NOTE: You'll need to place the dll in the same folder as the executable when redistributing.
0
 

Author Comment

by:gbmcneil
ID: 34993052
Thanks for responding nepaluz. I'm using VB.NET 2008.

I think there is more to it than just adding a reference in the calling executable.

See the code in the calling program and DLL below.
IN THE EXECUTABLE -

Module GlobalDeclarations

Public Declare Function GetStartTime Lib "c:\projects\VB.Net 2008\automan 1.0\automan\bin\release\automan.dll" (ByVal section As String, ByVal subsection As String) As VariantType

End Module


IN THE DLL -

Public Class Automan

    Public Function GetStartTime(ByVal section As String, ByVal subsection As String) As VariantType

        Dim intCharCount As Integer
        Dim defaultval As String
        Dim objResult As New System.Text.StringBuilder(256)
        Dim strFilename = "c:\windows\automan.ini"
        defaultval = "10/20/1964"

        intCharCount = Ini.GetPrivateProfileString(section, subsection, defaultval, objResult, objResult.Capacity, strFilename)
        If intCharCount > 0 Then GetStartTime = CObj(Left(objResult.ToString, intCharCount))

    End Function

End Class

Open in new window

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 34993150
correct
0
 

Author Comment

by:gbmcneil
ID: 34993547
I am at a total loss. I have no idea what I'm doing - what works and what doesn't.

When I run an executable calling the DLL, it bounces quickly out.

First off, I don't know how the DLL knows where to find "GetStartTime" - my sole function in the DLL.
0
 

Author Comment

by:gbmcneil
ID: 34994374
Well, there is something definitely wrong here. I've tried to register a compiled version of the DLL with regsvr32, and it fails.
0
 
LVL 5

Expert Comment

by:MedievalWarrior
ID: 34994585
Hi,

.NET is very different from VB6 in many ways... If you need something like VB6 ActiveX DLL then you want to start with a Class Library project and then use the ComClass template. You can define everything yourself but the template makes it easier. Next you have to forget all about (Regsvr32) when your dealing with a .NET COM library instead you have to use the (RegAsm) utility generate a type library the utility will then register the type library for you. Please check the following:

Walkthrough: Creating COM Objects with Visual Basic
http://msdn.microsoft.com/en-us/library/x66s8zcd(v=VS.90).aspx

Assembly Registration Tool (Regasm.exe)
http://msdn.microsoft.com/en-us/library/tzat5yw6(v=vs.71).aspx
0
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 34996071
Make sure that there is DLL entry point in your DLL file, please see here:
http://oreilly.com/pub/a/windows/2005/04/26/create_dll.html
0
 
LVL 9

Expert Comment

by:Orcbighter
ID: 34996686
Are you trying to convert the VB6 DLL into a VB.NET DLL? Or are you calling the VB6 (unmanaged) DLL from a VB.NET DLL or application?
0
 

Author Comment

by:gbmcneil
ID: 34999005
Thanks everyone for responding. I am going to have to study up.

It's just about noontime here on the East Coast and I just got up - after working on this thing most of the night. Give me an opportunity to get my head back into things.

The long-term goal is to create a DLL that is neither a COM nor a VB.NET-compatible DLL. (Yes, there were DLLs before plain-vanilla VB 3.0 came along.) But, because I am currently configured to test the DLL from a VB.NET executable, I'll go with whatever approach is easiest at the moment. Then, when I've had some positive results, I hope the concept can be to modified to get that simple DLL I'm looking for.

Before I've read anything you folks are suggesting, I figure the issue has something to do with: a) the way I've put the sole function to be called in a Class as opposed to a Module; b) incorrect use of the Namespace; the way I've declared the Windows API's I'm calling; and/or c) the way I've registered the DLL. In otherwords, just about any possible reason you could think of.

To clarify things. I'm trying to write a DLL from scratch. This is my first. I have no experience writing DLLs under VB 6.0 or any other language.

I'll be back to you in a couple of hours.    
0
 

Author Comment

by:gbmcneil
ID: 34999789
Okay. I guess I've got the gist of things.

Can I "create" a standard (non-COM) DLL with VB.NET or do I have to use VB 6?

If the answer is "Yes", can I "test" my DLL using VB.NET or do I have to use VB 6? For your info, the DLL is for my own personal use. It's not going to be distributed.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 125 total points
ID: 34999833
Neither. You should be using C/C++ to create a standard Windows DLL
0
 

Author Comment

by:gbmcneil
ID: 35000233
Hi eql1044 -

Is your answer "You should" or "You must" or "It would be impractical to attempt"?

I know some of you C/C++ guys would like to stamp out every last vestage of the Basic language. For many programmers, it's like workiing in a union shop. I am trying to get an unbiased answer here.
0
 

Author Comment

by:gbmcneil
ID: 35001807
Mr. eql1044 -

I see that you are correct. Standard DLLs are the domain of the C/C++ language.

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_20809342.html?sfQueryTermInfo=1+10+30+com+dll+non+standard+vb.net

That leaves making a COM DLL per what MedievalWarrior suggested above. Let me work on it.

0
 
LVL 5

Expert Comment

by:MedievalWarrior
ID: 35003752
If you really need standard windows DLL I could help you create one, it's not very complicated especially with such little detail you need in your example. Even better your already using VC 2008 so you won't have an EncodePointer issue with your target toolset. In a nutshell it basically means your windows DLL would work on minimum of Windows 2000. If you used VC2010 toolset it would only work on XP SP2 or later.
0
 

Author Comment

by:gbmcneil
ID: 35013853
Thank you for your offer MedievalWarrior, but I am still trying to figure out how I should go about building the DLL.

Here is the current situation. Building a "standard DLL" is out. I am not a C/C++ programmer and don't want to get involved with a long, drawn-out effort. At present it is indefinte whether I should build a DLL as a pure, native VB.NET assembly, or a VB.NET assembly to run under COM.

I have an email message to the company that makes the target application that the DLL would have to run with. That app has a built in VBScript capability and they claim that using its CreateObject function they can interface to non-standard DLLs. But, which specific non-standard VB.NET DLL is still unclear.

Once this is resolved I am going to wrap up this question to The Experts, award points, and move on to finalizng my code.

Thank you for your patience. I have learned a lot.
0
 
LVL 5

Assisted Solution

by:MedievalWarrior
MedievalWarrior earned 125 total points
ID: 35013872
If you would like to Interface with VBScript you will definatley want to expose your .NET DLL to COM as described earlier. Using the <ComClass> template.
0
 

Author Comment

by:gbmcneil
ID: 35013928
Is that because pure VB.NET DLL assemblies don't run in VBScript - as Objects?

And, if they don't run under VBScript, the only place left they could run under is VB.NET 2005 (and later) executables as Objects.

Am I on the right track here?
0
 
LVL 5

Expert Comment

by:MedievalWarrior
ID: 35013955
Yes that is correct. Pure .NET assembly don't have to registered and are only accessible to .NET framework. You can make your .NET assembly COM visible which allows it to "act" as COM server but very deep under they aren't real COM objects in the sense but it does allow you to use them as a type of COM object. Remember you will have to have the target framework installed on each computer you intend to use your COM DLL and you must use RegAsm to register.



0
 
LVL 5

Expert Comment

by:MedievalWarrior
ID: 35013975
Using RegAsm is the manual way... You would typically want to use a installation utility and package your application / DLL including the type library and it can register it for you on the system during installation. If you have the full version and not the express edition you can use Visual Studio Installer which comes with VS2008.
0
 

Author Closing Comment

by:gbmcneil
ID: 35013989
Thanks to everyone for their help. I am going to put my code in a VB.NET COMClass template and see if I can convert it to workable code.
0
 

Author Comment

by:gbmcneil
ID: 35014020
MedievalWarrior -

Thanks for the suggestions. If I can get this DLL to run, I'm going to deploy it on just my computers. I don't expect a distribution problem because each of them has Windows 7 Professional. That means I'll just run the RegAsm tool on each system.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now