Solved

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

Posted on 2004-10-06
2
258 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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

762 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

6 Experts available now in Live!

Get 1:1 Help Now