Hi everyone!
My function UsbID uses the libusb library (http://www.libusb.org/) to get a flash disk's serial number.
Libusb needs for some reason a Window created or else it doesn't work properly for me.
So i created a CWnd inside my UsbID function which is invisible.
My problem is that if i Destroy and Unregister this window after my job is done, i am getting an -1 return on a following CDialog:: DoModal() function call.
The CDialog has nothing to do with the previous mentioned CWnd.
If i don't Destroy and Unregister the window everything works fine. :S
Please take a look at the followin code and tell me if something is missing or incorrect.
char* UsbID(int len, HINSTANCE appinst)//appinst is the HINSTANCE of main application
{
////////////////////////////////////////////////////////////////////////////////
//CREATE A WINDOW WHICH IS INVISIBLE
HINSTANCE instance=appinst;
WNDCLASSEX win_class;
win_class.cbSize = sizeof(WNDCLASSEX);
win_class.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS ;
win_class.lpfnWndProc = win_proc1;
win_class.cbClsExtra = 0;
win_class.cbWndExtra = 0;
win_class.hInstance = instance;
win_class.hIcon = NULL;
win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
win_class.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
win_class.lpszMenuName = NULL;
win_class.lpszClassName = "test";
win_class.hIconSm = NULL;
RegisterClassEx(&win_class);
main_win = CreateWindowEx(WS_EX_APPWINDOW| WS_EX_CONTROLPARENT,
"test",
"TestLibUsb - Windows Version",
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN
| WS_DLGFRAME,
CW_USEDEFAULT, 0, 500, 500, NULL, NULL,
instance, NULL);
if (!main_win)
{
return FALSE;
}
ShowWindow(main_win, SW_HIDE);
//
////////////////////////////////////////////////////////////////////////////////
char *tt;
tt=(char *)malloc(len);
//LIB USB FUNCTIONS
usb_set_debug(4);
usb_init();
usb_find_busses();
char instr[5000];
get_usbID(instr,sizeof(instr));
//... OTHER CODE...//
///////////////////////////////////////
//DESTROY WINDOW
int error = DestroyWindow(main_win);
int error2 = UnregisterClass("test", instance);
//
///////////////////////////////////////
return tt;
}
And this is the main app:
BOOL MyApp::InitInstance()
{
//... OTHER CODE..//
//call UsbID
UsbID(8,this->m_hInstance);
//... OTHER CODE..//
CSelectSite sdlg;
int sResponse=sdlg.DoModal(); //FAIL
//
sResponce = -1 IF I DESTROY AND UNREGISTER WINDOW and DoModal fails.
Else DoModal WORKS!
The problem is that i need to call UsbID several times and it isn't appropriate to Register a new Window all the times without destroying it.
Thanks in advance...