can't fix error message

trying to write computer program to communicate with usb port. i keep getting error message unhandled exception at 0x70FA1650 (winusb.dll) in Consoleapplication3.exe: 0xC0000005: access violation writing location 0x00000000.

my code is

#include "stdafx.h"
#include "ConsoleApplication3.h"
#include "winusb.h"
#include "Setupapi.h"
#include "Usbiodef.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
HDEVINFO pdev;
GUID uusb;
uusb.Data1 = 0xA5DCBF10;
uusb.Data2 = 0x6530;
uusb.Data3 = 0x11D2;
uusb.Data4[0] = 0x90;
uusb.Data4[1] = 0x1F;
uusb.Data4[2] = 0x00;
uusb.Data4[3] = 0xC0;
uusb.Data4[4] = 0x4F;
uusb.Data4[5] = 0xB9;
uusb.Data4[6] = 0x51;
uusb.Data4[7] = 0xED;
SP_DEVICE_INTERFACE_DATA intadata;
ULONG rulength = 0;
PSP_DEVICE_INTERFACE_DETAIL_DATA dutaildata;
dutaildata = NULL;
ULONG lunghth;
HANDLE rr;
PWINUSB_INTERFACE_HANDLE usbhan = 0;

pdev = SetupDiGetClassDevs(&GUID_DEVINTERFACE_USB_DEVICE, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
intadata.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
SetupDiEnumDeviceInterfaces(pdev, NULL, &GUID_DEVINTERFACE_USB_DEVICE, 0, &intadata);
SetupDiGetDeviceInterfaceDetail(pdev, &intadata, NULL, 0, &rulength, NULL);
dutaildata = (PSP_DEVICE_INTERFACE_DETAIL_DATA)
LocalAlloc(LMEM_FIXED, rulength);
dutaildata->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
lunghth = rulength;
SetupDiGetDeviceInterfaceDetail(pdev, &intadata, dutaildata, lunghth, &rulength, NULL);

rr = CreateFile(dutaildata->DevicePath, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
WinUsb_Initialize(rr, usbhan);
int nRetCode = 0;

HMODULE hModule = ::GetModuleHandle(NULL);

if (hModule != NULL)
{
// initialize MFC and print and error on failure
if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
}
}
else
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
nRetCode = 1;
}

return nRetCode;
}

after i run the code in window named Autos

it says

dutaildata 0x007b72b8 {cbSize=6 DevicePath=0x007b72bc L"\\\\?\\usb#vid_0c45&pid_6450#6&2bc2ca&0&8#{a5dcbf10-6530-11d2-901f-00c04fb951ed}" } _SP_DEVICE_INTERFACE_DETAIL_DATA_W *

nRetCode -858993460 int

rr 0x000000e0 void *

+ usbhan 0x00000000 {???} void * *

what is wrong with my code? is there a bug somewhere?
OP's code, formatted and put into code blocks:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "ConsoleApplication3.h"
#include "Setupapi.h"
#include "Usbiodef.h"
#include "stdafx.h"
#include "winusb.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR *argv[], TCHAR *envp[]) {
  HDEVINFO pdev;
  GUID uusb;
  uusb.Data1 = 0xA5DCBF10;
  uusb.Data2 = 0x6530;
  uusb.Data3 = 0x11D2;
  uusb.Data4[0] = 0x90;
  uusb.Data4[1] = 0x1F;
  uusb.Data4[2] = 0x00;
  uusb.Data4[3] = 0xC0;
  uusb.Data4[4] = 0x4F;
  uusb.Data4[5] = 0xB9;
  uusb.Data4[6] = 0x51;
  uusb.Data4[7] = 0xED;
  SP_DEVICE_INTERFACE_DATA intadata;
  ULONG rulength = 0;
  PSP_DEVICE_INTERFACE_DETAIL_DATA dutaildata;
  dutaildata = NULL;
  ULONG lunghth;
  HANDLE rr;
  PWINUSB_INTERFACE_HANDLE usbhan = 0;

  pdev = SetupDiGetClassDevs(&GUID_DEVINTERFACE_USB_DEVICE, NULL, NULL,
                             DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
  intadata.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
  SetupDiEnumDeviceInterfaces(pdev, NULL, &GUID_DEVINTERFACE_USB_DEVICE, 0,
                              &intadata);
  SetupDiGetDeviceInterfaceDetail(pdev, &intadata, NULL, 0, &rulength, NULL);
  dutaildata =
      (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LMEM_FIXED, rulength);
  dutaildata->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  lunghth = rulength;
  SetupDiGetDeviceInterfaceDetail(pdev, &intadata, dutaildata, lunghth,
                                  &rulength, NULL);

  rr = CreateFile(dutaildata->DevicePath, GENERIC_WRITE | GENERIC_READ,
                  FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING,
                  FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
  WinUsb_Initialize(rr, usbhan);
  int nRetCode = 0;

  HMODULE hModule = ::GetModuleHandle(NULL);

  if (hModule != NULL) {
    // initialize MFC and print and error on failure
    if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0)) {
      // TODO: change error code to suit your needs
      _tprintf(_T("Fatal Error: MFC initialization failed\n"));
      nRetCode = 1;
    } else {
      // TODO: code your application's behavior here.
    }
  } else {
    // TODO: change error code to suit your needs
    _tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
    nRetCode = 1;
  }

  return nRetCode;
}

access violation writing location 0x00000000

I'm not a Windows programmer, but that looks an awful lot like you tried to dereference a null pointer. Do any of the functions you used return null/set a pointer to null on failure?

-Albatross
I dereferenced a null pointer? Where? I used visual studios 2013 to write this code. I have written console
Applications and win32 applications a lot before. I have
Some experience writing computer programs.
All self-taught. I have never done usb programming.
If I don't set usbhan to null or 0, I will get an error message saying uninitialized local variable urban.
@Monkey1999,


1
2
3
PWINUSB_INTERFACE_HANDLE usbhan = 0;

WinUsb_Initialize(rr, usbhan);


With nothing between to change it, usbhan is zero (null) at this call to WinUsb_Initialize


WinUsb_Initialize(rr, usbhan);

The docs give this on that function:

1
2
3
4
BOOL WinUsb_Initialize(
  HANDLE                   DeviceHandle,
  PWINUSB_INTERFACE_HANDLE InterfaceHandle
);


Note the "PWINUSB...."

The "P" indicates this is a pointer, but in your call you've given it the value. Microsoft's API does not usually type check these things tightly, so it is accepting the value of usbhan (which is 0) as the address of the handle.

What I believe is required is to provide the address of usbhan, as in:

WinUsb_Initialize(rr, &usbhan);

So that Windows can use this as an output, to give usbhan the value of the handle obtained by this function call.
Last edited on
I will see if that works.
i have tried writing WinUsb_Initialize(rr, &usbhan). i get an error message that says
argument of type PWINUSB_INTERFACE_HANDLE* is incompatible with a parameter of type
PWINUSB_INTERFACE_HANDLE. i have been unable to get rid of it.
@Monkey1999,

Where you declare usbhan, change PWINUSB_INTERFACE_HANDLE to WINUSB_INTERFACE_HANDLE

What you declared is the pointer to a handle.

What you need is a handle.

Remove the "P"
Last edited on
https://docs.microsoft.com/en-us/windows/win32/api/winusb/nf-winusb-winusb_initialize
The handle received in the InterfaceHandle parameter is a pointer to the memory block allocated for the first interface in the array.


Even though you seem to have missed the fact niccolo mentioned that the P means "pointer" so what you are doing is WINUSB_INTERFACE_HANDLE**. I would have also recommended zeroing the data, but it turns out that WINUSB_INTERFACE_HANDLE is a pointer (I forgot its a handle after all), so all you need to do is just remove the P in your code (I THINK). I don't write drivers so I cannot help you beyond this trivial error, seek help on some hardware forum, there has to be a section of stackoverflow for hardware if you are really lost.

Your lack of error handling is pitiful, don't ever remove them even for readability. Check every win32 function you use in msdn and check if it returns a GetLastError and also google how to get a error string from GetLastError. A nitpick but I don't see why you use namespace std when you also use ::GetModuleHandle, if you like signaling that something is a win32 function or a std function or not, you should just stick to one style (even though you aren't using any C++, and using namespace std is quite rare for experienced programmers not writing tutorials, my observation and opinion).

Also note you have quite a few memory leaks, there is no excuse.

I looked a tiny bit into examples, in here I found something called usbsamp sample, and it seems to be a command line operated example that shows things you can do with the usb with proper error checking, vs2012, and it doesn't use winusb.h since from reading it is removed in version 8 or something. So yea, still worth using this code as your cross reference.
https://code.msdn.microsoft.com/windowshardware/Windows-Driver-Kit-WDK-80-e3161626/sourcecode?fileId=92302&pathId=2016597265
I will see if that works.
I gotta ask, are you trying to write an MFC app or a console app?

Using _tmain() as your entry point is not MFC by a long shot.

MFC doesn't have a main()/_tmain() or even a WinMain(). It is hidden by the framework.

Here's some source for a minimal (somewhat) MFC app (note, no function for the program's entry point):

http://www.equestionanswers.com/vcpp/basic-steps-mfc-application.php

It is possible to create a console app that uses MFC, but your source doesn't appear to be doing that.

https://www.codeguru.com/cpp/misc/misc/consoleapps/article.php/c243/Console-app-using-MFC.htm

An old (2009) PDF on using WinUsb:

http://caxapa.ru/thumbs/161376/WinUsb_HowTo.pdf

Look at page 16 for how to initialize WinUsb, reading the rest of the document as well.
Last edited on
I am using a win32 console app, but I think removing the p worked.
I am using a win32 console app

Line 13 is how an MFC app is instantiated, that is not part of a standard console app.
Topic archived. No new replies allowed.