Quantcast
Channel: SharpDevelop Community
Viewing all articles
Browse latest Browse all 1764

Problem with c++ dll error with strings and char*

$
0
0

I am trying to load a c++ dll created with SharpDeveloper and haveing it load in a vb.net program. I am able to use int ,double functions with no problem. But when it comes to string functions it is another story. I am able to run the c++ code in console mode but when I port it out to a dll evey functions works except when strings are involved. I am able to use char* but that is about it. When ever I use strings with or without char* I get  a System AccessViolation\exception was thrown; read write protected memory is curupted. I have checked every where on the net but I have found a few possibilities but nothing is solved. Here is the code. BTW I am using it with Windows 7 and if I use it on XP it works perfectly!

MyClass.cpp

// MyClass.cpp
#pragma once;

usingnamespace System::IO;
usingnamespace System::Security::Cryptography;
usingnamespace System::Runtime::InteropServices;

usingnamespace System;
usingnamespace std;

#include "stdafx.h"
#include <string>
#include <iostream>
#include <vcclr.h>

//#import "AddObj.dll"

class MyClass
{

//8 bytes randomly selected for both the Key and the Initialization Vector
//the IV is used to encrypt the first block of text so that any repetitive
//patterns are not apparent

//Standard DES encryption
public:
String ^Encrypt(string value);

//Standard DES decryption
String ^Decrypt(String ^value);

//TRIPLE DES encryption
String ^EncryptTripleDES(String ^value);

//TRIPLE DES decryption
String ^DecryptTripleDES(String ^value);

String ^RemovePlus(String ^val);

String ^ReplacePlus(String ^val);

String ^AddEqual(String ^val);

String ^RemoveEqual(String ^val);

String ^Left(String ^s,int left);

staticvoidMain();

};

String ^MyClass::RemovePlus(String ^val)
{
String ^functionReturnValue =nullptr;
String ^div ="";
//replaces the + with a - to work with websites
functionReturnValue ="";
div = val->Replace("/","$");
functionReturnValue = div->Replace("+","-");
functionReturnValue =AddEqual(functionReturnValue);
return functionReturnValue;
}

String ^MyClass::AddEqual(String ^val)
{
String ^div ="";
//replaces the - with a + to work with websites
return val +"=";
}

inttest()
{
int account_num =0;
String ^name ="no name";
int money =0;
return money;
}

extern"C"__declspec(dllexport)char*About()
{
return"CruncherC version 1.00.00";
}

extern"C"__declspec(dllexport)char*TestEqual(char* value)
{
string str ="=";
str = value + str;
char* p =newchar[str.length()+1];
memcpy(p, str.c_str(), str.length()+1);
return p;
}

extern"C"__declspec(dllexport) string Encrypt(string inmsg)
{
try
{
return inmsg.c_str();
}
catch( exception e )
{}
}

extern"C"__declspec(dllexport)inttest2(int value)
{
String ^name ="no name";
int money = value +1;
return money;
}

voidmain()
{
//MyClass ^pa = gcnew MyClass();
MyClass pa;
Console::WriteLine(pa.AddEqual("AddEqual"));
cout <<TestEqual("TestEqual")<< endl;
cout <<test()<< endl;
cout <<test2(2)<< endl;
cout <<Encrypt("Encrypted");
cin.get();
}// MyClass.cpp
#pragma once;

usingnamespace System::IO;
usingnamespace System::Security::Cryptography;
usingnamespace System::Runtime::InteropServices;

usingnamespace System;
usingnamespace std;

#include "stdafx.h"
#include <string>
#include <iostream>
#include <vcclr.h>

//#import "AddObj.dll"

class MyClass
{

//8 bytes randomly selected for both the Key and the Initialization Vector
//the IV is used to encrypt the first block of text so that any repetitive
//patterns are not apparent

//Standard DES encryption
public:
String ^Encrypt(string value);

//Standard DES decryption
String ^Decrypt(String ^value);

//TRIPLE DES encryption
String ^EncryptTripleDES(String ^value);

//TRIPLE DES decryption
String ^DecryptTripleDES(String ^value);

String ^RemovePlus(String ^val);

String ^ReplacePlus(String ^val);

String ^AddEqual(String ^val);

String ^RemoveEqual(String ^val);

String ^Left(String ^s,int left);

staticvoidMain();

};

Byte KEY_64[  ={42,16,93,156,78,4,218,32};
Byte IV_64[={55,103,246,79,36,99,167,3};
Byte KEY_192[={42,16,93,156,78,4,218,32,15,167,44,80,26,250,155,112,2,94,11,204,119,35,184,197};
Byte IV_192[={55,103,246,79,36,99,167,3,42,5,62,83,184,7,209,13,145,23,200,58,173,10,121,222};

String ^MyClass::RemovePlus(String ^val)
{
String ^functionReturnValue =nullptr;
String ^div ="";
//replaces the + with a - to work with websites
functionReturnValue ="";
div = val->Replace("/","$");
functionReturnValue = div->Replace("+","-");
functionReturnValue =AddEqual(functionReturnValue);
return functionReturnValue;
}

String ^MyClass::AddEqual(String ^val)
{
String ^div ="";
//replaces the - with a + to work with websites
return val +"=";
}

inttest()
{
int account_num =0;
String ^name ="no name";
int money =0;
return money;
}

//Works
extern"C"__declspec(dllexport)char*About()
{
return"CruncherC version 1.00.00";
}

//Doesn't work
extern"C"__declspec(dllexport)char*TestEqual(char* value)
{
string str ="=";
str = value + str;
char* p =newchar[str.length()+1];
memcpy(p, str.c_str(), str.length()+1);
return p;
}

extern"C"__declspec(dllexport) string Encrypt(string inmsg)
{
try
{
return inmsg.c_str();
}
catch( exception e )
{}
}

//Works
extern"C"__declspec(dllexport)inttest2(int value)
{
String ^name ="no name";
int money = value +1;
return money;
}

voidmain()
{
//MyClass ^pa = gcnew MyClass();
MyClass pa;
Console::WriteLine(pa.AddEqual("AddEqual"));
Console::WriteLine(KEY_64[0]);
cout <<TestEqual("TestEqual")<< endl;
cout <<test()<< endl;
cout <<test2(2)<< endl;
cout <<Encrypt("Encrypted");
cin.get();
}

VB.Net code

'Module

Public Module Module1
    
'    <DllImport("CruncherC.dll", CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Cdecl)> _
'    public Function Encrypt(value As string) As String
'        
'    End Function
        
    Public Declare Function About Lib "CruncherC.dll" Alias "About" () As String
    
    Public Declare Function Encrypt Lib "CruncherC.dll" Alias "Encrypt" (inmsg As StringAs string
    
    Public Declare Function TestEqual Lib "CruncherC.dll" Alias "TestEqual" (ByVal value As StringAs String
    
    Public Declare Function test2 Lib "CruncherC.dll" Alias "test2" (ByVal value As integerAs integer
    
End Module

'Form

Imports system.text

Public Partial Class MainForm
    Public Sub New()
        ' The Me.InitializeComponent call is required for Windows Forms designer support.
        Me.InitializeComponent()
        
        '
        ' TODO : Add constructor code after InitializeComponents
        '
    End Sub
    
    Sub TextBox1TextChanged(sender As Object, e As EventArgs)
        
    End Sub
    
    Sub Button1Click(sender As Object, e As EventArgs)

        Dim sb As New StringBuilder(About(),512) 'This works

        Dim test As New StringBuilder(TestEqual("Encrypted"),512) 'Error thrown
        textBox2.Text = test.ToString
    End Sub
End Class


Viewing all articles
Browse latest Browse all 1764

Trending Articles