Hi all,
I have problem with invoking my c++ dll into c# code. The problem is when one of c++ dll functions being called an error exception accord "System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.".
I don't have any idea what is that mean.
This is c++ dll function:
extern "C" __declspec(dllexport) int ScanFile(string filename, int sigc, int hashc, string sigs[], string hashs[]);
Scanner scan;
int ScanFile(string filename, int sigc, int hashc, string sigs[], string hashs[])
{
int x;
for(x = 0; x < sigc; x++)
scan.sigs.push_back(sigs[x]);
for(x = 0; x < hashc; x++)
scan.hashs.push_back(hashs[x]);
int result = scan.ScanFile(filename);
if(result == 0) { if (scan.fileScanResult.IsVirus) result = 1; }
else result ++;
return result;
}
And this is c# code calling that function:
[DllImport("slib.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int ScanFile(string filename, int sigc, int hashc, string[] sigs, string[] hashs);
public struct FileScanResult
{
public string FileName;
public bool IsVirus;
public int result;
}
public static FileScanResult ScanFile(string Path, string[] Sigs, string[] Hashs)
{
int r = NativeMethods.ScanFile(Path, Sigs.Length, Hashs.Length, Sigs, Hashs);
FileScanResult result = new FileScanResult();
if (r == 0) { result.result = r; result.FileName = Path; result.IsVirus = false; }
if (r == 1) { result.result = r; result.FileName = Path; result.IsVirus = true; }
if (r == 2) { result.result = r; result.FileName = Path; result.IsVirus = false; }
if (r == 3) { result.result = r; result.FileName = Path; result.IsVirus = false; }
return result;
}