• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    Autodesk ObjectARX

    Reply
    Active Member
    Posts: 6
    Registered: ‎08-20-2012
    Accepted Solution

    How to ask for a password on the command line of AutoCAD 2013 using ObjectARX?

    178 Views, 4 Replies
    10-06-2012 12:35 AM

    How can I ask for a password, replacing each keystroke with a generic character, on the command line of AutoCAD 2013 using ObjectARX?

    Thanks in advance.

    Please use plain text.
    Moderator
    Alexander.Rivilis
    Posts: 1,168
    Registered: ‎04-09-2008

    Re: How to ask for a password on the command line of AutoCAD 2013 using ObjectAR

    10-06-2012 02:58 AM in reply to: JoseVicario

    Try this: http://adndevblog.typepad.com/autocad/2012/06/asking-for-a-password-on-the-command-line-of-autocad-u...


    Пожалуйста не забывайте про Утвердить в качестве решения!Утвердить в качестве решения и Give Kudos!Баллы
    Please remember to Accept Solution!Accept as Solution and Give Kudos!Kudos

    Please use plain text.
    Active Member
    Posts: 6
    Registered: ‎08-20-2012

    Re: How to ask for a password on the command line of AutoCAD 2013 using ObjectAR

    10-06-2012 05:34 AM in reply to: Alexander.Rivilis

    Thanks Alexander, but the solution in adndevblog fails in AutoCAD 2013. Look the comment by Thierry Prince below the solution.

    Please use plain text.
    ADN Support Specialist
    Balaji_Ram
    Posts: 351
    Registered: ‎03-21-2011

    Re: How to ask for a password on the command line of AutoCAD 2013 using ObjectAR

    10-13-2012 09:36 AM in reply to: JoseVicario

    Hi Jose,

     

    Using hooks is a hack that is not generally advisable to use. If you have the option, switch over to a UI to take that input which you are trying to mask. That way you will not need to use hooks.

     

    However if you still need to go this route, then here is a code snippet based on the low level keyboard hooks. Hope this helps. I havent tested it very extensively but seems to work in general using AutoCAD 2013.

     

    Here is the sample code :

     

    static ACHAR password[133]; 
    static int pwdLen; 
    static HHOOK hKeyboardHook;
    
    // - AdskMyTest1.MyCommand1 command (do not rename)
    static void AdskTestCommand(void)
    {
    	ACHAR pwd[133];   
    	adskGetPassword(1, ACRX_T("Password please ?"), pwd);	
    }
    
    static int adskGetPassword(int cronly,ACHAR* prompt,ACHAR* result)
    {
    	pwdLen = 0;
    	password[pwdLen]='\0';
    	HINSTANCE hInstance = GetModuleHandle(NULL);
    	if (!hInstance) 
    		return 1;
    
    	hKeyboardHook = SetWindowsHookEx ( 
    										WH_KEYBOARD_LL, 
    										(HOOKPROC) KeyboardEvent, 
    										hInstance, 
    										NULL 
    										);
    		
    	ACHAR tempResult[133];
    	int ret = acedGetString(cronly, prompt, tempResult);
    
    	UnhookWindowsHookEx(hKeyboardHook);
    
    	if(ret != RTNORM)
    		acutPrintf(ACRX_T("\nCancelled."));
    	else
    	{
    		password[++pwdLen] = '\0';
    		acutPrintf(password);
    	}
    
    	wcscpy(result, password);
    
    	return ret;
    }
    
    static LRESULT CALLBACK KeyboardEvent (int nCode, WPARAM wParam, LPARAM lParam)
    {
    	if ((nCode == HC_ACTION) && 
    	((wParam == WM_SYSKEYDOWN) || 
    	(wParam == WM_KEYDOWN))) 
    	{
    		KBDLLHOOKSTRUCT hooked_key = *((KBDLLHOOKSTRUCT*)lParam);
    		DWORD dwMsg = 1;
    		dwMsg += hooked_key.scanCode << 16;
    		dwMsg += hooked_key.flags << 24;
    		int key = hs.vkCode;
    		if ((key >= 'A' && key <= 'Z')  || (key >= '0' && key <= '9'))
    		{
    			GetKeyNameText(dwMsg, &password[pwdLen], 0xFF) + 1;
    			password[++pwdLen] = '\0';
    			keybd_event(VK_MULTIPLY, hooked_key.scanCode, hooked_key.flags, 0);
    			return 1;
    		}
    		else if(hooked_key.vkCode == VK_BACK)
    		{
    			password[--pwdLen] = '\0';
    		}
    	}
    	return CallNextHookEx(hKeyboardHook, nCode,wParam,lParam);
    }

     



    Balaji
    Developer Technical Services
    Autodesk Developer Network

    Please use plain text.
    Active Member
    Posts: 6
    Registered: ‎08-20-2012

    Re: How to ask for a password on the command line of AutoCAD 2013 using ObjectAR

    10-13-2012 11:05 AM in reply to: Balaji_Ram

    Thanks Balaji for your reply. The code works well with some corrections:

    The line

    int key = hs.vkCode;

    must be

    int key = hooked_key.vkCode;

    and the line

    GetKeyNameText(dwMsg, &password[pwdLen], 0xFF) + 1;

    must be

    GetKeyNameText(dwMsg, &password[pwdLen], 0xFF);

    Please use plain text.