Getting SIGSEGV (Segmentation fault error)

Posts 1–8 of 8 · Page 1 of 1
Getting SIGSEGV (Segmentation fault error)
Hello all.

I am making a program in c++, and i am getting a SIGSEGV (Segmentation fault) error. I have googled it, and it is to do with using pointers incorrectly. However, i do not think i have used them incorrectly. Can someone check this piece of code and correct me.

Code:
    DWORD dwPID[100] = {NULL};
    DWORD* ptr = dwPID;
    DWORD cbNeeded = NULL;
    EnumProcesses(ptr, 100, &cbNeeded);
The error occurs at the EnumProcesses line. Please help ASAP.

@Swag can you help?
@giniyat101 can you help?
The correct code should be:

Code:
/*
BOOL WINAPI EnumProcesses(
  __out  DWORD *pProcessIds,
  __in   DWORD cb,
  __out  DWORD *pBytesReturned
);
*/

DWORD dwPID[100] = { NULL };
DWORD Returned = NULL;

if( !EnumProcesses( &dwPID[0], sizeof( DWORD )*100, &Returned ) )
            printf("Error: 0x%.8x\n", GetLastError() );
Quote Originally Posted by .::SCHiM::. View Post
The correct code should be:

Code:
/*
BOOL WINAPI EnumProcesses(
  __out  DWORD *pProcessIds,
  __in   DWORD cb,
  __out  DWORD *pBytesReturned
);
*/

DWORD dwPID[100] = { NULL };
DWORD Returned = NULL;

if( !EnumProcesses( &dwPID[0], sizeof( DWORD )*100, &Returned ) )
            printf("Error: 0x%.8x\n", GetLastError() );
Yes, i think that to
thanks guys, ill test it...

---------- Post added at 03:29 PM ---------- Previous post was at 03:16 PM ----------

Still doesn't work. Here is the whole function:

Code:
DWORD GetProcessPID(string name)
{
    DWORD ProcPid = NULL;
    char FileName[100] = {NULL};
    DWORD dwPID[100] = {NULL};
    DWORD Returned = NULL;

    if(!EnumProcesses(&dwPID[0], 100, &Returned))
    {
        MessageBoxA(NULL, "Error", "Injector", MB_OK);
    }
    else
    {
    for (int i=0; i<100; i++)
    {
        HANDLE hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwPID[i]);
        GetModuleBaseNameA(hProc, NULL, FileName, 100);
        CloseHandle(hProc);
        if (!(_stricmp(FileName, name.c_str())))
        {
            ProcPid = dwPID[i];
        }
    }
    Sleep(50);
    return ProcPid;
    }
    return NULL;
}
GetModuleBaseNameA(hProc, 0, &FileName[0], 100 );

If that is not the problem however try this:
Code:
DWORD *dwPID = new DWORD[100]; // it could be that your stack grows too large for the windows function to work properly, try dynamic allocation.
I can only guess about what your problem might be if can't trace it back to a single line. Add __try{ line here }__except(1){ error box here } to see which line causes the problem.
i know what line is causing error - the EnumProcesses line.

dynamic allocation doesn't work.
You must change this line if(!EnumProcesses(&dwPID[0], 100, &Returned)) to if(!EnumProcesses(&dwPID[0], sizeof( DWORD ) *100 , &Returned)) not 100, as this would suggest using 100 bytes, instead of 0x400 bytes.
thanks for reply.... still doesn't work... what the hell?!?!??!
Posts 1–8 of 8 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

Talk with us