Thursday, March 5, 2009

Working with Microsoft Speech API (SAPI)

SAPI or Speech Application Programming Interface is provided by Microsoft for converting text-to-speech and speech recognition.

Isn't it fun to have your own application which would read out text to you? Here is the sample vbscript code:

Dim name, sapi, reply
name = InputBox("Please enter your name","I would like to talk to you!")
Set sapi = CreateObject("sapi.spvoice")
Set sapi.Voice = sapi.GetVoices().Item(1)
reply = "Hello" & name
sapi.Speak reply
sapi.Speak "How are you doing?"

Save this code as "test.vbs" for example and run it. Enter your name in the prompt and enjoy the computer speaking to you :)

Explaining the code:
Line No.2 prompts the user to enter name. The name entered by user is saved in variable "name".
Line No.3 creates the sapi object for manipulation
Line No.4 selects the voice available in the system
Line No.5 adds Hello to the name
Line No.6 and 7 speaks out the string to the user

For more information on SAPI, Microsoft Speech API 5.3

Monday, March 2, 2009

Put your system to hibernate in a double click

So I am back with more tips for people who are lazy like me.

In one of my earlier, blogs I had mentioned, how lazy I felt to lock my desktop and how I wanted it in a double-click.

Well, being lazy again, now I have decided to put my system in hibernate or standby in a double click too.

Here's the code:

#include "windows.h"
#include "atlbase.h"

void create(TCHAR argv[100])
{
STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory( & si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( & pi, sizeof(pi) );

// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
argv, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}

// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );

// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );

}
int _tmain(int argc, _TCHAR* argv[])
{
USES_CONVERSION;
char tmp_path[MAX_PATH];
char cmd[MAX_PATH];
strcpy_s(tmp_path, MAX_PATH,"%windir%\\System32\\rundll32.exe powrprof.dll,SetSuspendState");
ExpandEnvironmentStringsA(tmp_path, cmd, MAX_PATH);

TCHAR* str= A2T(cmd);
create(str);
}

Yahooooo... You are done!

Its too bad that blogger doesnt allow me to upload executables..else I would have just uploaded it for you. Maybe I should start a website for all these executables :)