Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am able to get the device interface and print out the device path but when I put in the device path as a parameter for create file, the program crashes at the point where it should create the file with the error 0xC0000005.


I have a Windows 7 64 bit computer, I am trying to connect to read data from this USB webcam specifically
Docooler USB 2.0 12 Megapixel HD Camera Web Cam with MIC Clip-on[^]
Here is the code that I am using.
I am automatically setting the required Interface detail size to 90 because the highest is 82
I also have if(i == 2) because that is when the devicepath for the camera prints out.
C++
i= 0;

    if((webdevices = SetupDiGetClassDevs(&GUID_DEVINTERFACE_USB_DEVICE,NULL,NULL,(DIGCF_PRESENT|DIGCF_DEVICEINTERFACE))) != NULL){
            printf("SetupDiGetClassDevs Worked!!\n");
    }else{
        printf("SetupDiGetClassDevs didn't work error %d\n",GetLastError());
    }

    webDevInfoDat->cbSize = sizeof(SP_DEVINFO_DATA);
    webInterDat->cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
    SetupDiEnumDeviceInfo(webdevices,i,webDevInfoDat);
    while(sdei = SetupDiEnumDeviceInterfaces(webdevices,NULL,&GUID_DEVINTERFACE_USB_DEVICE,i,webInterDat)){  if(SetupDiGetDeviceInterfaceDetail(webdevices,webInterDat,webInterDetDat,interDetSiz,&reqInterDetSiz,webDevInfoDat)){
            
            printf("Interface Detail Data\nDevicePath: %s\nRequired Size: %d\n",webInterDetDat->DevicePath,reqInterDetSiz);
        }else{
            printf("Getting SetupDiGetDeviceDetail error %d RequiredSize %d\n",GetLastError(),reqInterDetSiz);
        }

        if(i == 2){
        webcam = CreateFile(webInterDetDat->DevicePath,(GENERIC_READ|GENERIC_WRITE),(FILE_SHARE_READ|FILE_SHARE_WRITE),NULL,OPEN_EXISTING,0,NULL);
        if(webcam == INVALID_HANDLE_VALUE){
        printf("Creating File didn't work %d\n",GetLastError());
        }else{
            printf("Creating File did work %s\n",webcam);
            close(webcam);
        }
        }

        printf("\n");
        i++;
        webInterDat->cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
    }

    if(sdei){
    }else{
        printf("Getting SetupDiEnumDeviceInterfaces error %d I is %d\n",GetLastError(), i);
    }


What I have tried:

I have tried keeping it to the A-methods then tried changing it to the wide methods.
I have tried changing the '?' to a '.'. Then I tried adding in "\\\\.\\" and "\\\\?\\" and that still didn't work.
When I comment out the create file method it works perfectly and reaches to the end of the program with no errors.
Posted
Updated 24-Aug-16 21:49pm
Comments
jeron1 24-Aug-16 16:03pm    
If you set a break point on the CreateFile() call, all parameters are as you expect?
Member 12701845 24-Aug-16 23:40pm    
I tried going through it with a debugger and it looks like all the values are right. Do you have any other Ideas?

1 solution

Error code 0xC0000005 is the HRESULT form of error 5 (ERROR_ACCESS_DENIED) with facility NULL.

If you don't need to write to your web cam, try to open the device for reading only. If this fails too or you need to write, try executing your application as administrator.

[EDIT]
Just saw this line:
printf("Creating File did work %s\n",webcam);

webcam is a HANDLE passed as string parameter to printf. This may result in an access violation or unexpected behaviour when CreateFile was successful.
[/EDIT]
 
Share this answer
 
v2
Comments
Member 12701845 25-Aug-16 9:09am    
I tried running as administrator for both read and write and read only and it still gives me the same crash with the same error.
Jochen Arndt 25-Aug-16 9:27am    
Crash?
You are checking the return value so that there should be no crash.

But see my updated answer about a possible reason which I did not recognised before.

If that is not the reason you may also try to open with exclusive acces (no sharing flags). That should fail with ERROR_SHARING_VIOLATION when the device is already opened by another application. If so, try your code also with the other application closed.
Member 12701845 25-Aug-16 9:39am    
I looked at your EDIT and that helped with my problem. I just cut out printing the variable and now it prints out that "Creating File Did work". Now it crashes at the "close(webcam)" line with error 0xC0000096
Jochen Arndt 25-Aug-16 9:43am    
Another error.

Handles returned by CreateFile() must be closed with CloseHandle().
Member 12701845 25-Aug-16 9:46am    
That was it.
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900