Solved

From vb.net function to C++ DLL

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

Expert Comment

by:käµfm³d 👽
Comment Utility
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_
Comment Utility
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_
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:vmorais_
Comment Utility
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_
Comment Utility
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
Comment Utility
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_
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

772 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

10 Experts available now in Live!

Get 1:1 Help Now