المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Unmanaged code with C#



C# Programming
04-02-2009, 08:50 AM
I would like to start off by saying that I have never done this before, so please excuse if this is a silly mistake.

I have a function within a DLL that I need to call. The C# project will fail on occasion with an exception, "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." I have been looking around, and my best guess is using strings in my project will cause errors.

Any help from the experts? http://www.barakasoft.com/script/Forums/Images/smiley_smile.gif

Here's the code I'm using in C#
public class Encryption
{
[DllImport("Encryption.dll", EntryPoint = "encrypt")]
public unsafe static extern IntPtr encrypt(char [] data, char [] key);

static public string Encrypt(string data, string key)
{
if (data.Length > 0)
{
return Marshal.PtrToStringAnsi(encrypt(data.ToCharArray(), key.ToCharArray()));
}
else
return "";
}

}

And the function in the DLL (C++)
extern "C"
{
__declspec(dllexport) string encrypt(char* data, char* key)
{
...

}
}



Thanks,
-Kalivos