0
votes

I have C++ dll that using by another software. I need when software call one function of this dll , it shows one Dialog for get user/pwd. My code before worked but in this time not works.

    INT_PTR CALLBACK DialogFunc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
        g_hInstance = hModule;
        bRun = FALSE;
        dwNewLong = 0;
        g_Msg = RegisterWindowMessageA("MetaTrader4_Internal_Message");
        g_hWnd = FindWindowA("TForm1", "Form1");
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

HWND hWnd1;
int nRet;


char b1[]="VALID";


HWND g_hDlg2,hWnd,hDlg2,hDlg3;
INT_PTR CALLBACK DialogFunc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{


    switch(uMsg)
    {
    case WM_INITDIALOG:

        read(f1);
        read1(f2);

        hDlg1=GetDlgItem(hDlg,IDC_EDIT1);
        SetWindowTextA(hDlg1,UserSaved);
        hDlg2=GetDlgItem(hDlg,IDC_EDIT2);
        SetWindowTextA(hDlg2,PWDSaved);
        break;
    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
        case IDC_BUTTON2:

            hDlg3=hDlg;

            GetWindowText(hDlg1,username,1024);

            write(f1);

            GetWindowText(hDlg2,key,1024);
            write1(f2);
            if(memcmp(Get_Account_Number_FromWEB(),b1,sizeof(b1))==0 ){result=1; DestroyWindow(hDlg);}
            else MessageBox(0,TEXT("Wrong User or PWD"),0,0);

            break;
            case IDC_BUTTON3:
                DestroyWindow(hDlg);
                break;
                case IDC_EDIT1:
                 break;
            case IDC_EDIT2:
              break;

        }
        break;
    case WM_CLOSE:
        SetWindowLongA(hDlg, GWL_WNDPROC, dwNewLong);
        DestroyWindow(hDlg);
        return TRUE;
        break;
    }
    return FALSE;
}
int aa=0;
HWND hWnd2;
int dd;
HWND hDlg;
MT4_EXPFUNC int __stdcall INITIAL(HWND hWnd)
{

    MSG msg; 
    BOOL msgstatus;

     if(aa==0){
    hDlg = CreateDialogParamA(g_hInstance, MAKEINTRESOURCE(IDD_DIALOG1 ), hWnd, DialogFunc, 0);
     SetWindowPos( hDlg, HWND_TOPMOST,0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | WS_EX_TOPMOST );
     ShowWindow(hDlg, SW_SHOW);

    aa=1;
    }



    dd=result;
    hWnd2=hWnd;

    return  result;
}

I added resource to dll too and compile without problem. but when call this function it not works and not shows dialog box. Any bug in my code? Regards,

1
CreateDialogParam does work. Yes there is a bug in your code. What debugging have you done so far? I cannot see any checking of return values in your code. Why not?David Heffernan
It must return result value, 0 or 1. as I said before this code worked for me. I compiled as Debug & Release mode and try dll, but not works again.user2769769
No, you are calling many many Win32 API calls and failing to check the return value. Step 1 is to add error handling to your code.David Heffernan
Your extract is a little too extracted... It would help if you either removed the stuff not relating to the CreateDialog() function, or provided some of the other code to explain what is happening. eg. The read and write functions could have any side effect which may be cause your problems.Strings

1 Answers

2
votes

CreateDialogParam() does work, but is not required in your example as you are not attempting to pass a parameter to the WM_INITPARAM wndproc handler. So a better function would be CreateDialog().

And it seems you want "perhaps a login screen" that would be modal, therefore the correct function would probably be

DialogBox(hInstance, lpTemplate, hWndParent, lpDialogFunc)

It's hard to offer much more help than this because your question is jumbled

  1. I don't think that code ever worked there are so many errors in it
  2. It's not clear why you want it in a DLL.
  3. The actual security "check" (which is totally insecure) doesn't even accept the user as a parameter.

Perhaps you should start a little simpler and just create your dialog box (whatever it's eventual use will be) in a WinMain() executable.

Once you really have got that working, then, if you still can't transfer the code to a DLL then come back and ask a more focused question.