Solved

Unmanaged C DLL calling from VB.net and having it pass wrong information

Posted on 2004-10-06
2
261 Views
Last Modified: 2013-11-20
I have a c++ dll I created that encrypts a string and converts the string to HEX and vice versa.

Here is the following functions from my dll file (c++)

void * __stdcall CreateDll3()
void __stdcall DestroyDll3(void * objptr)
int __stdcall dEncrypt(void * objptr, char* sData, char* sPassword)
int __stdcall dDecrypt(void * objptr,char *sData, char *sPassword)
BSTR __stdcall dGetBuffer(void * objptr)
void __stdcall ExecuteProgram(BSTR cmdl,BSTR startDir)
BSTR __stdcall CharStr2HexStr(char* pucCharStr, int iSize)
BSTR __stdcall HexStr2CharStr(char* pszHexStr, int iSize)

Now from my vb.net program I am doing the following

    Private Declare Function CreateDll3 Lib "Encryption.dll" () As Long
    Private Declare Sub DestroyDll3 Lib "Encryption.dll" (ByVal objptr As Long)
    Private Declare Function dGetBuffer Lib "Encryption.dll" (ByVal objptr As Long) As String
    Private Declare Function dDecrypt Lib "Encryption.dll" (ByVal objptr As Long, ByVal qData As String, ByVal password1 As String) As Integer
    Private Declare Function dEncrypt Lib "Encryption.dll" (ByVal objptr As Long, ByVal qData As String, ByVal password1 As String) As Integer
    Private Declare Sub ExecuteProgram Lib "Encryption.dll" (ByVal cmdline As String, ByVal startDir As String)
    Private Declare Function CharStr2HexStr Lib "Encryption.dll" (ByVal str As String, ByVal size As Integer) As String
    Private Declare Function HexStr2CharStr Lib "Encryption.dll" (ByVal hexcode As String, ByVal size As Integer) As String

    Private Sub mnuCreateSession_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCreateSession.Click
        Dim path As String
        Dim tagetApp As String
        Dim encrypt As String
        Dim objptr As Long
        Dim enc As String
        Dim peram As String
        Dim hexcode As String

        objptr = CreateDll3()
        'txtPassword.Text = vPass
        enc = dEncrypt(objptr, vPass, "123456789")
        encrypt = dGetBuffer(objptr)
        hexcode = CharStr2HexStr(encrypt, 16)
        DestroyDll3(objptr)

        MsgBox(encrypt)
        peram = vUserName + " " + hexcode + " " + vSystem + " " + vCli

        path = Application.StartupPath + "\AppLoader.exe"

        Dim psi As New System.Diagnostics.ProcessStartInfo(path)
        psi.WorkingDirectory = Application.StartupPath
        MsgBox(peram)
        psi.Arguments = "vbquicksd" + " " + peram
        psi.RedirectStandardOutput = False
        psi.WindowStyle = ProcessWindowStyle.Normal
        psi.UseShellExecute = False
        Dim ShareGuard As System.Diagnostics.Process
        Try
            ShareGuard = System.Diagnostics.Process.Start(psi)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

My issue is when you call the dEncrypt function you pass the object of the class Crypto, the users password and your encryption shared key. Everything seems to pass correctly except in the dll the function is recieving a werd string of characters as the users password and the users password is ending up as the encryption password. I also wrote this same thing in VB 6 and everything works great, so I know it has something to do with .NET.
0
Comment
Question by:rader19
2 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 12246111
Try this:

Private Declare Function dDecrypt Lib "Encryption.dll"
    (ByVal objptr As Long,
    <MarshalAs(UnmanagedType.LPStr)>
    ByVal qData As String,
    <MarshalAs(UnmanagedType.LPStr)>
    ByVal password1 As String) As Integer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
windows 10 how make picture as desktop background 2 58
ShiftLeft challenge 21 78
Fibonacci challenge 11 112
how to use laptop or pad camera in vb.net windows application 2 55
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

861 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

23 Experts available now in Live!

Get 1:1 Help Now