1
votes

When trying to call (DLLImport) an external c++ dll from a .net application that has a manifest file with requireAdministrator, I get this error trying to call function from the C++ dll in Windows 7 with UAC enabled.

Method I am calling: EnCrypts

Exception:

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Code:

public class BlowFish
{
    [DllImport("BlowfishTool.dll", CharSet = CharSet.Auto)]
    public static extern String EnCrypt(String strData, String strPassword);

    [DllImport("BlowfishTool.dll", CharSet = CharSet.Auto)]
    public static extern String EnCrypt(String strData, String strPassword, bool doNotUsePassChecking);

    [DllImport("BlowfishTool.dll", CharSet = CharSet.Auto)]
    public static extern String DeCrypt(String strData, String strPassword, bool doNotUsePassChecking);

    [DllImport("BlowfishTool.dll", CharSet = CharSet.Auto)]
    public static extern String DeCrypt(String strData, String strPassword);


    public static String EnCrypts(String strData, String strPassword)
    {
        return EnCrypt(strData, strPassword, true);
    }
}
1
Manifest and UAC are irrelevant here. The error hints that there is something wrong with the declarations (either parameters, return value, data types and so) or the dll does some kind of memory corruption itself. Can you post the C++ declarations for those funcions?Alejandro

1 Answers

0
votes

It occurs because - String at creation allocates memory for 4 elements. If the size of the returned parameter is more 4 that there is a mistake.

It is necessary to use IntPtr and Marshal.PtrToStringAuto :-)