-----------------------------------------------------------

New Message on cochindotnet

-----------------------------------------------------------
From: J.K.
Message 6 in Discussion

Hi Rosh,

>It would be helpful if someone could tell me how to write
>a win32 c/cpp console program that can change the current
>folder of the shell window where it is called ?


I am afraid a general purpose solution is simply not available.... win32 
applications maintain a per process current directory.

There are many fixes... like using a batch file for example, but no simple 
method as such to perform this under win32.

But I have given below a rather different method to achieve what you are 
looking for..., the pros are that it doesn't require any extra batch or dos 
exe file to function. The cons this method in its present form would only 
be suitable for a console program that would terminate immediately after 
executing this function. Also expect a few focus issues which can happen. 
Although I have not included the specific code for that, it is advisable to 
set the focus or attach the input for the console thread before performing 
this, otherwise the keys might end up in the wrong window :-).

In a nutshell, the function uses the keybd_event() func to send a "cd dir" 
command and then terminates. This results in a change directory being 
executed by the dos prompt and you end up where you wanted to be :-).

The code in the present form has been tested working under 98/nt/2000. It 
should work under the other versions of windows also. Hope this does 
provide a solution to what you are looking for...


#include <windows.h>
#include <stdio.h>

void SendKeypress (char ch)
{
         unsigned short int vk, sc;

         vk = VkKeyScan(ch);
         sc = MapVirtualKey(vk, 0);

         if (vk & 0x0100)
                 keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT,0), 0,0);
         if (vk & 0x0200)
                 keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL,0), 0,0);
         if (vk & 0x0400)
                 keybd_event(VK_MENU, MapVirtualKey(VK_MENU,0), 0,0);

         keybd_event((unsigned char)vk, (unsigned char)sc, 0, 0);
         keybd_event((unsigned char)vk, (unsigned char)sc, KEYEVENTF_KEYUP, 0);

         if (vk & 0x0100)
                 keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT,0), 
KEYEVENTF_KEYUP,0);
         if (vk & 0x0200)
                 keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL,0), 
KEYEVENTF_KEYUP,0);
         if (vk & 0x0400)
                 keybd_event(VK_MENU, MapVirtualKey(VK_MENU,0), 
KEYEVENTF_KEYUP,0);

}

bool SetConsoleDir (char *pDir)
{
         int i;
         char Buffer[256];

         sprintf(Buffer, "cd %s\r", pDir);

         for (i=0; Buffer[i] != '\0'; ++i)
                 SendKeypress(Buffer[i]);
         return TRUE;
}

int main (int argc, char* argv[])
{
         if (argc != 2)
                 return 1;

         SetConsoleDir (argv[1]);
         return 0;
}



Have a nice day,

Jayakrishnan K [MVP - Visual C++]
(Director)
Xtend Technologies (P) Ltd.  Cochin.



-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/cochindotnet/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you received 
this message by mistake, please click the "Remove" link below. On the pre-addressed 
e-mail message that opens, simply click "Send". Your e-mail address will be deleted 
from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to