Thursday, July 30, 2009

How to close a notepad window using win32 api in c#?

this is not working


[DllImport("user32")]


public static extern int CloseWindow(int hwnd);


Process[] p = Process.GetProcessesByName("Notepad");


int hWnd = (int)p[0].MainWindowHandle;


CloseWindow(hWnd);

How to close a notepad window using win32 api in c#?
i think windows does not allow this. it is not nice to close other application's windows, no?





but it is possible to forcibly close an application which is launched by you. you will need these APIs:





// to launch an application:





BOOL WINAPI CreateProcess(


LPCTSTR lpApplicationName,


LPTSTR lpCommandLine,


LPSECURITY_ATTRIBUTES lpProcessAttributes,


LPSECURITY_ATTRIBUTES lpThreadAttributes,


BOOL bInheritHandles,


DWORD dwCreationFlags,


LPVOID lpEnvironment,


LPCTSTR lpCurrentDirectory,


LPSTARTUPINFO lpStartupInfo,


LPPROCESS_INFORMATION lpProcessInformation


);





// and





BOOL WINAPI TerminateProcess(


HANDLE hProcess,


UINT uExitCode


);


No comments:

Post a Comment