Solved

From vb.net function to C++ DLL

Posted on 2011-03-21
7
237 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_
[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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

724 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