Index
itoa,
MakeIntegerFromFloat,
msleep,
struppercase,
strlowercase,
Country code,
Windown Main function for communicating over the comm port with TCPIP
Index to examples from my own projects
Platforms and frameworks for C and C++
Anno 1988 I started with Turbo C, with DOS
Anno 2000 I was using the lcc compiler and the DevCpp compiler, with Windows
Anno 2005 I started using Borland C++, with Windows
Anno 2009 I started using OpenWatcom, with Windows
Anno 2010 I started with GCC, with Windows and Linux-intel and Linux-arm9
Frameworks for Windows: Visual C++, CodeLite
Framework for Ubuntu: CodeLite
itoa
Itoa makes an integer from an array of type char
/* reverse: reverse string s in place */
void reverse(char s[])
{
int c, i, j;
for (i = 0, j = strlen(s)-1; i 0); /* delete it */
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
Make an Integer From a floating point value
int MakeIntegerFromFloat(float dValue)
{
return (int)(dValue>=0 ? dValue+0.5 : dValue-0.5);
}
msleep
void msleep(int iQuantity)
{
struct timespec req; // 0.001s
if (iQuantity <= 999)
{
req.tv_sec = 0;
req.tv_nsec = iQuantity * 1000000;
}
else {
req.tv_sec = iQuantity / 1000;
req.tv_nsec = (iQuantity % 1000) * 1000000;
}
nanosleep(&req, NULL);
}
StringLowerCase
void strlowercase(char *si)
{
char *s;
s = si;
while ((*s)!='\0')
{
(*s) = tolower(*s);
s++;
}
}
StringUpperCase
void struppercase(char *si)
{
char *s;
s = si;
while ((*s)!='\0')
{
(*s) = toupper(*s);
s++;
}
}
Country code
struct strCountryComb {
char *sCountry;
char *sAbbr;
int iCode;
int iTZflag;
};
// From countrycode.org
static struct strCountryComb cb[] = {
{ "Afghanistan","AF",93,0 },
{ "Albania","AL",355,0 },
{ "Algeria","DZ",213,0 },
{ "American Samoa","AS",1684,0 },
{ "Andorra","AD",376,0 },
{ "Angola","AO",244,0 },
{ "Anguilla","AI",1264,0 },
{ "Antarctica","AQ",672,0 },
{ "Antigua and Barbuda","AG",1268,0 },
{ "Argentina","AR",54,0 },
{ "Armenia","AM",374,0 },
{ "Aruba","AW",297,0 },
{ "Australia","AU",61,0 },
{ "Austria","AT",43,0 },
{ "Azerbaijan","AZ",994,0 },
{ "Bahamas","BS",1242,0 },
{ "Bahrain","BH",973,0 },
{ "Bangladesh","BD",880,0 },
{ "Barbados","BB",1246,0 },
{ "Belarus","BY",375,0 },
{ "Belgium","BE",32,0 },
{ "Belize","BZ",501,0 },
{ "Benin","BJ",229,0 },
{ "Bermuda","BM",1441,0 },
{ "Bhutan","BT",975,0 },
{ "Bolivia","BO",591,0 },
{ "Bosnia and Herzegovina","BA",387,0 },
{ "Botswana","BW",267,0 },
{ "Brazil","BR",55,0 },
{ "British Indian Ocean Territory","IO",0,0 },
{ "British Virgin Islands","VG",1284,0 },
{ "Brunei","BN",673,0 },
{ "Bulgaria","BG",359,0 },
{ "Burkina Faso","BF",226,0 },
{ "Burma (Myanmar)","MM",95,0 },
{ "Burundi","BI",257,0 },
{ "Cambodia","KH",855,0 },
{ "Cameroon","CM",237,0 },
{ "Canada","CA",1,0 },
{ "Cape Verde","CV",238,0 },
{ "Cayman Islands","KY",1345,0 },
{ "Central African Republic","CF",236,0 },
{ "Chad","TD",235,0 },
{ "Chile","CL",56,0 },
{ "China","CN",86,0 },
{ "Christmas Island","CX",61,0 },
{ "Cocos (Keeling) Islands","CC",61,0 },
{ "Colombia","CO",57,0 },
{ "Comoros","KM",269,0 },
{ "Cook Islands","CK",682,0 },
{ "Costa Rica","CR",506,0 },
{ "Croatia","HR",385,0 },
{ "Cuba","CU",53,0 },
{ "Cyprus","CY",357,0 },
{ "Czech Republic","CZ",420,0 },
{ "Democratic Republic of the Congo","CD",243,0 },
{ "Denmark","DK",45,0 },
{ "Djibouti","DJ",253,0 },
{ "Dominica","DM",1767,0 },
{ "Dominican Republic","DO",1809,0 },
{ "Ecuador","EC",593,0 },
{ "Egypt","EG",20,0 },
{ "El Salvador","SV",503,0 },
{ "Equatorial Guinea","GQ",240,0 },
{ "Eritrea","ER",291,0 },
{ "Estonia","EE",372,0 },
{ "Ethiopia","ET",251,0 },
{ "Falkland Islands","FK",500,0 },
{ "Faroe Islands","FO",298,0 },
{ "Fiji","FJ",679,0 },
{ "Finland","FI",358,0 },
{ "France","FR",33,0 },
{ "French Polynesia","PF",689,0 },
{ "Gabon","GA",241,0 },
{ "Gambia","GM",220,0 },
{ "Gaza Strip","",970,0 },
{ "Georgia","GE",995,0 },
{ "Germany","DE",49,0 },
{ "Ghana","GH",233,0 },
{ "Gibraltar","GI",350,0 },
{ "Greece","GR",30,0 },
{ "Greenland","GL",299,0 },
{ "Grenada","GD",1473,0 },
{ "Guam","GU",1671,0 },
{ "Guatemala","GT",502,0 },
{ "Guinea","GN",224,0 },
{ "Guinea-Bissau","GW",245,0 },
{ "Guyana","GY",592,0 },
{ "Haiti","HT",509,0 },
{ "Holy See (Vatican City)","VA",39,0 },
{ "Honduras","HN",504,0 },
{ "Hong Kong","HK",852,0 },
{ "Hungary","HU",36,0 },
{ "Iceland","IS",354,0 },
{ "India","IN",91,0 },
{ "Indonesia","ID",62,0 },
{ "Iran","IR",98,0 },
{ "Iraq","IQ",964,0 },
{ "Ireland","IE",353,0 },
{ "Isle of Man","IM",44,0 },
{ "Israel","IL",972,0 },
{ "Italy","IT",39,0 },
{ "Ivory Coast","CI",225,0 },
{ "Jamaica","JM",1876,0 },
{ "Japan","JP",81,0 },
{ "Jersey","JE",0,0 },
{ "Jordan","JO",962,0 },
{ "Kazakhstan","KZ",7,0 },
{ "Kenya","KE",254,0 },
{ "Kiribati","KI",686,0 },
{ "Kosovo","KS",381,0 },
{ "Kuwait","KW",965,0 },
{ "Kyrgyzstan","KG",996,0 },
{ "Laos","LA",856,0 },
{ "Latvia","LV",371,0 },
{ "Lebanon","LB",961,0 },
{ "Lesotho","LS",266,0 },
{ "Liberia","LR",231,0 },
{ "Libya","LY",218,0 },
{ "Liechtenstein","LI",423,0 },
{ "Lithuania","LT",370,0 },
{ "Luxembourg","LU",352,0 },
{ "Macau","MO",853,0 },
{ "Macedonia","MK",389,0 },
{ "Madagascar","MG",261,0 },
{ "Malawi","MW",265,0 },
{ "Malaysia","MY",60,0 },
{ "Maldives","MV",960,0 },
{ "Mali","ML",223,0 },
{ "Malta","MT",356,0 },
{ "Marshall Islands","MH",0,0 },
{ "Mauritania","MR",222,0 },
{ "Mauritius","MU",230,0 },
{ "Mayotte","YT",262,0 },
{ "Mexico","MX",52,0 },
{ "Micronesia","FM",691,0 },
{ "Moldova","MD",373,0 },
{ "Monaco","MC",377,0 },
{ "Mongolia","MN",976,0 },
{ "Montenegro","ME",382,0 },
{ "Montserrat","MS",1664,0 },
{ "Morocco","MA",212,0 },
{ "Mozambique","MZ",258,0 },
{ "Namibia","NA",264,0 },
{ "Nauru","NR",674,0 },
{ "Nepal","NP",977,0 },
{ "Netherlands","NL",31,0 },
{ "Netherlands Antilles","AN",599,0 },
{ "New Caledonia","NC",687,0 },
{ "New Zealand","NZ",64,0 },
{ "Nicaragua","NI",505,0 },
{ "Niger","NE",227,0 },
{ "Nigeria","NG",234,0 },
{ "Niue","NU",683,0 },
{ "Norfolk Island","NF",672,0 },
{ "North Korea","KP",850,0 },
{ "Northern Mariana Islands","MP",1670,0 },
{ "Norway","NO",47,0 },
{ "Oman","OM",968,0 },
{ "Pakistan","PK",92,0 },
{ "Palau","PW",680,0 },
{ "Panama","PA",507,0 },
{ "Papua New Guinea","PG",675,0 },
{ "Paraguay","PY",595,0 },
{ "Peru","PE",51,0 },
{ "Philippines","PH",63,0 },
{ "Pitcairn Islands","PN",870,0 },
{ "Poland","PL",48,0 },
{ "Portugal","PT",351,0 },
{ "Puerto Rico","PR",1,0 },
{ "Qatar","QA",974,0 },
{ "Republic of the Congo","CG",242,0 },
{ "Romania","RO",40,0 },
{ "Russia","RU",7,0 },
{ "Rwanda","RW",250,0 },
{ "Saint Barthelemy","BL",590,0 },
{ "Saint Helena","SH",290,0 },
{ "Saint Kitts and Nevis","KN",1869,0 },
{ "Saint Lucia","LC",1758,0 },
{ "Saint Martin","MF",1599,0 },
{ "Saint Pierre and Miquelon","PM",508,0 },
{ "Saint Vincent and the Grenadines","VC",1784,0 },
{ "Samoa","WS",685,0 },
{ "San Marino","SM",378,0 },
{ "Sao Tome and Principe","ST",239,0 },
{ "Saudi Arabia","SA",966,0 },
{ "Senegal","SN",221,0 },
{ "Serbia","RS",381,0 },
{ "Seychelles","SC",248,0 },
{ "Sierra Leone","SL",232,0 },
{ "Singapore","SG",65,0 },
{ "Slovakia","SK",421,0 },
{ "Slovenia","SI",386,0 },
{ "Solomon Islands","SB",677,0 },
{ "Somalia","SO",252,0 },
{ "South Africa","ZA",27,0 },
{ "South Korea","KR",82,0 },
{ "Spain","ES",34,0 },
{ "Sri Lanka","LK",94,0 },
{ "Sudan","SD",249,0 },
{ "Suriname","SR",597,0 },
{ "Svalbard","SJ",0,0 },
{ "Swaziland","SZ",268,0 },
{ "Sweden","SE",46,0 },
{ "Switzerland","CH",41,0 },
{ "Syria","SY",963,0 },
{ "Taiwan","TW",886,0 },
{ "Tajikistan","TJ",992,0 },
{ "Tanzania","TZ",255,0 },
{ "Thailand","TH",66,0 },
{ "Timor-Leste","TL",670,0 },
{ "Togo","TG",228,0 },
{ "Tokelau","TK",690,0 },
{ "Tonga","TO",676,0 },
{ "Trinidad and Tobago","TT",1868,0 },
{ "Tunisia","TN",216,0 },
{ "Turkey","TR",90,0 },
{ "Turkmenistan","TM",993,0 },
{ "Turks and Caicos Islands","TC",1649,0 },
{ "Tuvalu","TV",688,0 },
{ "US Virgin Islands","VI",1340,0 },
{ "Uganda","UG",256,0 },
{ "Ukraine","UA",380,0 },
{ "United Arab Emirates","AE",971,0 },
{ "United Kingdom","GB",44,0 },
{ "United States","US",1,0 },
{ "Uruguay","UY",598,0 },
{ "Uzbekistan","UZ",998,0 },
{ "Vanuatu","VU",678,0 },
{ "Venezuela","VE",58,0 },
{ "Vietnam","VN",84,0 },
{ "Wallis and Futuna","WF",681,0 },
{ "West Bank","",970,0 },
{ "Western Sahara","EH",0,0 },
{ "Yemen","YE",967,0 },
{ "Zambia","ZM",260,0 },
{ "Zimbabwe","ZW",263,0 }
};
Windows Main Function
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
char *s;
char s2[100];
char *cmd=GetCommandLine();
int comnummer;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_COMBYTCPIP, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
if ((s=strstr(cmd,"ombytcpip_"))!=NULL)
{
strcpy(s2,s+10);
if (sscanf(s2,"%d",&comnummer) !=EOF) COMport = comnummer;
}
else s2[0]='\0';
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_COMBYTCPIP);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
Main Function for killing a windows process
int main(int argc,char *argv[])
{
HANDLE prc1;
PROCESSENTRY32 pe1;
HWND hw;
DWORD progid;
DWORD exitcode;
if (argc == 1) {
Usage(argv[0]);
return 1;
}
HandleOptions(argc,argv);
prc1=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if (prc1==NULL)
{
printf("Cannot take a snapshot of processes");
return 1;
}
pe1.dwSize=sizeof(PROCESSENTRY32);
if (Process32First(prc1,&pe1)==FALSE)
{
printf("No processfirst found");
CloseHandle(prc1);
return 2;
}
do {
if (strcmp(argv[1],pe1.szExeFile)==0)
{
progid=pe1.th32ProcessID;
hw=OpenProcess(PROCESS_ALL_ACCESS,TRUE,(DWORD)progid);
if (hw==NULL)
{
printf("Process with id %d not found",(unsigned int)progid);
}
else
{
GetExitCodeProcess(hw,&exitcode);
TerminateProcess(hw,exitcode);
printf("Process %d killed\n",(unsigned int)progid);
}
}
}while(Process32Next(prc1,&pe1)==TRUE);
Sleep(5000);
CloseHandle(prc1);
return 0;
}