Solved

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

Posted on 2014-07-22
4
43 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

697 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