Click here to Skip to main content
15,895,142 members
Articles / Programming Languages / C++

Winlogon using Mobile Disk

Rate me:
Please Sign up or sign in to vote.
4.83/5 (25 votes)
30 Nov 2007CPOL6 min read 136.7K   2.6K   89  
This is a full set of applications that can be used to logon to Windows system using mobile disk. No password typing.
#include "windows.h"
#include "resource.h"
#include "Dbt.h"
#include "..\UserInfo.h"

extern char g_lpUserName[30], g_lpPassword[30], g_lpDomain[100];
extern int g_nLoginType;

void SystemLog(const char *msg);

VOID CenterWindow( HWND    hwnd )
{
    RECT    rect;
    LONG    dx, dy;
    LONG    dxParent, dyParent;
    LONG    Style;

    // Get window rect
    GetWindowRect(hwnd, &rect);

    dx = rect.right - rect.left;
    dy = rect.bottom - rect.top;

    // Get parent rect
    Style = GetWindowLong(hwnd, GWL_STYLE);
    if ((Style & WS_CHILD) == 0) {

        // Return the desktop windows size (size of main screen)
        dxParent = GetSystemMetrics(SM_CXSCREEN);
        dyParent = GetSystemMetrics(SM_CYSCREEN);
    } else {
        HWND    hwndParent;
        RECT    rectParent;

        hwndParent = GetParent(hwnd);
        if (hwndParent == NULL) {
            hwndParent = GetDesktopWindow();
        }

        GetWindowRect(hwndParent, &rectParent);

        dxParent = rectParent.right - rectParent.left;
        dyParent = rectParent.bottom - rectParent.top;
    }

    // Centre the child in the parent
    rect.left = (dxParent - dx) / 2;
    rect.top  = (dyParent - dy) / 3;

    // Move the child into position
    SetWindowPos(hwnd, HWND_TOPMOST, rect.left, rect.top, 0, 0, SWP_NOSIZE);

    SetForegroundWindow(hwnd);
}

int CALLBACK DisplaySASNoticeDlgProc(
    HWND    hDlg,
    UINT    Message,
    WPARAM  wParam,
    LPARAM  lParam)
{

	PDEV_BROADCAST_HDR pheadBC;
	PDEV_BROADCAST_VOLUME pVol;


	HWND hMsg=GetDlgItem(hDlg,IDC_STATUS_STATIC);

	CUserInfo ui;
	int drv;
	char lpPath[255];
//    pGlobals = (PGlobals) GetWindowLong(hDlg, GWL_USERDATA);
    switch (Message)
    {
        case WM_INITDIALOG:
            CenterWindow(hDlg);
				

				drv=ui.ValidDiskPresent();
						
				if(drv)
				{
					sprintf(lpPath,"%c:\\%s",'A'+drv,DISK_FILE_NAME);

					USER_INFO user_info=ui.LoadUserInfo(lpPath);
					user_info=ui.Decode(user_info);

					strcpy(g_lpUserName,user_info.lpWindowsUser);
					strcpy(g_lpPassword,user_info.lpWindowsPassword);
					
					if(strlen(user_info.lpDomain))
						strcpy(g_lpDomain,user_info.lpDomain);
					else
					{
						strcpy(g_lpDomain,".");
					}

					g_nLoginType=user_info.dwLogonType;

					EndDialog(hDlg, IDC_LOGON_BUTTON);
				}
            return(TRUE);

        case WM_COMMAND:

			if (LOWORD(wParam) == IDOK)
            {
                //EndDialog(hDlg, IDCANCEL);
            }
            
			if (LOWORD(wParam) == IDCANCEL)
            {
                //EndDialog(hDlg, IDCANCEL);
            }

			if(LOWORD(wParam)==IDC_SHUT_DOWN_STATIC)
			{
				EndDialog(hDlg, IDCANCEL);
			}
            return(TRUE);

		case WM_DEVICECHANGE:
			Sleep(2000);

			pheadBC=(PDEV_BROADCAST_HDR)lParam;

			if(wParam==DBT_DEVICEARRIVAL && pheadBC->dbch_devicetype==DBT_DEVTYP_VOLUME)
			{
				pVol=(PDEV_BROADCAST_VOLUME)lParam;

				drv=ui.ValidDiskPresent();
				lpPath[255];
		
				if(drv)
				{
					sprintf(lpPath,"%c:\\%s",'A'+drv,DISK_FILE_NAME);

					USER_INFO user_info=ui.LoadUserInfo(lpPath);
					user_info=ui.Decode(user_info);

					strcpy(g_lpUserName,user_info.lpWindowsUser);
					strcpy(g_lpPassword,user_info.lpWindowsPassword);
					if(strlen(user_info.lpDomain))
						strcpy(g_lpDomain,user_info.lpDomain);
					else
					{
						strcpy(g_lpDomain,".");
					}

					g_nLoginType=user_info.dwLogonType;


					EndDialog(hDlg, IDC_LOGON_BUTTON);
				}
				else
				{
					//SetDlgItemText(hDlg,IDC_STATUS_STATIC,"");
				}
				//

			}
			
			break;
    }
    return(FALSE);
}



int CALLBACK UpdateWindowsUserDlgProc(
    HWND    hDlg,
    UINT    Message,
    WPARAM  wParam,
    LPARAM  lParam)
{

	char lpUserName[100], lpPassword[100];

	HWND hWnd=GetDlgItem(hDlg,IDC_DOMAIN_COMBO); 

//    pGlobals = (PGlobals) GetWindowLong(hDlg, GWL_USERDATA);
    switch (Message)
    {
        case WM_INITDIALOG:
            CenterWindow(hDlg);
			SendMessage(hWnd,CB_INSERTSTRING,(WPARAM) 0, (LPARAM)"This Computer");

			SendMessage(hWnd, CB_SETCURSEL, (WPARAM)0 ,(LPARAM)0);

            //SetWindowLong(hDlg, GWL_USERDATA, lParam);

            return(TRUE);

        case WM_COMMAND:

			if (LOWORD(wParam) == IDOK)
            {
				GetDlgItemText(hDlg,IDC_USER_EDIT,lpUserName,99);
				GetDlgItemText(hDlg,IDC_PASSWORD_EDIT,lpPassword,99);

				strcpy(g_lpUserName, lpUserName);
				strcpy(g_lpPassword, lpPassword);




				EndDialog(hDlg, IDOK);
            }
            
			if (LOWORD(wParam) == IDCANCEL)
            {
                //EndDialog(hDlg, IDCANCEL);
            }
            return(TRUE);
			
			break;
    }
    return(FALSE);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Microsoft
United States United States
Have completed BSc in Computer Science & Engineering from Shah Jalal University of Science & Technology, Sylhet, Bangladesh (SUST).

Story books (specially Masud Rana series), tourism, songs and programming is most favorite.

Blog:
Maruf Notes
http://blog.kuashaonline.com

Comments and Discussions