分类:
2007-03-19 09:36:03
/*********************************** Filename : String.h Description : Author : HCJ Date : 2006-5-7 ************************************/ int Strlen(char *pStr); int StrNlen(char *pStr, unsigned long ulMaxLen); |
/************************************** Filename : get string length Description : Author : HCJ Date : 2006/5/7 **************************************/ #include #include int Strlen(char *pStr) { unsigned long ulLength; assert(NULL != pStr); ulLength = 0; while(*pStr++) { ulLength++; } return ulLength; } |
********************************************** Fileneme: mystrnlen.c Description: get input string length,if string large max length input return max length, else real length Author: HCJ Date : 2006-5-7 **********************************************/ #include #include int StrNlen(char *pStr, unsigned long ulMaxLen) { unsigned long ulLength; assert(NULL != pStr); if(ulMaxLen <= 0) { printf("Wrong Max Length!\n"); return -1; } ulLength = 0; while(*pStr++ && ulLength < ulMaxLen) { ulLength++; } return ulLength; } |
/***************************************** FileName: main.c Description: test static/dynamic library Author: HCJ Date : 2005-5-7 ******************************************/ #include #include int main(int argc, char* argv[]) { char str[] = {"hello world"}; unsigned long ulLength = 0; printf("The string is : %s\n", str); ulLength = Strlen(str); printf("The string length is : %d(use Strlen)\n", ulLength); ulLength = StrNlen(str, 10); printf("The string length is : %d(use StrNlen)\n", ulLength); return 0; } |
/***************************************** FileName: main2.c Description: test static/dynamic library Author: HCJ Date : 2005-5-7 ******************************************/ #include #include int main(int argc, char* argv[]) { //define function pointor int (*pStrlenFun)(char* pStr); // int (*pStrnlenFun)(char* pStr, int ulMaxLen); char str[] = {"hello world"}; unsigned long ulLength = 0; void *pdlHandle; char *pszErr; pdlHandle = dlopen("./libstr.so", RTLD_LAZY); //加载链接库/libstr.so if(!pdlHandle) { printf("Failed load library\n"); } pszErr = dlerror(); if(pszErr != NULL) { printf("%s\n", pszErr); return 0; } //get function from lib pStrlenFun = dlsym(pdlHandle, "Strlen"); //获取函数的地址 pszErr = dlerror(); if(pszErr != NULL) { printf("%s\n", pszErr); return 0; } pStrnlenFun = dlsym(pdlHandle, "StrNlen"); pszErr = dlerror(); if(pszErr != NULL) { printf("%s\n", pszErr); return 0; } printf("The string is : %s\n", str); ulLength = pStrlenFun(str); //调用相关的函数 printf("The string length is : %d(use Strlen)\n", ulLength); ulLength = pStrnlenFun(str, 10); printf("The string length is : %d(use StrNlen)\n", ulLength); dlclose(pdlHandle); return 0; } |
gcc -o mian2 -ldl main2.c