Solved

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

Posted on 2014-07-22
4
39 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

803 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