Salizar

I have a C# application that is using Dllimports to decode(base64) and decrypt an xmlfile.
While testing the application the process works with out issues if the application is launched through VS IDE.
If I try to execute the application and process the xml, the call to the decode method throws an "attempted to read or write protected memory" error.
I would expect if the code had an issue it would manifest itself regardless if it was executed via the IDE or not.
This error occurs if I launch the debug version (executable, not through IDE) or the release version of the code.
If anyone has any ideas about this one I would greatly appearciate it.

Thank you






Re: Visual C# General Odd Attempted to read or write protected memory Error

Peter Ritchie

There are many reasons why you'd get an protected memory error with DllImports. The memory that was passed to the function many not have been pinned, the DllImport signature may be wrong, etc.

If you provide the details of the native function definition and your DllImport declaration we can probably find where the problem is.

Having said that, you could use Convert.ToBase64String (and Convert.FromBase64String ) instead of PInvoking something externally.






Re: Visual C# General Odd Attempted to read or write protected memory Error

Salizar

Thanks for the reply, I'll post the DllImport signatures comes monday.
The reason for the Pinvoke is that I'm having to decode an established encryption scheme, however their implementation of Base64 seems to e a bit off. I've attempted to use the .NET 2.0 native decodes in the past but it would always fail because the byte count didn't match up correctly.
And the encryption is RC4.

Again these same methods work if the Application is invoked from within VS IDE.
These are only failing when the application is launched directly.



Code Block

// DLL Export from C++ DLL

#define DllExport __declspec (dllexport)

extern "C"

{

DllExport LPCTSTR Decrypt64(LPCTSTRP EncryptedMsg);

DllExport LPCTSTR Decrypt(LPCTSTR EncryptedMsg);

}


//DllImport from C#

[DllImport("Licenselib.dll" EntryPoint="Decrypt64")]

public static extern string Decrypt64(string EncryptedMsg);


[DllImport("Licenselib.dll" EntryPoint="Decrypt")]

public static extern string Decrypt(string EncryptedMsg);





Re: Visual C# General Odd Attempted to read or write protected memory Error

Peter Ritchie

Base64 is not encryption, it's a conversion from base 10 to base 64. Sounds to me like your Decrypt64 method simply decrypts encrypted data that has been encoded as base 64 (i.e. it converts from base 64 to base 10 before decrypting...).

What have you defined "LPCTSTRP" as

Is licenselib.dll compiled with UNICODE defined

If Decrypt64 is modifying the memory pointed to by EncryptedMsg, you will get protected memory error.






Re: Visual C# General Odd Attempted to read or write protected memory Error

Salizar

The raw data is an RC4 encoded XML file that is then Base64'ed in order to store the result in a database.
As mentioned before I can't simply use standard base64 decoding since their implementation is slightly off from a standard base64 implementation. Yeah they botched it years ago, and I have to play along...

I found another thread that appears to be having the same issue, Works inside VS but fails when launching the application manually. I'll go through that thread to see if anyone resolved it.

LPCTSTR is defined as
typedef const TCHAR FAR * LPCTSTR;
Project Character Set Setting is set to Use Multi-Byte Character Set

Here is the other thread for reference:
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=202386&SiteID=1






Re: Visual C# General Odd Attempted to read or write protected memory Error

Peter Ritchie

I know what LPCTSTR is, you have another define "LPCTSTRP" that isn't defined in Visual C++ anywhere; if that doesn't map to a String or a StringBuilder you're going to run into memory access problems.




Re: Visual C# General Odd Attempted to read or write protected memory Error

Salizar

I was able to resolve the issue.
At some point in my project I had enabled the Option to Allow Unsafe Code.
Once I disbaled this the Application started functioning without issues...
Not sure why this would have caused this issue....