CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2011
    Posts
    2

    Unhappy How to close DLL window before process gets detached

    Hello,

    I have a DLL

    BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
    {
    switch(ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    inj_hModule = hModule;
    break;
    /*case DLL_THREAD_ATTACH:
    break;
    case DLL_THREAD_DETACH:
    break;
    case DLL_PROCESS_DETACH:
    break;*/
    }

    return TRUE;
    }

    which starts a thread

    if(hThread == NULL) hThread=CreateThread(0, NULL, ThreadProc, NULL, NULL, NULL);

    which creates a window

    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = inj_hModule;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "snapzWindowClass";
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    if(!RegisterClassEx(&wc)) return 0;

    hWnd = CreateWindowEx(
    WS_EX_CLIENTEDGE,
    "snapzWindowClass",
    "vfwmex",
    WS_OVERLAPPEDWINDOW,
    0, 0,
    capture_w, capture_h,
    NULL, NULL, inj_hModule, NULL);
    if(hWnd == NULL) return 0;

    . Sending the WM_CLOSE message works fine

    LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    switch(msg)
    {
    case WM_CLOSE:
    capPreview(hWndCap, FALSE);
    capDriverDisconnect(hWndCap);
    DestroyWindow(hWndCap);
    DestroyWindow(hWnd);
    hThread = NULL;
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hWnd, msg, wParam, lParam);
    }

    return 0;
    }

    . But how do I close the window/terminate the thread before the process gets detached from the DLL? Sending the WM_CLOSE message on DLL_PROCESS_DETACH didn't have the desired effect.
    Attached Files Attached Files

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: How to close DLL window before process gets detached

    Dll must have exported functions/classes. Define some function (or class method) which makes all necessary cleanup. If Dll client doesn't call this function when it is necessary, Dll cannot cleanup its resources properly. Anyway, in the end of the process everything is destroyed.

  3. #3
    Join Date
    May 2011
    Posts
    2

    Thumbs up Re: How to close DLL window before process gets detached

    Thank you for the hint! The DLL is used by Matlab, it needs to export this function

    http://amath.colorado.edu/computing/...me=mexFunction

    and register the cleanup function:

    http://amath.colorado.edu/computing/...it.html#175175

    Thanks again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured