Solved

From vb.net function to C++ DLL

Posted on 2011-03-21
7
225 Views
Last Modified: 2012-05-11
Hi,

I have a vb.net function (in attachment) and I would like to have it in C++.

Does any expert could translate it or help me doing it? My C++ knowledge is very weak.

The function is to be putted in a DLL and called by Delphi.

Delphi calls the DLL like this: PEncripta = Function (Valor : Integer): PChar ; cdecl;
Public Function Encrypt(ByVal EncInteger As Integer) As Integer
        Dim strReturn As String = String.Empty
        Dim cipherText As String = String.Empty
        Dim strSenha As String

        Try
            strSenha = EncInteger
            Dim nLetra As String
            Dim L As Integer
            strSenha = (strSenha)
            For L = 1 To Len(strSenha)
                nLetra = Asc(Mid(strSenha, L, 1))
                nLetra = Chr(Trim(Str(Val(nLetra) Xor (L * 2))))
                cipherText = cipherText & nLetra
            Next

            strReturn = cipherText
        Catch ex As Exception
            strReturn = Nothing
        End Try
        Return strReturn
    End Function

Open in new window

0
Comment
Question by:vmorais_
  • 6
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35182545
Can you clarify one thing:  The method is declared to return an Integer, but it is in fact returning a string. Typo?
0
 

Author Comment

by:vmorais_
ID: 35182807
Sorry, it is supposed to return a string, not an Integer.

I've pasted a test vb function. sorry, my mistake.
0
 

Author Comment

by:vmorais_
ID: 35188023
Hi have this just for testing my first steps and I am getting insane..

The Delphi EXE, can already access the function.

I am not being able to receive parameters from it (commented line is not OK to receive it, I think).
//_CRTIMP int __cdecl system(const char *)

extern "C" { int __declspec(dllexport) var1 = 10; }

extern "C" int __declspec(dllexport) __cdecl Encrypt( int i )
{


}

Open in new window

0
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.

 

Author Comment

by:vmorais_
ID: 35188728
Ok.. now I have this.

If I uncomment X = "teste" line, it shows "teste" on the EXE interface.

Like it is now, it does not return anything (no error, seems like it is returning nothing).
extern "C" __declspec(dllexport) char* Encrypt(char*);
 
extern "C" __declspec(dllexport) char* Encrypt(char* x)
{      
        //x = "teste";

        return (x);

}

Open in new window

0
 

Author Comment

by:vmorais_
ID: 35205646
This is what I have so far..

extern "C" __declspec(dllexport) PCHAR Encrypt(int x)
{      

	char c[200];
	char* cp;
	int z;
	int i=0;

	z=x;
	if (z == 0) strcpy(cp,"0");
    c[i]='T';
	i++;
	c[i]='X';
	i++;
    while (z>0)
    {
        c[i]=z%10+48;
        z/=10;
		i++;
    }
	c[i]=0;
   
	z=x;
	cp=&c[0];
	
return (PCHAR)cp;

}

Open in new window

0
 

Accepted Solution

by:
vmorais_ earned 0 total points
ID: 35225926
Done and working by myself..
// CRMEncoding.cpp : Defines the initialization routines for the DLL.
//
#include <stdlib.h>
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <iostream>

extern "C" __declspec(dllexport) PCHAR Encrypt(int x)
{      

 char c[200];
 char mb[200];
 char* cp;
 int z;
 int i=0;

 z=x;
 if (z == 0) strcpy(cp,"0");
 
    while (z>0)
    {
        c[i]=z%10+48;
        z/=10;
  i++;
    }
 c[i]=0;
 cp=&c[0];
 int y = strlen(cp);
 for(i=0;i<y;i++) 
  mb[i]=c[y-i-1];
   mb[i]=0;
 z=x;


 int  myInt = 0;
 char letter = cp[0];


 char *b = mb;


 for (int i = 1; i < y+1; i++)
 {
  
  myInt = mb[i-1];

  myInt= ((myInt-48)^(i * 2))+48;

 c[i-1]=myInt;

 }
 c[i]=0;

 char * myAscii= new char [6];

 itoa(myInt, myAscii, 10);

return (PCHAR)cp;

}

Open in new window

0
 

Author Closing Comment

by:vmorais_
ID: 35292439
Have not received any useful Help from experts and could get to the answer googling by myself. It was not easy, regarding my experience with C++ and took some days to get to it.
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
How to convert MFC APP to Win32 APP. 19 69
sorting efficency of sorting algorithm 30 99
Least Squares Curve Fitting 4 58
Microsoft C++ code failing in executable that worked 9 84
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

919 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

13 Experts available now in Live!

Get 1:1 Help Now