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

    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;


    main_win = CreateWindowEx(WS_EX_APPWINDOW| WS_EX_CONTROLPARENT,
                              "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);

	char instr[5000];

//... OTHER CODE...//

	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

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

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

Will post back.

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.

Edited 6 Years Ago by ktsangop: n/a

This question has already been answered. Start a new discussion instead.