Solved

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

Posted on 2004-10-06
2
263 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bobThere java chalenge 45 94
Expand to include initial dialog with two choices. 9 80
Modbus - whats the maximum I can store in one register? 4 80
Problem to open Excel file 15 174
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

840 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