Dec 11, 2009

'snprintf' was not declared in this scope

This is one of the incompatibility problem of Linux.

I have found this problem while installing MySQLConnector/C++.

The source code is written in some Linux distribution in which snprintf() is in stdlib.h.

But, in my Linux distribution, Fedora Core 12, snprintf() is in cstdio.

Therefore, the way to fix it is to add #include <cstdio>.

PS. The problem can also be something like 'printf' was not declared in this scope.

Dec 10, 2009

Declare C++ Interface

Weird C++ ! If your would like to declare an interface class, you shall use normal "class" keyword and declare every method to be "virtual". And you must add a virtual destructor, which must be implemented (as a blank method), otherwise you would not be able to pass an instance of the class as a pointer.

Here is the demo code: (from http://stackoverflow.com/questions/318064/how-do-you-declare-an-interface-in-c)

class IDemo
{
  public:
    virtual ~IDemo() {}
    virtual void OverrideMe() = 0;
};

class Parent
{
  public:
    virtual ~Parent();
};

class Child : public Parent, public IDemo
{
  public:
    virtual void OverrideMe()
    {
      //do stuff
    }
};

Dec 9, 2009

How to correctly link a shared libray in GCC

This is one of the fucking weird problem which I tried to solve for two hours.

First, you need to prepare the library, which consists of header files (*.h) and libtanintest.so.

Here is the thing: When you link the shared library, libtanintest.so, to your code, you need to set library's directory (use -L) and library name (use -l) (small L).

Here is when it gets weird, your library name must be chopped into tanintest (remove lib in the front and .so in the back.

Therefore, the command to link library is :

gcc main.c-o output_file -L/lib_dir -ltanintest


If you do not link it, you will get error "undefined reference to ...".
If you link it incorrectly, you will get "the library cannot be found".

How to correctly close a socket

In order to close a socket, we shall use shutdown() and then close(). Shutdown() will notify the socket to shut itself down and release from read() command, while close() won't do those stuffs.

Therefore, if you used only close(), the blocking read() won't be released !

Thread call on class method

It is very strange for C++. When you want to fork another thread, which runs on a class method, you cannot invoke pthread_create() because C++ does not a class method of type void* start_routine)(void*) to be "deferenced".

Here is the signature of pthread_create();

int pthread_create
(
  pthread_t *thread,
  const pthread_attr_t *attr,
  void * (*start_routine)(void*),
  void *arg
);


To work arounde this problem, we must declare a class method to be a static class method, which is allowed to be deferenced. Then, we pass the instance of the class as arg. Then, inside the static method, we invoke the wanted class method.

Here is an example:

class Test
{
  private:

    void _StartDoing()
    {
      // do something
    };

    static void* _ThreadStartDoing(void *obj)
    {
      reinterpret_cast<test *>(obj)->_StartDoing();
    };
}


And when you want to fork a thread, inside Test:

pthread_create( &pid, 0, &Test::_ThreadStartDoing, this);


And that is it.

Dec 2, 2009

Wifi (wlan) is off when the power-off button is pressed

Today I've just found the way to prevent wlan (wifi) on Window Mobile from being off after the power button is pressed (in order to turn off the pocket pc's screen).

The solution is very simple; We just switch on the "Unattended" PowerPolicy.

In order to set the PowerPolicy, we use:

[PreserveSig]
[DllImport("coredll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Ansi)]
public static extern bool PowerPolicyNotify(PowerManager.PPN_Message dwMessage, int dwData);


Then to switch on the "Unattended" PowerPolicy, we invoke:

PowerPolicyNotify(3, 1);

  • The first parameter is the PowerPolicy's identification and 3 is for the "unattended" PowerPolicy.
  • The second parameter is either 1(on) or 0(off).


You also need to keep sending package every 10 or 20 seconds to keep the wifi active.

And that is it. When you press the power button to turn off the screen, the wifi won't go offline anymore. Try it!


PS. I have tried with Symbol MC50 (Window Mobile 5)

---------------------------------------------------------

* After beta-testing, we have found that WiFi still goes off around every hour.

We have to turn WiFi on programmatically, if that is the case.

Here is the code :

[PreserveSig]
[DllImport("Coredll.dll")]
public static extern int SetDevicePower(string device, int dwDeviceFlags, int state);

[PreserveSig]
[DllImport("Coredll.dll")]
public static extern int DevicePowerNotify(string device, int state, int flags);


DevicePowerNotify("wlp1:", 0,1);
SetDevicePower("wlp1:", 1, 0);

* WiFi device for Symbol MC50 is named "wlp1:"