Solved

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

Posted on 2014-07-22
4
37 Views
Last Modified: 2016-06-21
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.
0
Comment
Question by:petekipe
4 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40213965
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 40214093
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
 

Accepted Solution

by:
petekipe earned 0 total points
ID: 40295477
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

Featured Post

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.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

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

18 Experts available now in Live!

Get 1:1 Help Now