2013-03-18 14 views
0

별도로 사용 된 응용 프로그램 용 CAN Dll 프로그램이 있습니다. 이제 Driver Program을 내 응용 프로그램에 포함 시켰습니다.이 오류가 발생했습니다. System Access Violation Exception:Attempted to read or write protected memory. 오류는 무엇입니까? 이 오류를 던지고있는 DLL 코드와 응용 프로그램 코드를 첨부하고 있습니다.시스템 액세스 위반 예외 : 보호 된 메모리를 읽거나 쓰려고 시도했습니다.

캔의 DLL 코드

int receive_data(unsigned char *data_output, int *MsgId, XLportHandle g_xlPortHandle){ 
    XLstatus xlStatus, xlStatus_new; 
    XLevent xlEvent; 
    unsigned int msgsrx=RECEIVE_EVENT_SIZE; 
    char *local_data ="11111111"; 
    DWORD status; 

    xlStatus_new = xlSetNotification(g_xlPortHandle, &h, 1); 

    WaitForSingleObject(h,1); 
    xlStatus = XL_SUCCESS; 

    while(!xlStatus){ 
     msgsrx = RECEIVE_EVENT_SIZE; 
     xlStatus = xlReceive(g_xlPortHandle, &msgsrx, &xlEvent);---------->Here is the error 
     if (xlStatus!=XL_ERR_QUEUE_IS_EMPTY) { 
      memcpy(data_output,xlEvent.tagData.msg.data,8); 
      *MsgId = xlEvent.tagData.msg.id; 
      return 0; 
     } 

     else{ 
      return XL_ERR_QUEUE_IS_EMPTY; 
     } 
    } 
} 

그래서이 프로그램이 성공적 사례 내가

Receive thread quit Unexpectedly 
system Access violation Exception:Attempted to read or write protected memory.This is often an indication that other memory is corrupt at 
xlreceive(int32,Uint32*,s_xl_event*) 
at receive_data(Byte*data_output,int32*MsgId,Int32 g_xlPortHandle). 
+1

아마도 'xlEvent.tagData.msg.data' 버퍼를 제공해야합니까? xlEvent는 초기화가 필요합니까? – noelicus

+0

이지만 xlEvent.tagData.msg.data는 버퍼 data_output으로 복사됩니다. – bobby2387

+0

xlSetNotification 또는 WaitForSingleObject에서 오류를 확인하지 않습니다. 어느 쪽이든 또는 둘 다 실패 할 수 있으며 이는 다운 스트림에서 충돌을 일으킬 수 있습니다. 또한, h에 대한 선언이 없습니다. 그것은 무엇이며 어디에 정의되어 있습니까? –

답변

0

로 오류가 발생하고 그것을 실행하는 동안 나는 유사한/같은 오류가 초기화의 부족에 의해 발생했다 XLevent 구조체.

초기화 목록을 달성 할 수

:

XLevent xlEvent = {0}; 

하거나 이미 트릭 할 것입니다 memset 함수를 동적 메모리 관리를 사용하는 경우 :

XLevent xlEvent; 
memset(&xlEvent, 0, sizeof(xlEvent)); 

다음은 부분적으로 동등한를 (출력 동일합니다). 자세한 내용은이 스레드를 확인하십시오 : what is the difference between struct {0} and memset 0

관련 문제