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...

Do you really want WS_EX_APPWINDOW?

Don't really know if WS_EX_APPWINDOW is needed.
Will try to remove it and see what's happening.

Will post back.
Thanks

Well that wasn't that easy. Replace WS_EX_APPWINDOW with what?

Anyway i foud another way round.
I made the window handler global, and now i check everytime i call UsbID() if it exists so that no new window is created every time.

But this is just a trick...
I would really like to know why dialogs which are invisible behave that way.
It is not the first time i ran into this problem. Invisible windows always mess things up.

If someone has any other idea of how to properly create and destroy invisible dialogs please help.

Otherwise i will just mark the topic as solved in a couple of days.

Thanks in advance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.