Wednesday, October 6, 2010

Set DateFormat

We can set current application's date style using this in C++/CLI

System::Threading::Thread^ pThread;
System::Globalization::CultureInfo^ pCultureInfo;
pThread = System::Threading::Thread::CurrentThread;
pCultureInfo = (System::Globalization::CultureInfo^)pThread->CurrentCulture->Clone();
pCultureInfo->DateTimeFormat->ShortDatePattern = "dd.MM.yyyy";
pThread->CurrentCulture = pCultureInfo;

In WPF application, the same thing is doing
like

CultureInfo culutreInfo = System.Threading.Thread.CurrentThread.CurrentCulture.Clone() as CultureInfo;
culutreInfo.DateTimeFormat.ShortDatePattern = "MMM dd/yyyy";
System.Threading.Thread.CurrentThread.CurrentCulture = culutreInfo;

or

Thread.CurrentThread.CurrentCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";

Also we can set a particular datepicker's date format like this,

CultureInfo cultureInfo = new CultureInfo("en-US");
DateTimeFormatInfo dateInfo = new DateTimeFormatInfo();
dateInfo.ShortDatePattern = "MM/yyyy";
cultureInfo.DateTimeFormat = dateInfo;
radDatePicker1.Culture = cultureInfo;


or

CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
culture.DateTimeFormat.ShortDatePattern = "MMM/dd/yyyy";
datePicker1.Culture = culture;

Sunday, September 19, 2010

Call a new function from button click with arguments in C++/CLI

//FirstWindow.h
#pragma once

using namespace System; using namespace System::Windows; using namespace System::Windows::Controls;

ref class Functor;

ref class FirstWindow : Window { Canvas^ maincanvas; Button^ addbutton1; Button^ addbutton2; Functor^ pFunctor; public: FirstWindow(void); void InitControls(void); void MyFunction( int x, int y );

};

//FirstWindow.cpp #include "FirstWindow.h" #include "Functor.h"

FirstWindow::FirstWindow(void) { Title = "First Avalon App"; Width = 400; Height = 400; ResizeMode = System::Windows::ResizeMode::NoResize;

InitControls();

}

void FirstWindow::InitControls(void) { addbutton1 = gcnew Button(); addbutton1->Width = 80; addbutton1->Height = 25; addbutton1->Content = "Add"; pFunctor = gcnew Functor(this, 10, 20); addbutton1->Click += gcnew RoutedEventHandler( pFunctor, &Functor::Handler);

    Canvas::SetTop(addbutton1, 45);
Canvas::SetLeft(addbutton1, 200);


pFunctor
= gcnew Functor(this, 100, 200);
addbutton2
= gcnew Button();
addbutton2
->Width = 80;
addbutton2
->Height = 25;
addbutton2
->Content = "Add";
addbutton2
->Click += gcnew RoutedEventHandler(pFunctor, &Functor::Handler);

Canvas::SetTop(addbutton2, 85);
Canvas::SetLeft(addbutton2, 200);


maincanvas
= gcnew Canvas();

maincanvas
->Children->Add(addbutton1);
maincanvas
->Children->Add(addbutton2);
Content = maincanvas;

}

void FirstWindow::MyFunction( int x, int y ) { MessageBox::Show("This function is call by Button Click with values " + x.ToString() + " , " + y.ToString() ); }

//Functor.h #pragma once

using namespace System; using namespace System::Windows; using namespace System::Windows::Controls;

ref class FirstWindow;

private ref class Functor { public: Functor(FirstWindow^ pFirstWindow, int pFirstArg, int pSecArg);

// This is what we will use as the handler method
void Handler(Object ^ sender, RoutedEventArgs ^ e);

private: int m_pFirstArg; int m_pSecArg; FirstWindow^ m_pFirstWindow; };

//Functor.cpp

#include "Functor.h" #include "FirstWindow.h"

Functor::Functor(FirstWindow^ pFirstWindow, int pFirstArg, int pSecArg) : m_pFirstWindow( pFirstWindow ), m_pFirstArg(pFirstArg), m_pSecArg( pSecArg ) {

}

void Functor::Handler(Object ^ sender, RoutedEventArgs ^ e) { if ( m_pFirstWindow ) m_pFirstWindow->MyFunction(m_pFirstArg, m_pSecArg );

}

Now when we click on button one, then the application call the function "MyFunction" with value 10,20 and when we click on button 2 then the same function "MyFunction" with value 100,200.

Saturday, September 11, 2010

Load Controls from WPF window using C++/CLI

void FindChildren(System::Windows::DependencyObject^ source, List^ pList)
{
System::Collections::Generic::List^ pDependencyObject = nullptr;
pDependencyObject = gcnew System::Collections::Generic::List();

if (source != nullptr)
{
GetChildObjects(source, pDependencyObject);

for each ( System::Windows::DependencyObject^ child in pDependencyObject)
{
if ( dynamic_cast(child) )
{
//MessageBox::Show ( "Yes Add" + child->GetType()->ToString());
pList->Add( (System::Windows::Controls::Control^)child );
}
//else
//MessageBox::Show ( "Not need" + child->GetType()->ToString());

}
}

}

void GetChildObjects(System::Windows::DependencyObject^ parent, System::Collections::Generic::List^ pDependencyObject)
{
bool bContentElement = false;
bool bFrameworkElement = false;

if (parent == nullptr)
return;

if ( dynamic_cast(parent) )
bContentElement = true;
else
bContentElement = false;


if ( dynamic_cast(parent) )
bFrameworkElement = true;
else
bFrameworkElement = false;

if ( ( bContentElement == true ) || ( bFrameworkElement == true ) )
{
for each (System::Object^ obj in System::Windows::LogicalTreeHelper::GetChildren(parent))
{
System::Windows::DependencyObject^ depObj = nullptr;
if ( dynamic_cast(obj) )
depObj = (System::Windows::DependencyObject^)obj;

if (depObj != nullptr)
{
pDependencyObject->Add( (System::Windows::DependencyObject^)obj );
//MessageBox::Show( ((System::Windows::DependencyObject^)obj)->GetType()->ToString() );
GetChildObjects( (System::Windows::DependencyObject^)obj, pDependencyObject );
}
}
}
}


call the function

List^ pControlList = nullptr;
pControlList = gcnew List();

FindChildren(pDialog, pControlList); //Load controls from dialog

for each ( System::Windows::Controls::Control^ pControl in pControlList )
{
//your checking
}
My Blog - Windows Live: "- Sent using Google Toolbar"

Monday, April 19, 2010

Wake On LAN

General

How can I start a computer over the network?

There are two different procedures, depending on whether the target computer is in standby/sleep/suspend mode or whether it is actually powered down in hibernate mode or entirely off.

Note that you cannot wake a computer that is powered down over a WLAN connection. Only cable connections will do.

Waking from suspend mode

  1. On the target computer in Device Manager or in the network settings select the network card.
  2. Click on: Properties
  3. Click on the configure button of the network card.
  4. Select the tab: Power management
  5. Set a check mark at: Allow this device to bring the computer out of standby
  6. Click on: OK
  7. Continue to close all property pages.

Now your computer should wake up from standby whenever the network card is accessed by any other computer. If it doesn't, check any relevant BIOS settings, but usually there aren't any.

A frequent problem is that the computer is woken up by all and sundry network traffic, hence it may be better to send the computer to hibernate mode or shut it down and off altogether and use the following method.

Wake On LAN from hibernate or off mode

One way to do this is the following.

  1. Check whether the network adapter on the target computer supports Wake On LAN. Enable the function in the network adapter settings or in any special management program that came with it. If the network adapter is integrated on the motherboard, enable Wake On LAN in the motherboard BIOS.
  2. Download and install WakeOnLanGui from www.depicus.com.
  3. On the target computer, i.e. the one to be woken up, open a command line window and enter the command: ipconfig /all
  4. Note the physical address (sometimes called MAC = Media Access Control address) of your Ethernet adapter from the ipconfig output. Example: 00-E0-18-90-A5-44
  5. Run WakeOnLanGui on the computer from which you want to wake the other computer and type the physical address into the proper field in WakeOnLanGui. The other two fields are filled with 255.255.255.255, and the port is 7 or actually any other port. Port 9 is sometimes used.
  6. Shut down the computer to be waked.
  7. Test WakeOnLanGui.

If you need the opposite function as well, to shut down a computer remotely, have a look at:

http://www.microsoft.com/technet/sysinternals/utilities/PsShutdown.mspx

This program, PsShutdown, is a member of Sysinternals' PsTools group of freeware utilities.

To shut down and power down a remote computer named "computername", enter the command:

psshutdown -k -f -t 00 \\computername

-k indicates shutdown and powerdown, -f means force all running programs to close without asking, and -t 00 means don't wait (wait zero seconds) until shutting down the computer named computername. (Don't miss the required space between -t and 00.) This utility can do several other things as well, like sending the target computer into hibernate mode or rebooting it.

Wake On LAN over the Internet

One example of how to wake up a home or office computer across the Internet is the following.

Assume a home network with a WAN router, in this case a DrayTek Vigor 2500We with firmware version 2.54. The main problem is that you cannot use any of the usual methods to address the target computer. Since it is off, it has no IP address. False advice is widespread, because people keep forgetting this simple fact.

The router has to send a special broadcast packet into the Local Area Network, using the MAC (Media Access Control) address, rather than the IP address. With some routers you may be able to achieve this by sending a Wake-On-LAN packet to the router and forward the port to an internal broadcast address, for example: 192.168.0.255

With most routers, however, this still does not work. You have to rely on the router to have the special ability to forward or send a Wake-On-LAN broadcast packet. In the case of the DrayTek router this can only be achieved through the telnet interface, but unfortunately not yet through the Web interface. An example for the telnet command for this DrayTek router is:

wol up 00E01890A545

Of course you have to replace the hex number with the MAC address of your target computer, and for this particular router you cannot intersperse hyphens as usual. The router needs the address in the direct short hexadecimal format. Check your router's manual or help system for its specifics.


Wake On Lan Command Line

Wake on Lan Command Line

http://www.depicus.com/wake-on-lan/wake-on-lan-cmd.aspx

The command line tool wolcmd is run from a dos prompt. The syntax is as follows:

wolcmd [mac address] [ip address] [subnet mask] [port number]

you can also do a local broadcast if you are running on an internal network thus

wolcmd [macaddress] 255.255.255.255 255.255.255.255

where mac address is the unique address of the network card. To find the mac address of a card you can either open the computer and read the address on the sticker on the card or much easier is, if you are using TCP/IP, ping the machine from a dos prompt (ping [machine name or ip number] i.e. ping delphi or ping 10.43.43.43) then issue an arp command (arp -a) which will list the ip number and physical address of the card.

If the port number is not specified it will default to port 7

Running from a batch file

Simply add multiple entries thus:

wolcmd 009027a324fe 195.188.159.20 255.255.255.0 8900
wolcmd 009024a731da 212.86.64.31 255.255.255.0 8900

To run the batch file, say half an hour before everybody gets into work, use the AT command on a Windows NT computer.

The AT command schedules commands and programs to run on a computer at a specified time and date. The Schedule service must be running to use the AT command. The syntax is as follows:

AT [\\computername] [ [id] [/DELETE] | /DELETE [/YES]]
AT [\\computername] time [/INTERACTIVE]
[ /EVERY:date[,...] | /NEXT:date[,...]] "command"


\\computername Specifies a remote computer. Commands are scheduled on local computer if this parameter is omitted.
id Is an identification number assigned to a scheduled command.
/delete Cancels a scheduled command. If id is omitted, all the scheduled commands on the computer are cancelled.
/yes Used with cancel all jobs command when no further confirmation is desired.
time Specifies the time when command is to run.
/interactive Allows the job to interact with the desktop of the use who is logged on at the time the job runs.
/every:date[,...] Runs the command on each specified day(s) of the week month. If date is omitted, the current day of the month is assumed.
/next:date[,...] Runs the specified command on the next occurrence of the day (for example, next Thursday). If date is omitted, current day of the month is assumed.
"command" Is the Windows NT command, or batch program to be run.


Example

wolcmd 00:1D:7D:22:DC:F0 192.168.1.86 255.255.255.128

wolcmd 001D7D22DCF0 192.168.1.86 255.255.255.128


Tuesday, January 12, 2010

Keyboard Listener in WPF

Key board message Hooking in WPF, using C++/CLI

KeyboardListener.h
==================
#pragma once

using namespace System;
using namespace System::Diagnostics;
using namespace System::Runtime::InteropServices;
using namespace System::Runtime::CompilerServices;
using namespace System::Windows::Input;
using namespace System::Windows::Interop;

public ref class CKeyboardListener : System::IDisposable
{

public:
CKeyboardListener(void);
~CKeyboardListener(void);

public:
delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
static LowLevelKeyboardProc^ m_pLowLevelKeyboardProc;

public:
void DisposeHook(void);
static IntPtr SetHook( LowLevelKeyboardProc^ pLowLevelKeyboardProc );
static IntPtr GetWindowHandle(System::Windows::Window^ window );
static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam);

private:
static const int WH_KEYBOARD_LL = 13;
static const int WM_KEYDOWN = 0x0100;
static System::IntPtr m_hookID = IntPtr::Zero;

public :
[DllImport("user32.dll", CharSet = CharSet::Auto, SetLastError = true)]
static IntPtr SetWindowsHookEx(int idHook,LowLevelKeyboardProc^ lpfn, System::IntPtr hMod, unsigned int dwThreadId);

[DllImport("user32.dll", CharSet = CharSet::Auto, SetLastError = true)]
//[return: MarshalAs(UnmanagedType::Bool)]
static bool UnhookWindowsHookEx(System::IntPtr hhk);

[DllImport("user32.dll", CharSet = CharSet::Auto, SetLastError = true)]
static System::IntPtr CallNextHookEx(System::IntPtr hhk, int nCode,
System::IntPtr wParam, System::IntPtr lParam);

[DllImport("kernel32.dll", CharSet = CharSet::Auto, SetLastError = true)]
static IntPtr GetModuleHandle(System::String^ lpModuleName);

[ DllImport("user32.dll") ]
static IntPtr GetForegroundWindow();
};

KeyboardListener.cpp
================
#include "KeyboardListener.h"
#include "MainApp.h"

CKeyboardListener::CKeyboardListener(void)
{
m_pLowLevelKeyboardProc = gcnew LowLevelKeyboardProc( &CKeyboardListener::HookCallback);
m_hookID = SetHook( m_pLowLevelKeyboardProc );
}

CKeyboardListener::~CKeyboardListener(void)
{

}

void CKeyboardListener::DisposeHook(void)
{
UnhookWindowsHookEx( m_hookID );
}

IntPtr CKeyboardListener::SetHook(LowLevelKeyboardProc^ pLowLevelKeyboardProc)
{
Process^ curProcess = nullptr;
ProcessModule^ curModule = nullptr;

while( curProcess = Process::GetCurrentProcess() )
{
while( curModule = curProcess->MainModule )
{
return SetWindowsHookEx(WH_KEYBOARD_LL, pLowLevelKeyboardProc,
GetModuleHandle(curModule->ModuleName), 0);
}
}

return System::IntPtr::Zero;
}

IntPtr CKeyboardListener::GetWindowHandle(System::Windows::Window^ window )
{
System::Windows::Interop::WindowInteropHelper^ pWindow;
pWindow = gcnew System::Windows::Interop::WindowInteropHelper(window);
return pWindow->Handle;
}

IntPtr CKeyboardListener::HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
int l_nWindowCnt = 0;
System::IntPtr l_hForeGndWnd = GetForegroundWindow();
MainApp^ l_pCurApp = (MainApp^) Application::Current;
int l_nVirtualKeyCode = 0;

l_nVirtualKeyCode = Marshal::ReadInt32(lParam);

if ( l_nVirtualKeyCode == 112 ) //If F1 key
{
for each (Window^ hWindow in l_pCurApp->Windows)
{
++l_nWindowCnt;

System::IntPtr l_hWindow = GetWindowHandle(hWindow);

if ( l_hForeGndWnd == l_hWindow )
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
((MainScreen^)l_pCurApp->mainwnd)->LoadHelp(); //Call the LoadHelp function from MainScreen class
return CallNextHookEx( m_hookID, nCode, wParam, lParam);
}
}
}
}

return System::IntPtr::Zero;
}


And in MainApp.cpp we create an instance of the class "CKeyboardListener"

void MainApp::OnStartup( System::Windows::StartupEventArgs^ e )
{
Application::OnStartup(e);
mainwnd = gcnew MainScreen(ResourceAssembly->Location);
m_pKeyboardListener = gcnew CKeyboardListener(); //Start the key board hooking

mainwnd->Show();
}

void MainApp::OnExit( System::Windows::ExitEventArgs^ e )
{
m_pKeyboardListener->DisposeHook();
m_pKeyboardListener = nullptr;
}