星期一, 九月 12, 2011

Use C language to get Hard Disk Serial Number in Linux





/*
* It gets the hard disk information in this case serial no.
* It uses ioctl() system call
*/
#include <stdio.h>
#include <string.h>
#include <linux/types.h>
#include <linux/hdreg.h>
#include <linux/fcntl.h>

//
int hd_serial(char *searial_no);
void trim(char *s);
//
int main() {
char serial_no[256];

if (hd_serial(serial_no)){
printf("%s",serial_no);
}

return (0);
}
// ----------------------------------
int hd_serial(char *serial_no){
int fd,err,i;

/* structure to get disk information and
* returned by HDIO_GET_IDENTITY, as per ANSI ATA2 rev.2f spec
*/
struct hd_driveid hd;

/* open the device */
if( (fd=open("/dev/sda", O_RDONLY ) ) < 0 ){
//perror("Device Open Error");
return 0;
}

/* get required info */
if( (err = ioctl(fd, HDIO_GET_IDENTITY, &hd) ) < 0){
//perror("IOCTL err");
return 0;
}else{
//printf("Serial No = %s\n",hd.serial_no);
strcpy(serial_no, hd.serial_no);
trim(serial_no);
//printf("%s",serial_no);
}
return 1;
}
// ------------- trim ---------------------------
void trim(char *s){
int i=0, j, k, l=0;

while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
i++;

j = strlen(s)-1;
while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
j--;

if(i==0 && j==strlen(s)-1) { }
else if(i==0) s[j+1] = '\0';
else {
for(k=i; k<=j; k++) s[l++] = s[k];
s[l] = '\0';
}
}
//----------------------------------------------


星期四, 八月 18, 2011

網路看到的笑話幾則

一對夫妻在蜜月旅途中....車突然拋錨~
男下車修理~搞了老半天都弄不好~
女:天色漸晚了耶~前面有家汽車旅館~我們過去那裡休息~說不定跟上次 一樣~睡一覺後車子就會好了~
男:那次是結婚前好不好~這次是真的車子壞了!!!

-----------------------------------------------------------------------------------
一女從計程車下車,司機突然從車里探出頭:小姐,你像雞!
女臉一紅,罵到:你他X的像隻鴨。
司機聽了不爽,然後就快速開走了。








然後女孩突然想起追著車喊:運將,我相機,我相機!
-------------------------------------------------------------------------------------
廣東一夥劫匪在搶劫銀行時說了一句至理名言:“通通不許動,錢是國家的,命是自己的!”
大家一聲不吭躺倒。
劫匪望了一眼躺在桌上四肢朝天的出納小姐,說:“請你躺文明些!這是劫財,又不是劫色!”
劫匪回去後,其中一個新來的碩士劫匪說:“老大,我們趕快數一下搶了多少錢。”
那老劫匪(小學畢業)說:“你傻啊?這麼多,你要數到什麼時候啊?今天晚上看看新聞不就知道了嗎。”

——這就叫工作經驗,這年頭工作經驗比學歷更重要!!!

劫匪走後,行長說,趕緊報案!
主任剛要走,行長急忙說:“等等!把我們上次私自挪用的五千萬也加上去!”
主任會意,陰笑道:“要是劫匪每個月都來搶一回就好了。”
第二天,新聞聯播報導銀行被搶了七千萬。
劫匪數來數去只有兩千萬。
老大罵道:“媽的,老子拼了一條命才搶了兩千萬,銀行行長動動嘴皮就賺了五千萬,看來這年頭還是要讀書啊!

---------------------------------------------------------------------------------------------------
高官兒子愛逃學說謊。為此高官高價買一測謊機器人。

一日兒晚歸。
父:去哪裡了?
兒:圖書館。
機器人一巴掌摑過去。

兒:去同學家看A片了。
父:膽子好大,我長這麼大從未看過。
機器人隨即給其父一巴掌。

母怒斥父說:活該,對兒子這麼苛刻。再怎麼說他都是你親生的呀!

啪!機器人又給其母一個大耳光!
-----------------------------------------------------------------------------------------------------------------



星期四, 五月 05, 2011

Flex3 Localization

http://www.herrodius.com/blog/123


1. create a folder in your project src to store your resource bundles (e.g. "locale")
2. create a subfolder for each locale you want to implement (e.g. "locale\en_US" and "locale\zh_TW")
3. in the locale subfolder, create a resources.properties file and save it as UTF-8
4. enter the resources in the form of "key=value", like Ant property files
example:

# locale/en_US/resources.properties
login_id=id
password=password

5. update the compiler settings: -locale=en_US,zh_TW -source-path=locale/{locale}


[ResourceBundle("resources")]



< mx:FormItem label="{resourceManager.getString('resources', 'login_id')}" ....


switch your locale at runtime by setting the localeChain property on the resourceManager:

resourceManager.localeChain = ["en_US"];
//resourceManager.localeChain = ["zh_TW"];


Set focus on textfield in Flex3

In Action Script:
login.setFocus();

In HTML:






< body onload="setFlexAppFocus();">


2. detect press Enter
   

/**
* Handle login when user press enter on password
*/
private function enterListener(e:KeyboardEvent):void {
if(e.keyCode == Keyboard.ENTER) {
enter(); // check login function
}
}

星期五, 十一月 05, 2010

利用 vb.net 偵測全域的滑鼠及鍵盤動作

使用windowshooklib

http://www.vbforums.com/showthread.php?t=436321


http://www.code2point.com/Project.aspx?proj=4

使用方法就是下載其 WindowsHookLib.dll , 在 .net 專案中加入此參考,





Imports WindowsHook

Public Class Form1
Dim WithEvents mHook As New MouseHook

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
'Install the mouse hook
mHook.InstallHook()
Catch ex As Exception
MessageBox.Show("Failed to install the mouse hook!." _
& Environment.NewLine & ex.Message, "Hook Error!", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
'Remove the mouse hook
mHook.RemoveHook()
Catch ex As Exception
MessageBox.Show("Error removing the mouse hook!." _
& Environment.NewLine & ex.Message, "Hook Error!", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

End Sub

Private Sub mHook_MouseDown(ByVal sender As Object, ByVal e As WindowsHook.MouseEventArgs) Handles mHook.MouseDown
'Set the Handled property for the mouse down event
'to block the mouse down message for the
'controls that are not in the alowedList.
'e.Handled = Not Me.alowedList.Contains(CType(sender, IntPtr))
'Do some other things here
'...
'Me.TextBox1.Text = "mouse down"
Me.TextBox1.Text = e.Button.ToString
'...
End Sub
End Class


星期三, 七月 14, 2010

半形轉全形 (php)




function n_to_w($strs, $types = '0'){ // narrow to wide , or wide to narrow
$nt = array(
"(", ")", "[", "]", "{", "}", ".", ",", ";", ":",
"-", "?", "!", "@", "#", "$", "%", "&", "|", "\\",
"/", "+", "=", "*", "~", "`", "'", "\"", "<", ">",
"^", "_",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
"u", "v", "w", "x", "y", "z",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z",
" "
);
$wt = array(
"(", ")", "〔", "〕", "{", "}", "﹒", ",", ";", ":",
"-", "?", "!", "@", "#", "$", "%", "&", "|", "\",
"/", "+", "=", "*", "~", "、", "、", """, "<", ">",
"︿", "_",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
"u", "v", "w", "x", "y", "z",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z",
" "
);

if ($types == '0'){
// narrow to wide
$strtmp = str_replace($nt, $wt, $strs);
}else{
// wide to narrow
$strtmp = str_replace($wt, $nt, $strs);
}
return $strtmp;
}

台北城中扶輪社網址

台北城中扶輪社
Rotary Club of Taipei Castle
www.taipei-castle.org.tw

星期二, 六月 22, 2010

超肥的Microsoft Office 輸入法 2010

真是厲害, 輸入法竟然能搞到 131.2 MB

星期二, 九月 15, 2009

Using MD5 in C language




http://mixtt.blogspot.com/2009/06/c-using-md5-in-c-language.html





#include <openssl/md5.h>
/*Encode str to str_enc using md5 algorithm*/
void md5(char *str, char *str_enc) {
int i = 0;
unsigned char d[16];
MD5_CTX ctx;
MD5_Init(&ctx);
MD5_Update (&ctx, (char *) str, strlen(str));
MD5_Final(d, &ctx);
for (i = 0; i < 16; i++) {
sprintf(str_enc + (i*2), "%02X", d[i]);
}
str_enc[32] = 0;
}

Use C language to get MAC Address in linux





#include <stdio.h>
#include <string.h> /* for strncpy */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>

int main(void) {
int fd;
struct ifreq ifr;

char mac[20];
fd = socket(AF_INET, SOCK_DGRAM, 0);

ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);

ioctl(fd, SIOCGIFHWADDR, &ifr);

close(fd);

sprintf(mac,"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
(unsigned char)ifr.ifr_hwaddr.sa_data[0],
(unsigned char)ifr.ifr_hwaddr.sa_data[1],
(unsigned char)ifr.ifr_hwaddr.sa_data[2],
(unsigned char)ifr.ifr_hwaddr.sa_data[3],
(unsigned char)ifr.ifr_hwaddr.sa_data[4],
(unsigned char)ifr.ifr_hwaddr.sa_data[5]);

printf("eth0 mac address:%s\n", mac);

return 0;
}