Conversion of VB6 Code to VB.NET - Handling Pointer to String

I'm upgrading a software licensing class from VB6 to VB.NET (2013) that uses an old dll to encrypt and decrypt license keys. I need to continue using the same dll to avoid problems with installed software in customer locations.

The dll function 'fGetEncrypted' returns a pointer to a null-terminated string. Here's an example of code snippets from a few places in the VB6 project that get a string. I've got most of it converted and clean, but I can't figure out how to convert the pfPtrToStr function at line 19 below:
' In a form:
    ' get encrypted registered station number
    msStationNumber = pfPtrToStr(bGetEncryptedInfo("station_number"))

' In a class:
Public Declare Function fGetEncrypted Lib "xxxxx" Alias "GetEncrypted" ( _
    ByVal Company As String, _
    ByVal Product As String, _
    ByVal strName As String) As Long

Public Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" ( _
    lpString As Any) As Long

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
    Destination As Any, _
    Source As Any, _
    ByVal Length As Long)

Public Function pfPtrToStr(ByVal lpsz As Long) As String
Dim sOut                                As String * 80
Dim lLen                                As Long
    lLen = lstrlen(lpsz)
    If lLen > 0 Then
        Call CopyMemory(ByVal sOut, ByVal lpsz, 80)
        lLen = InStr(1, sOut, Chr(0))
        pfPtrToStr = Left(sOut, lLen - 1)
    End If
End Function

Public Function bGetEncryptedInfo(ByVal aStringName As String) As String
    bGetEncryptedInfo = ""
    bGetEncryptedInfo = fGetEncrypted(msDeveloperCompany & Chr(0), _
                                      msProductName & Chr(0), _
                                      aStringName & Chr(0))
End Function

Open in new window

Also, if there is a simpler or better way to approach this, I'd appreciate any suggestions.
petekipeOwnerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
One way would be to write an intermediate dll in C++.net.  That means mixed mode so you can access the unmanaged dll (de/encrypt) and provide a managed interface for the rest of your app.
0
Bob LearnedCommented:
Another way would be to use marshaling attributes for the types.  You need to find out what the specific type of string it is:

Default Marshaling for Strings
http://msdn.microsoft.com/en-us/library/s9ts558h(v=vs.110).aspx

UnmanagedType.AnsiBStr
A COM-style BSTR with a prefixed length and ANSI characters.

UnmanagedType.BStr
A COM-style BSTR with a prefixed length and Unicode characters.

UnmanagedType.LPStr
A pointer to a null-terminated array of ANSI characters.

UnmanagedType.LPTStr (default)
A pointer to a null-terminated array of platform-dependent characters.

UnmanagedType.LPWStr
A pointer to a null-terminated array of Unicode characters.

UnmanagedType.TBStr
A COM-style BSTR with a prefixed length and platform-dependent characters.

VBByRefStr
A value that enables Visual Basic .NET to change a string in unmanaged code, and have the results reflected in managed code. This value is supported only for platform invoke.
0
petekipeOwnerAuthor Commented:
I stopped trying to directly invoke the Delphi .dll I was trying to use from vb.net, and instead wrote an ActiveX .dll wrapper in VB6.  All is well now.  Thanks to all for the suggestions.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.