D
D
D0ct0r_Murder2018-04-16 19:41:30
C++ / C#
D0ct0r_Murder, 2018-04-16 19:41:30

System.AccessViolationException: "An attempt was made to read or write to protected memory. This is often an indication that other memory is corrupted."?

Wrote a trial dll to create a process in C++. I call it in the Sharpe console - this is the error. Here are the codes:

#include "stdafx.h"
#include <Windows.h>
#include <conio.h>

extern "C" __declspec(dllexport) int CallNewProccess() {
  STARTUPINFO si;
  PROCESS_INFORMATION pi;

  ZeroMemory(&si, sizeof(si));

  if (!CreateProcess(NULL, const_cast<WCHAR*>(L"cmd"), NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) {
    return 0;
  }
  else
    return 1;
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

class WinApiClass
{
    [DllImport("DLL4.dll")]
    public static extern int CallNewProccess();
}

namespace ConsoleApp50
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int flag = WinApiClass.CallNewProccess();
            }

            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey(true);
        }
    }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Roman, 2018-04-16
@D0ct0r_Murder

Incorrectly initialized
https://msdn.microsoft.com/en-us/library/windows/d...

extern "C" __declspec(dllexport) int* CallNewProccess()
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    if(!CreateProcess(NULL, const_cast<WCHAR*>(L"cmd"), NULL, NULL,
             FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi))
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return 0;
    }
    return pi.hProcess;
}

And you need to figure out how
// Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );

class WinApiClass
{
[DllImport("DLL4.dll")]
[DllImport("kernel32.dll", SetLastError=true)]
static extern bool CloseHandle(IntPtr hObject);
public static extern IntPtr CallNewProccess();
}

static void Main(string[] args)
{
            IntPtr hndl = 0;
            try
            {
               hndl  = WinApiClass.CallNewProccess();
            }

            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            if( hndl != 0 )
            {
                 CloseHandle(hndl);
            }
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey(true);
}

#
#, 2018-04-16
@mindtester

1 - to check the memory, use the appropriate tests 1 and 2
2 - in the general case, this indicates that you have addressing violations - some of the subprocesses get into memory that is not allowed to it
ps This line confuses me

ZeroMemory(&si, sizeof(si));

S
Stanislav Makarov, 2018-04-16
@Nipheris

Start by testing your C++ code in a normal console application and make sure it works.
Specifically, now I can advise you to fill out the structure correctlySTARTUPINFO

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question