Archive for Programming

“Hello World” in 330 programming languages

So you think you know many programming languages? :)

TakeĀ  a look at this website http://www.roesler-ac.de/wolfram/hello.htm which exhibits around 330 “Hello World” programs in different programming languages!

Leave a Comment

“Threads” without Threads

Have you ever wondered how threads work and how they achieve “parallel” execution?
I have written a small demonstration article and posted it on CodeProject: “Threads” without threads

Check it out and rate my article!

Leave a Comment

The “CON” Folder

I just received a funny email today, reading:

TRY IT NOW , IT WILL NOT CREATE ” CON ” FOLDER.
YOU WON’T BELIEVE THIS !!!!!!!! An Indian discovered that nobody can create a FOLDER anywhere on the computer which can be named as “CON”. This is something pretty cool…and unbelievable… At Microsoft the whole Team, including Bill Gates, couldn’t answer why this happened!

TRY IT NOW , IT WILL NOT CREATE ” CON ” FOLDER.

What is funny about it is that people forward emails without research or verification.

As a programmer and as most programmers know, the “CON” is a special device name which stands for console. There are many other reserved devices names in the DOS/Windows OS, such as: AUX, NUL, LPT1, COM1, etc….

However, you would be able to create use such names if you pass a complete file as: “\\.\c:\con”. As an example, follow these steps:

  1. Start -> Run -> Cmd
  2. type: “notepad \\.\c:\con.”
  3. now go to “My Computer” -> “C:” and notice the file named “CON”
  4. Notice that you won’t be able to delete that file simply by selecting it and pressing DEL. If you ask why, it is because (again) Explorer is passing the name “CON” to the operating system’s DeleteFile() API and not its full name
  5. Now to delete this file, simply type “del \\.\c:\con.” in the command prompt

If you want another “CON” (console) trick, here’s one that allows you to create a text file without using notepad or any other text editing tools:

  1. Go to command prompt and type:
  2. copy con hello.txt
  3. start typing anything you want here, for everything you type here will go into the “hello.txt” file
  4. When you want to finish, simply press CTRL+Z (which will show as ^Z on the screen) and press ENTER

If you wonder what’s the CTRL+Z (aka ^Z) it simply marks the EOF (End Of file).

Comments (4)

Visual Studio and some useful keyboard shortcuts

Hello

I will present a short and handy list of useful keyboard shortcuts:

Key Meaning
*Make text selection then Shift+( This will surround the selected text with “(” and “)”
*Make text selection then Shift+{ Same as above but with { } surrounding
Ctrl+Ins/Shift+Ins To Copy / Paste (aka Ctrl+C/+V)
Ctrl+SPACE or Alt+RIGHT Toggle the autocompletion combobox
Ctrl+T To test a resource dialog
F12 When cursor is over an identifier, it goes to definition
Ctrl+- and Shift+Ctrl+- To navigate back and forth
Alt+F, J To go to Recently Opened Project

Hope you find them useful!

(*) You need Whole Tomatoes Visual Assist

Comments (1)

Panasonic BL-Cxx network cameras

Working with Network Cameras

Recently I had the chance to explore the two network cameras from Panasonic: BL-C1 and BL-C10.

Both cameras have builtin webservers and can be plugged to the network and operate standalone. They can also be configured to get static IP address or make use of a DHCP server.

Once the camera is set up, the user can explore its features by accessing its IP address via web protocol, example: http://192.168.1.253:80/ .

From there on you can see the camera image stream (pseudo-stream), control brightness/image quality, add users so they can access the camera, control timers, sensors, etc…

BL-C1 is a simple model where it provides nothing more than image capturing facility and motion detection which works by comparing an image with a previous image and see if the difference is within the user defined threshold.

The BL-C10 is a bit more complex model allowing you to control the camera’s eye direction. You can move the view to all the four directions. There is no motion detection, though one can implement this feature from the software, there is human detection sensor (“by picking up the infrared light naturally emitted from people and animals”).

The cameras come with a software CD containing set of user manuals and a windows application to control the cameras. For developers, they can download C SDK written and provided by Panasonic. You may compile the code using Visual Studio or (did not try it) any other C compiler. The C code is highly portable and should also work on other operating systems such as Unix. In case you want to program the camera using a different programming language, then you may download the CGI programmer’s document and start forming your own HTTP requests in order to talk with the camera’s webserver.
Now using CGI, to control the camera’s direction, one for example can call this web address: http://camera_ip_address:80/nphControlCamera?Direction=HomePosition

Till next time,
lallous

Comments (1)

C++0x ?

Hello

I’ve been looking around and was surprised that there will be a new C++ standard and additions dubbed “C++0x”.

According to Bjarne Stroustrup, “the new standard will be almost 100% compatible with the current standard”.

Some of the features I like about this new addition are:
- atomic keyword (for uninterruptible code)
- thread local storage
- asynchronous calls through the “future” keyword
- implicit function calls (as if using properties in other languages)
- parallel execution

The good news for some, probably bad news for eager ones is that the C++ Standards Committee aims to introduce the new standard in 2009.

Reference:

Leave a Comment

Emulating CoCreateInstance()

Introduction

A while ago I wrote a small utility that converts DOT syntax into an image using
the WinGraphViz.DLL COM component.

However it is not very usual to have the user register this component before
running the tool, thus I started looking around for a way to use COM component
as if they were normal DLL.
The research yielded that there are many techniques to accomplish that:

  1. Registration-Free COM (for XP and above)
  2. Emulating the CoCreateInstance()

To learn more about the Registration-Free COM please check the references at
the end of this article.

Emulating CoCreateInstance()

Normally, to create an instance you would do something like:

  hr = CoCreateInstance(CLSID_DOT, NULL, CLSCTX_ALL,
  IID_IDOT, (LPVOID *)&pIDOT);

This will cause OLE to fetch the associated DLL from the registry and call its
DllGetClassObject() method to get a class factory then from the class factory
an instance of your required IID will be created.

For that reason we may emulate the CoCreateInstance() by the following code:

HRESULT __stdcall MyCoCreateInstance(
  LPCTSTR szDllName,
  IN REFCLSID rclsid,
  IUnknown* pUnkOuter,
  IN REFIID riid,
  OUT LPVOID FAR* ppv)
{
  HRESULT hr = REGDB_E_KEYMISSING;

  HMODULE hDll = ::LoadLibrary(szDllName);
  if (hDll == 0)
    return hr;

  typedef HRESULT (__stdcall *pDllGetClassObject)(IN REFCLSID rclsid, IN REFIID riid, OUT LPVOID FAR* ppv);

  pDllGetClassObject GetClassObject = (pDllGetClassObject)::GetProcAddress(hDll, "DllGetClassObject");
  if (GetClassObject == 0)
  {
    ::FreeLibrary(hDll);
    return hr;
  }

  IClassFactory *pIFactory;

  hr = GetClassObject(rclsid, IID_IClassFactory, (LPVOID *)&pIFactory);

  if (!SUCCEEDED(hr))
    return hr;

  hr = pIFactory->CreateInstance(pUnkOuter, riid, ppv);
  pIFactory->Release();

  return hr;
}

Notice that how this function takes a parameter holding the DLL’s name.

Using the code

You need your application to run first if the COM is registered, if not you
would resort to emulating the CoCreateInstance(). Your code could look like
this:

hr = CoCreateInstance(CLSID_DOT, NULL, CLSCTX_ALL,
  IID_IDOT, (LPVOID *)&pIDOT);

if (hr == REGDB_E_CLASSNOTREG)
{
  hr = MyCoCreateInstance(_T("WinGraphViz.dll"), CLSID_DOT, NULL, IID_IDOT, (LPVOID *)&pIDOT);
}

if (FAILED(hr))
{
  cout << "CoCreateInstance Failed: " << hr << "nn";
  return -1;
}

Reference

The following links were helpful during the building of this simple snippet:

Comments (3)