Solved

From vb.net function to C++ DLL

Posted on 2011-03-21
7
232 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
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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ error cannot convert from std::string to const char * 6 298
Best book to learn C++ 4 87
learn programming 8 66
what type of pointer is this pointer? 3 21
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

685 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