GUI in DLL

 
// ZTest.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"

HWND g_hWnd = NULL;
HRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
DWORD WINAPI Run( void* pParam );

extern "C" __declspec( dllexport ) void Start() {
        WNDCLASSEX      wc      = { 0 };
        HINSTANCE       hInstance = GetDLLHandle();

        wc.cbSize                       = sizeof( WNDCLASSEX );
        wc.hbrBackground        = static_cast< HBRUSH >( ::GetStockObject( WHITE_BRUSH ) );
        wc.hCursor                      = ::LoadCursor( hInstance, IDC_ARROW );
        wc.hIcon                        = ::LoadIcon( hInstance, IDI_APPLICATION );
        wc.hIconSm                      = wc.hIcon;
        wc.hInstance            = hInstance;
        wc.lpfnWndProc          = &WndProc;
        wc.lpszClassName        = CLASS_NAME;
        wc.lpszMenuName         = NULL;
        wc.style                        = CS_HREDRAW | CS_VREDRAW;

        if ( !::RegisterClassEx( &wc ) ) {
                ::MessageBox( NULL, TEXT( "Window class register failed." ), TEXT( "Error" ), MB_OK );
                return;
        }

        if ( g_hWnd = ::CreateWindowEx(
                                                                        0,
                                                                        CLASS_NAME,
                                                                        TEXT( "ZTest" ),
                                                                        WS_OVERLAPPEDWINDOW,
                                                                        CW_USEDEFAULT,
                                                                        CW_USEDEFAULT,
                                                                        300,
                                                                        200,
                                                                        NULL,
                                                                        NULL,
                                                                        hInstance,
                                                                        NULL
                                                                        )
                ) {
                ::ShowWindow( g_hWnd, SW_SHOW );
                ::UpdateWindow( g_hWnd );
        } else
                ::MessageBox( NULL, TEXT( "Window creation failed." ), TEXT( "Error" ), MB_OK );

        ::CreateThread( NULL, 0, Run, NULL, 0, NULL );
}

extern "C" __declspec( dllexport ) void End() {
        if ( g_hWnd )
                ::DestroyWindow( g_hWnd );
}

DWORD WINAPI Run( void* pParam ) {
        MSG msg = { 0 };

        while ( msg.message != WM_QUIT ) {
                while ( ::PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) {
                        ::TranslateMessage( &msg );
                        ::DispatchMessage( &msg );
                }
        }

        return 0;
}

HRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) {
        switch ( uMsg ) {
        default:
                return ::DefWindowProc( hWnd, uMsg, wParam, lParam );
        }

        return 0;
}

this is gui in dll, if i do call the exported function using normal test exe file, it works fine. no problem, but calling it in customer indicator, the gui is started after call, but it freeze. I can not move the gui window and after some seconds metatrader 5 crashed. whats the problem? can someone help me in this case? In customer indicator I have the following source code

#property copyright "ZTest"
#property link      "https://www.mql5.com/"
#property version   "1.00"
#property indicator_separate_window // Indicator is in the subwindow
#property indicator_plots           0




#import "ZTest.dll"
   int Start();
   void End();
#import



int OnInit() {
   Start();
   
   return( INIT_SUCCEEDED );
}

void OnDeinit( const int reason ) {
   End();
}

int OnCalculate (
                  const int      rates_total,
                  const int      prev_calculated,
                  const int      begin,
                  const double&  price[]
                  ) {

   return( rates_total );
}

 
mql5programer:

this is gui in dll, if i do call the exported function using normal test exe file, it works fine. no problem, but calling it in customer indicator, the gui is started after call, but it freeze. I can not move the gui window and after some seconds metatrader 5 crashed. whats the problem? can someone help me in this case? In customer indicator I have the following source code

any luck with this?