Tag: windows

Entries for tag "windows", ordered from most recent. Entry count: 40.

Warning! Some information on this page is older than 5 years now. I keep it for reference, but it probably doesn't reflect my current knowledge and beliefs.

Pages: > 1 2 3 4 5 >

# Handling Ctrl+C in Windows Console Application

09:23
Wed
28
Sep 2011

Just a small code snippet: Let's say you write a console application in native C++ for Windows. Closing the console by pressing Ctrl+C, Ctrl+Break or clicking on close window button [X] kills the process. Is there any way to handle such event and close the program gracefully? The answer is calling SetConsoleCtrlHandler() WinAPI function and implementing your own HandlerRoutine callback function. The template looks like this:

// Handler function will be called on separate thread!
static BOOL WINAPI console_ctrl_handler(DWORD dwCtrlType)
{
  switch (dwCtrlType)
  {
  case CTRL_C_EVENT: // Ctrl+C
    break;
  case CTRL_BREAK_EVENT: // Ctrl+Break
    break;
  case CTRL_CLOSE_EVENT: // Closing the console window
    break;
  case CTRL_LOGOFF_EVENT: // User logs off. Passed only to services!
    break;
  case CTRL_SHUTDOWN_EVENT: // System is shutting down. Passed only to services!
    break;
  }

  // Return TRUE if handled this message, further handler functions won't be called.
  // Return FALSE to pass this message to further handlers until default handler calls ExitProcess().
  return FALSE;
}

int main(int argc, char **argv)
{
  SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
  ...
}

If your program performs some loop and you want the user to be able to break it by closing the console or pressing Ctrl+C, you can solve it this way:

static volatile bool g_exit = false;

static BOOL WINAPI console_ctrl_handler(DWORD dwCtrlType)
{
  g_exit = true;
  return TRUE;
}

int main(int argc, char **argv)
{
  SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
  initialize();
  while (!g_exit)
    do_a_piece_of_work();
  finalize();
}

Comments | #windows #winapi #c++ Share

# Windows 8 Developer Preview

22:45
Thu
15
Sep 2011

News about upcoming Windows 8 appear for some time. Information about the new Windows version directly from Microsoft, including technical details for developers, can be found in this PDF: Windows Developer Preview - Windows 8 guide. Recently Microsoft shared a full, development version of this system to download, install and test on your computer for free. It's a developer preview - it contains the new operating system along with new Visual Studio 11. You can download it as ISO image from Windows Developer Preview downloads. The system works in VirtualBox virtual machine. You can see my first gallery of screenshots here:

My first impressions after testing Windows 8 are... quite weird. Apparently they try to make desktop OS looking like a cellphone, with big fonts and all apps working in fullscreen. But that's only a half-truth. I feel that in Microsoft they always do it this way: a boss comes and tells that "we do it again from scratch only better, redefine everything, but we have to preserve backward compatibility", then a developer thinks how to implement it the simplest possible way and it ends in a new flashy UI containing several shortcuts to the most important commands with the old, good windows hiding somewhere under "Advanced" button. It was the same with Control Panel, with formatting functions in MS Office and now it's the same with the whole Desktop. You are presented a new screen full of colourful rectangles, but as soon as you move your cursor to the bottom-left corner of the screen and click "Start", you are teleported to the normal desktop with a wallpaper, taskbar and the Recycle Bin :)

Other things that attracted my attention: You now login to Windows using your Windows Live ID account. System is integrated with Facebook by providing some application to browse it. Explorer now uses the new Ribbon toolbar, just like new versions of MS Office, Paint or WordPad do for some time. There are lots of new games. New Task Manager is great because it now shows four columns with all most important statistics of every running program: the usage of CPU time, RAM memory, disk transfer and network transfer. Finally the new UI style: flat, colourful, minimalistic, full of solid filled rectangles.

Comments | #windows #visual studio #gui Share

# Hotkey for Macro Inserting Text

15:42
Wed
03
Aug 2011

I recently code in C - an ancient language with no support for namespaces. To code a bigger system and not create name conflicts, prefixes for all public identifiers must be used. But they not only make the code less readable, but also take lots of time to type. I thought that at least the second issue can be minimized by setting up some macro that would insert predefined text (like "rendering_"), whenever I press a hotkey button (like Pause/Break).

I couldn't find such feature in my Visual C++ 2010 Express, so I decided to look for some general tool for Windows that can trigger a script when a hotkey is pressed. I found AutoHotkey - a free application with its own scripting language that claims to be successor of AutoIt, which I used some time ago. After reading some documentation, I coded following script:

Pause::
SetKeyDelay -1
send rendering_
return

+Pause::
SetKeyDelay -1
send RENDERING_
return

If you have AutoHotkey installed, just save this code to a file, give it "ahk" extension and double-click to run it. Program will create a system tray icon for this script indicating that it's running in the background. From now on you can just press Pause key to insert text "rendering_" to whatever input control you are focused, or Shift+Pause to insert "RENDERING_". It really speeds up coding in C :)

Comments | #windows #tools #c Share

# Pointing to DLL Files in Visual CPP

18:35
Tue
12
Apr 2011

When coding in Visual C++, we sometimes need to use some DLL libraries like FMOD, wxWidgets, Intel TBB etc. We download or build the library, setup directories to include and library files, finally #include <header.h>, #pragma comment(lib, "library.lib"), compile, run and...

Certainly our program also needs DLL file at runtime. Sure we have to attach it to the program when we distribute it, but do we really need to copy all these libraries to the Debug and Release subdirectories in our project? For a project run from Visual C++ to find required DLL files, they must be placed either in:

or in the PATH environmental variable. That's an option I've discovered yesterday. To use it in Visual C++, navigate to project properties / Configuration Properties / Debugging / Environment and set it to something like: PATH=$(PATH)$;C:\my_libraries\library_1\DLL_dir

If you do it correctly, your program launched from Visual C++ (with or without debugger - F5 or Ctrl+F5) will now be able to find required DLL libraries without need to copy them to your project directory.

Comments | #windows #c++ #visual studio Share

# Visual Studio 2010 Service Pack 1 Released Yesterday

20:14
Fri
11
Mar 2011

Yesterday Microsoft publicly released Visual Studio 2010 Service Pack 1. It is big (1.48 GB) and the installation takes quite long. It applies to all Visual Studio 2010 versions and components, including C++ Express, C# Express etc. Here is the list of changes and here is download link (you can find link to ISO file with offline installer at the bottom of that page). There are lots of fixes and improvements, but most of them apply to managed or web technologies like Silverlight. What's interesting for a C++ programmer is the addition of intrinsics to enable the extensions on the AMD and Intel new microprocessors that will be released next year - Intel AVX and AMD Bulldozer. Microsoft also claims to optimize and fix C++ compiler. Changelist mentions fixing lots of IDE crashes, including incompatibility with AMD CodeAnalyst.

Overall I think the new SP1 for VS2010 is worth installing, unless you have "Windows SDK for Windows 7 and .NET Framework 4" installed in your system. In that case an unsolved issue would happen so you should NOT install the Service Pack.

By the way: MSDN Library - the extensive documentation of C, C++, WinAPI, OpenGL, .NET and lots of other technologies - is available for free in form of an offline application for some time, but the latest version is MSDN Library for Visual Studio 2008 SP1. Do you know how to download some newer release? Or maybe that "Windows SDK" ships with it?

Comments | #c++ #visual studio #windows Share

# New jEdit Launcher - How to Install

20:12
Mon
17
Jan 2011

Writing programs in some managed technologies like Java or .NET instead of native C/C++ has many disadvantages. First and the most obvious one is smaller efficiency and greater memory consumption. That's why, for example, I prefer uTorrent over Azureus as BitTorrent client. Second flaw is harder access to native system API. That's why my friend Przemek created libraries for accessing new Windows 7 taskbar features from Java, Python and Qt code and sells them as his startup Strix Code.

Finally it's sometimes harder to integrate such programs with system shell. With .NET it's not the case because it creates EXE files so they can work as normal programs and even system services (I'm not sure about shell extensions and device drivers). With Java it's more difficult because there program is a JAR file. As jEdit is still my favourite text editor (second after Visual C++ of course), I was looking for a way to integrate it with Windows so it was my default editor for TXT and other text files. Recently I've found New jEdit Launcher - a package that serves this purpose very well, but unfortunately it's quite hard to setup. So in case you also use jEdit and look for a way to do it, here are the steps, checked under Windows XP as well as 7:

  1. Unpack New jEdit Launcher to some directory. Windows 7 causes lots of problems with Program Files so I prefer some other places, let's say "h:\Program_Files\New jEdit Launcher".
  2. Run command prompt ("cmd.exe"). On Windows 7 you must do it as Administrator, so open Start menu, type "cmd", right-click on the black "cmd.exe" icon and select "Run as administrator".
  3. From the command prompt, enter the "bin" subdirectory of New jEdit Launcher like this:
    > h:
    > cd "\Program_Files\New jEdit Launcher\bin"
  4. Call the magic command to register DLL file:
    > regsvr32 jeditext.dll
  5. Now launch registery editor through Start > Run > "regedit" and create entries as decribed in "New jEdit Launcher\README.html" documentation in "Installing" section.
  6. Second bunch of required registry entries (described in "Configuration" section) can be entered more conveniently, through "New jEdit Launcher\config\launcher.reg" file. Open this file in some text editor (Notepad or... jEdit :) and fix the paths to your Java JRE and jEdit directories. Enter path to "javaw.exe" not "java.exe" as the "Java Executable", because you don't want system console to be shown along with jEdit window. Values "jEdit Target" and "jEdit Working Directory" must point to the place where you have jEdit installed. Don't forget that all backslashes in these paths must be doubled, like "h:\\Program_Files\\jEdit 4.3.2\jedit.jar"!!!
  7. Now run this "launcher.reg" file through double-clicking on it. After confirming the warning/question, the registry entries should be created.
  8. Check if it works by running "New jEdit Launcher\bin\jedit.exe" file. jEdit should be started if it wasn't already running.
  9. You can create a shortcut to this "jedit.exe" file in your Start menu. This way you have a shortcut to jEdit with pretty icon instead of default Java one, as when linking directly to the "jedit.jar" file.
  10. Associate TXT and possibly other file types (extensions) with this "jedit.exe" program. It's done differently in Windows XP and 7 but generally you can find the appropriate configuration window it in the Control Panel. This way all text files will be opened by jEdit and they also (unfortunately) get jEdit icon.

Comments | #windows Share

# Generating LIB File for DLL Library

22:09
Tue
04
Jan 2011

I've been recently trying to use libVLC - functionality of great, codec-less VLC media player enclosed in form of DLL library. By the way I've came across a great article: GenerateLibFromDll and now I know how to generate LIB file for any DLL library! Here is detailed description of the problem:

When you have a DLL library you want to use in your C++ code, you may do it dynamically by using LoadLibrary and GetProcAddress functions from WinAPI, but it's more convenient to do it statically. But it's not enough to just #include signatures of library functions and use them in your project. You also need to link with some LIB file, even if the file is not a real static library with compiled code, but only a few-kilobyte-long list of imported functions. I believe that's just another flaw of C++ language, because other languages like for example C# don't need this even when importing functions from native DLL libraries.

If you have some SDK prepared for Visual C++ or compile the library by yourself, you also get the LIB file next to DLL. But if you have only the library, that article shows following steps to generate matching LIB:

1. From Start menu run "Visual Studio Command Prompt".

2. Execute command:

dumpbin /exports DLL_FILE.dll > DEF_FILE.def

This command prints some information about given DLL library in textual form to its standard output. We redirect it to a text file with DEF extension. But to make it real DEF file, we need to edit it.

3. Open DEF_FILE.def in some text editor and edit it to contain only the names of exported functions in form of:

EXPORTS
function_1_name
function_2_name
function_3_name
...

4. From the Visual Studio Command Prompt, execute another command:

lib /def:DEF_FILE.def /out:LIB_FILE.lib /machine:x86

And there you have it! The so much required LIB file generated from DLL library. You only need signatures of these functions with proper parameters and return values declared in some H header file and you can successfully use your DLL by linking with LIB file created by yourself :)

Comments | #visual studio #c++ #windows #winapi Share

# JustSendIt Prototype

20:00
Thu
07
Oct 2010

It's not a new idea for me, but last weekend it made me angry again that I couldn't send a file from my laptop to my desktop PC over my home local network. It may seem strange and it's really shocking to me because transferring data is the most fundamental thing we do when using any kind of network. But at the same time I don't know about any program that would simply allow me to send a file to a destination machine without being overly sophisticated or causing some strange technical problems. Here are solutions I've considered:

Network Neighbourhood - most natural way of passing files between Windows computers. I tried to share some folder on my laptop to access it from my PC and it didn't work, despite machines could contact each other via hostname or IP. Same happened when I shared a folder on my desktop and tried to enter it from my laptop. Well, we all know that this service often stop working randomly... Disabling Windows Firewall on both machines didn't help either.

FTP - I have FileZilla FTP server installed on both machines and I know it's quite good software, but that day I couldn't make it work. I could logon to the server from remote machine, but not to transfer any file. I tried different settings on client side like Passive Mode etc., but it changed nothing. I don't like the idea of using FTP to transfer files anyway, because why do I have to install the server, setup user accounts etc. when I just want to transfer a file? Besides, FTP protocol is known for causing technical problems because it opens separate connections on different ports to transfer data.

Other possibilities to send a file over a network are:

So finally I've used an USB flash memory stick to copy this file :P But after this, I've recalled my old idea about a simple program to transfer files over a network called JustSendIt [PL]. Then I decided to code a quick prototype of such program in C# and here it is: JustSendIt Prototype. It requires .NET Framework 4.0 to work.


If you have some computers at home or at work that you transfer files between or if you want to a file to your friend over the Internet, you are sure one of the computers have public IP or is set as DMZ so the other can connect to it, the way to send a file is:

  1. Run JustSendIt Prototype on both machines.
  2. On the receiving machine, go to Receive tab and press Load button to see your computer name and IP address..
  3. On the sending machine, go to Send tab, click Browse button to select a file to be sent, as well as enter the name or IP of the destination computer.
  4. On receiving machine, press the big Receive! buton to start waiting for incoming connections.
  5. On sending machine, press the big Send! button to connect to the second machine.
  6. On the receiving machine, a dialog pops-up to as user about the directory where to save the incoming file.
  7. After confirming this dialog, the file is transferred until finished.

I hope some day I'll find enough motivation to polish and finish this program :)

Comments | #networking #tools #windows Share

Pages: > 1 2 3 4 5 >

STAT NO AD
[Stat] [STAT NO AD] [Download] [Dropbox] [pub] [Mirror]
Copyright © 2004-2017