Saturday, August 15, 2009
Google Wave Hackathon Malaysia
Thursday, August 6, 2009
Too much support?
Monday, August 3, 2009
C# calling C DLL routines (YET ANOTHER ONE)
Not only are many of them repeating one same example after another, you soon realize they are not very helpful when you want to do something that is common like passing a structure with all kinds of types inside.
So here is one example that serve as an answer to MANY of the pinvoke/calling C apis problem :- ( i wanted to say 90% but i can't justify or prove it ;-))
I got this example structure that has :
struct ToughCallDef
{
char * ptrName; // this will contain some return string values
int ptrNameLen; // normal integer
MEMBLOCK somestruct; // a structure
ANOTERBLOCK* structpointer; // structure pointer
int* ptrtoMyInt; // integer pointer
char * ptrID // this must be supplied
};
Just translate it this way :-
[StructLayout(LayoutKind.Sequential)]
public class ToughCallDef
{
IntPtr ptrName;
MEMBLOCK somestruct; // you need to define MEMBLOCK also
IntPtr structPointer;
IntPtr ptrtoMyInt;
IntPtr ptrID;
}
And if the call requires a reference to this "ToughCall" :
[DllImport("anotherc.dll")]
public static extern int Call_C_API(ref ptrToughCall);
define it this way instead :
[DllImport("anotherc.dll")]
public static extern int Call_C_API(IntPtr ptrToughCall);
Basically anything to do with Pointers, regardless of whether its integer *, char *, structure reference etc, just use IntPtr.
1 . If the member is a "char *" that you expect return values you point the IntPtr this way :
szReason = new StringBuilder(100);
zReason.Append(' ', 100); // empty string with spaces
ToughCall.PtrName = Marshal.StringToHGlobalAnsi(m_szReason.ToString());
2. If the member is a "char *" that you need to assign a value before passing in :
ToughCall.ptrID = Marshal.StringToHGlobalAnsi(StringVar);
3. If the member is a structure pointer, you assign it this way :
ANOTHERBLOCK o = new ANOTHERBLOCK();
ToughCall.structPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ANOTHERBLOCK)));
Marshal.StructureToPtr(o, ToughCall.structPointer, true);
4. After calling the C routine you will need to do the following to get the values :-
- If its a string, use :
string s = Marshal.PtrToStringAnsi(ToughCall.PtrName) - If its a structure use :
Marshal.PtrToStructure(ToughCall.structPointer, o); - Free any allocated strings/struct pointers via :
Marshal.FreeHGlobal
That's it.
Wednesday, July 22, 2009
Twitter (python api) on Nokia S60
With this system an organization can keep track of what its support/sales staff are at any given point of time.
After tinkering around with the twitter python api and Nokia s60 python (1.9.6), i finally
got it working...(duh...that took me more than an hour...)
There is however ONE big catch (read end of the section)
Here is the code :
Gotchas :-
1 - Ah yes you also need to change that getusername routine in twitter, else it wont work
2- Pay attention to that pesky pyS60 Application Packager (READ the README file) , this is not py2exe. Basically, you will need to rename your main file to some fix default.py.
3- It will keep asking you for connection (access point) on each msg , unless your using some newer phones like nokia 5800 XM. If you are using N82, ALL the popular examples shown in the internet like these :
http://discussion.forum.nokia.com/forum/showthread.php?t=163939
or
http://snippets.dzone.com/tag/pys60
or
http://croozeus.com/blogs/?p=836
DOESN'T Work. The reason is due to some "compatibility problem" between socket and btsocket.
"Twitter" uses urllib2 not urllib, and these people probably have never tested it on that api. Someone from Silicon Valley once said that Nokia know nuts about promoting development, and thus he rejected their offer to develop for symbian and instead move the whole team to iphone.
In some ways, i agree....but i still love my nokias ;-) I reckon the same thing would have worked in Iphone much earlier and with less blood on my desk.
** Update :
Marcelo Barros from Croozeus :
Yes, that is the problem. I reported it at maemo some time ago.
I suggest you to use my twitter api. Extend it if necessary.
http://code.google.com/p/wordmobi/source/browse/trunk/wordmobi/src/s60twitter.py
It uses urllib and simplejson (I ported it to S60, code in the same dir).
Note : If you still want to use the twitterapi as it is and not the ported version, you will
need to hardcode the "getusername" and stick to "socket" only calls, avoid using btsocket.
Sunday, July 12, 2009
C# calling Python scripts and processing output
you want to process each line one by one and you happened to have somekind of timing (time.sleep) or some other python codes that works correctly under console but unable to be processed by C#.
Below is how you do it : (read until the end, there is a gotcha)
private static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if (outLine.Data != null)
Console.Out.WriteLine(outLine.Data.ToString());
}
static void Main(string[] args)
{
Process p = new Process();
p.StartInfo.FileName = "python.exe";
p.StartInfo.Arguments = "c:\\test\\test.py";
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();
p.Close();
}
Assuming ur python script is the following (test.py) :
import time
import sys
for x in range(100) :
print x
time.sleep(1)
You will be surprised that you dont get anything. For some reason, time.sleep(1) and possibly
some other python library calls would result in the output not being flushed. The solution
would be to "flush" it :
for x in range(100) :
print x
sys.stdout.flush()
Vmware and the Virtualization Gotcha's
Server A - Mailserver
Server B - Intranet application with MS SQL db
Server C - Vpn server and backup
Server D - Test server
Along the way, the admin decides to put all these dated servers into a VM and slot it in to a high end Server from Dell. The whole porting process took around 2 weeks and when it was over,
everyone was happy with the new setup. No more additional switching cables and multiple monitors lyring around and the perceived energy savings cost was a bonus.
However one day, the mailserver began to feel very slow, Procexp (sysinternals) itself was myteriously taking up 45% cpu and more and even with the mailserver service stopped the pc still feels awkwardly slow. Rebooting didnt that mail server VM doesn't help either.
When i came in to help out in this scenario, the first thing i went thru was the list of VMs running in the server. One particular VM is taking up 26% of the CPU of the main server, however that should not be the reason why it would affect the mail server VM. Upon closer inspection however, i notice some native apps running on the test machine VM (26%) that is using up the VM's tcp/ip port very quickly.
It then became logically clear that this was the problem, pausing that VM immediately restored the other server's performance and that was like a 100% improvement.
I suspect the problem is because the main server is still just an OS with the normal limitation of the 65535 ports on a single ip and single network card. Since all the VMs runs on this machine , that test application was just blasting away the network resource, this turns the allocation of the real main server NDIS packets resources into an ugly situation where each of the VMs are queueing up to get its allocation.
Another case solve. Another Hamster Huey and the GooiKablooi award for virtualization.
Thursday, June 4, 2009
Linux Desktop And Windows 7
Most of the stuffs are similar to windows in that you have hardware detection app running in the background with an icon in the system tray. The wifi detector works its best in trying to minimise "geeky" configurations and in my test notebook DELL inspiron 1520, it works straight out without the need for me to configure it manually and tweaking the config files.
With the common host of free apps like openoffice and email clients (Evolution - the email client is still lacking in refinement compared to Outlook Express but Thunderbird isn't), heck, for most Janes and Johns who just want to have email, internet surfing and office documents, this would feel right at home. The system works faster and you even feel "safer" without having any anti-virus, anti-malware, anti-adware, malicious removal tools , Windows Defender and all those hooking apps that layers itself one on top of the other.
Nine months ago, when i tried OpenSuse on my other notebook , it was hell, i had to configure and download specific wifi patch just to make it work. If a common user would have to choose between OpenSuse and Windows OS, even Win 98 would be a better option (speaking strictly in terms of usability for users who want things to just work and not do any command lines, scavaging forums and pulling hairs)
Ubuntu, rightly one of the most popular distro now, deserves an applause from the Linux community. Instead of sticking to the unspoken rule of "we can't copy microsoft, if you can't configure some simple scripts you are lame", it went straight on to make Linux a much friendlier distro for desktop. By providing an option without the need to repartition the HD and installing it side by side with Windows XP/Vista, it became an attractive option for users to try it.
The introduction of Version 8.1 was nothing short of perfect timing too. Vista, being an infamous slow OS - thanks to the "dev team" that insists on working on the new approach and forsake previous compatibility.
The fact is, Vista is slow. There is just something wrong with its design.
Personally i do not use any anti-virus, instead i even ran defrag religiously and PerfectDisk periodically to make sure i have optimum speed for my HD files. Still it was slow, i made sure my services are configured correctly and disabled all that i can lay my hands on and it is still slow. The frighthening thing was the fact that my Vista ran 3 times faster than any other Vista around my office on similar hardware, yet i find the response unacceptable.
Ubuntu on the other hand, ran seemingly faster even when it was installed using the side by side installation. Thanks to Vista, I believe tons of users flock to try Linux distro and many who happened to try Ubuntu would be positively surprised. Just this 2 weeks, i notice that there are 2 more collegues who installed Ubuntu and was excited to show it around.
However, there are still many hurdles for Ubuntu to overcome before it can become a replacement for Windows. For e.g, try connecting it to a projector, and good luck.
Try getting Compiz to work, chances are that you won't get it working correctly in the first 30 minutes and oh, pls update your Nvidia drivers else be prepared to do some file editing.
To date, there are around 200 Linux distros, some says 450. This is actually good news to Microsoft. As long as these jokers cannot unite, Windows will continue to dominate. Every aspiring Linux techie with enough willpower will want to produce his/her own Distro with some lousy excuse for yet another distro.
I would bet that if the community would drop all the similar distros and say just concentrate on 3 :
1. CentOS or RedHat for Server
2. Ubuntu for Desktop / Workstation
3. Knoppix for small storage / misc applications
Or better :
Ubuntu Desktop, Ubuntu Server, Ubuntu Mini.
Then all the big hardware manufacturers and the gaming industry would ensure that they will provide support for this alternative OS. Ms.Window would have to taste its own medicine in pitting against FREE OS. Yup, taste its own medicine. The number of companies that MS brought down just by including the said product/feature FREE are calling out for judgement day.
Netscape, RIP.
The only other area that MS still have not managed to kill are the Anti-Virus companies. Free Anti-virus seems to be playing along MS agenda all the time and yet failed to be executed. Hmm, before this happens, i should seriously fold up my sleeves and start working on this project i have in mind for some time now, the Corporate-Anti-Malware-End-Of-Anti-Virus project. A solution that will guarantee a corporate environment to be clean of viruses using a filter driver and a concept that involves a model pc, central sandbox and whitelisting. No more antivirus scanning and signature subscriptions!
Yawn....signing off.

