common.h:
- C/C++ code
//头部标识const unsigned short Head_Identifier = 0x0808;#ifdef _MSC_VER//在src指向的内存中查找由sub指向的内存数据一致的地方unsigned char *memmem(const char *src, unsigned long srcSize, const void *sub, unsigned long subSize);#elseint GetPrivateProfileString( const char *lpSectionName,// section name const char *lpKeyName, // key name const char *lpDefault, // default string char *lpReturnedString, // destination buffer int nSize, // size of destination buffer const char *lpFileName // initialization file name );#endif
common.cpp
- C/C++ code
#ifdef _MSC_VER/*********************************************************函数名: memmem描述: 在src指向的内存中查找由sub指向的内存数据一致的地方输入: src:要被查找的内存首地址,不被能为空 srcSize:src指向的内存长度 sub:要查询的内容的地址 subSize:sub指向的内存长度返回: src第一个匹配的内存地址,没有找到返回NULL创建时间:20111214创建者:panjingwei修改者:修改时间:其它:*********************************************************/unsigned char *memmem(const void *src, unsigned long srcSize, const void *sub, unsigned long subSize){ if (NULL==src || NULL==sub || srcSize<subSize) { return NULL; } unsigned char *ptr = (unsigned char *)src; for (unsigned long i=0; i<=(srcSize-subSize); i++) { if (0 != memcmp(ptr, sub, subSize)) { ptr++; } else { return ptr; } } return NULL;}#else//linux下读ini配置文件,但是只有Key无Section(即lpSectionName为NULL),如果有注释必须在独立行上,不能加到末尾int GetPrivateProfileString(const char *lpSectionName, const char *lpKeyName, const char *lpDefault, char *lpReturnedString, int nSize, char *lpFileName){ FILE * fd = fopen(lpFileName, "r"); if(!fd) { strcpy(lpReturnedString, lpDefault); return faile; } char strline[256] = {0}; while (NULL != fgets(strline, 256, fd)) { char *tmp = NULL; //去掉所有空格 while (NULL != (tmp = strchr(strline, ' '))) { strcpy(tmp, tmp+1); } //此行是注释,继续找下一行 if('#' == strline[0]) { continue; } char *pEqualMark = strchr(strline, '=');//查找第一个= if (NULL == pEqualMark) { continue; } else { pEqualMark[0] = 0; if (0 == strcmp(lpKeyName, strline)) { strncpy(lpReturnedString, pEqualMark+1, nSize); } else { continue; } } } fclose(fd); strcpy(lpReturnedString, lpDefault); return faile;} #endif
建立了一个RtuClient的MFC工程:然后在
UINT CRtuClientApp::RecvDataThread(LPVOID pParam)函数里面调用memmem函数,编译错误:
{
char *recvbuf = new char[10240];
UINT recvlen = 0;
char *phead = (char *)memmem(recvbuf, recvlen, (void *)&Head_Identifier, sizeof(Head_Identifier));
}
-------------------Configuration: rtuClient - Win32 Debug--------------------
Linking...
rtuClient.obj : error LNK2001: unresolved external symbol "unsigned char * __cdecl memmem(char const *,unsigned long,void const *,unsigned long)" (?memmem@@[email protected])
Debug/rtuClient.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
rtuClient.exe - 2 error(s), 0 warning(s)
------解决方案--------------------
unsigned char *memmem(const char *src, unsigned long srcSize, const void *sub, unsigned long subSize);