?
Solved

From vb.net function to C++ DLL

Posted on 2011-03-21
7
Medium Priority
?
242 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

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