Link to home
Start Free TrialLog in
Avatar of 1_21gigawatts
1_21gigawatts

asked on

Using vb6 dll in vb.net

I have a dll (xxx.dll) that was written for  use with vb6. I don't have the source for it, and the publisher doesn't want to touch it.

I am able to access this fine from vb6. I tried to add it to a vb.net program, but when I try to add the reference to it (Add Reference -> Browse) I get:

"A reference to c:\windows\system32\xxx.dll could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component."

I also tried to register the dll with regsvr32 and got an error about "DllRegisterServer entry point was not found"

An example of how this dll is used in the vb6 program is:

Public Declare Sub SetDataFeedhwnd Lib "XXX.DLL" (ByVal hwnd As Long)

I really do need to get this to work, and I'm pretty much a beginner at vb.net.
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Try putting the DLL in the same folder as your .Net EXE with one of the following declarations:

    Public Declare Sub SetDataFeedhwnd Lib "XXX.DLL" (ByVal handle As IntPtr)
    Public Declare Sub SetDataFeedhwnd Lib "XXX.DLL" (ByVal handle As Integer)

Avatar of lovelimetostitos
lovelimetostitos

Have you used tlbexp.exe to create the COM DLL? If not, export it using tlbexp and use the DLL that is generated as the output.

Following is an article on how to do it. It uses C# but it should be pretty much the same in VB.Net.
http://www.developer.com/net/csharp/article.php/1501271

I'm sorry. I should have said tblimp.exe.
Avatar of 1_21gigawatts

ASKER

OK, I got this partly working by making a new module with this line in it (which gets one function to work)

  Public Declare Sub SetDataFeedhwnd Lib "XXX.DLL" (ByVal hwnd As Long)

Now, I'm in to the next function in the dll, which is:

  Public Declare Function SETUPDATA Lib "XXX.DLL" (ByVal Ticker As String, ByVal description As String, ByVal BarInt As Single, ByVal NumColumns As Long, ByVal Category As String, ByVal FirstDateAvailable As Double) As Long

The compiler doesn't complain about the declaration, but when I call it with

  i = SETUPDATA(Ticker, Description, BarInterval, 3, Category, Today.ToOADate)

I get an error about "Attempted to read or write protected memory..."

There's GOT to be an easy way to do this.

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK, I replaced the declaration with this:

   Public Declare Function NSTSETUPDATA Lib "NSTFEED.DLL" ( _
    ByVal Ticker As System.Text.StringBuilder, _
    ByVal description As System.Text.StringBuilder, _
    ByVal BarInt As Single, _
    ByVal NumColumns As Long, _
    ByVal Category As System.Text.StringBuilder, _
    ByVal FirstDateAvailable As Double _
    ) As Long


and the calling code with this:

        Dim Temp As Integer
        Dim Ticker As New System.Text.StringBuilder()
        Dim Description As New System.Text.StringBuilder()
        Dim BarInterval As Double
        Dim Category As New System.Text.StringBuilder()
        Dim StartDate As Double


                Ticker.Append("EUR/USD")
                Description.Append("Euro/Dollar")
                BarInterval = 0
                Category.Append("Stock")
                StartDate = Today.ToOADate


                Temp = NSTSETUPDATA(Ticker, Description, BarInterval, 3, Category, Today.ToOADate)

And I'm still getting the same error.
Are all of the values only passed IN?...and not modified by the DLL?...or are some of them supposed to be "ByRef"?
No, This is the way it was set up in the vb6 program:

Public Declare Function NSTSETUPDATA Lib "NSTFEED.DLL" (ByVal Ticker As String, ByVal description As String, ByVal BarInt As Single, ByVal NumColumns As Long, ByVal Category As String, ByVal FirstDateAvailable As Double) As Long

Changing longs to ints pretty much did the trick. The stringbuilder thing (though I spent way too long chasing it) was unneccessary in this case.
So, I beat it into submission. Mostly, all I had to do was change longs to ints, and tweak up the way dates and times are converted. It's all working now. Woo Hoo!
Glad ya finally solved it!...  =)