VC6.0做法:
File--新建---工程--win32 Dynamic-link Library 输入工程名和位置,即可进入动态库界面。
以16进制转10进制为例
建立C程序或C++库文件。对于库文件最好用C写,可移植性更强。
- #include<stdio.h>
- #include<string.h>
- #include<math.h>
- #include "Hextoint.h"
-
-
- /*计算字符串是否有四个字节*/
- int GetLeghtofHEX(char *p)
- {
- int count=0;
- char *temp =p;
- while(*temp++!='\0')
- count++;
- return count ;
- }
- /*如果不足四个字节,前4-count位填0*/
- char *modifbit(char *p)
- {
- char tab[5];
- int i=0,j=0;
- char * temp;
- int count=GetLeghtofHEX(p);
- if (count!=4)
- {
- for(i=0;i<4-count;i++)
- tab[i]='0';
- while(i<4)
- {
- tab[i]=*(p+j);
- j++;
- i++;
- }
- tab[i]='\0';
- temp = tab;
-
- }
- else
- temp=p;
- return temp;
- }
-
- int Hexstoinit(char *p)
- {
- int i,j=0;
- char string[5]="0000";
- int Dec=0,temp;
- char *tempoint=p;
- char *temp2;
- if(strstr(p,"0x"))
- {
- tempoint=strstr(p,"0x")+2;
- }
- printf("string=%s\n",tempoint);
- temp2=modifbit(tempoint);
- //printf("count=%s\n",temp2);
- tempoint=temp2;
- for(i=3;i>=0;i--)
- {
- string[j++]=*(tempoint+i);
- }
- string[4]='\0' ;
-
- printf("string1=%s\n",string);
- for(j=0;j<4;j++)
- {
- if(string[j]<='9')
- {
- temp=string[j]-'0';
- }
- else if(string[j]=='a'||string[j]=='A')
- temp=10;
- else if(string[j]=='b'||string[j]=='B')
- temp=11;
- else if(string[j]=='c'||string[j]=='C')
- temp=12;
- else if(string[j]=='d'||string[j]=='D')
- temp=13;
- else if(string[j]=='e'||string[j]=='E')
- temp=14;
- else if(string[j]=='f'||string[j]=='F')
- temp=15;
-
- Dec+=temp*pow(16,j);
- }
- printf("string=%d\n",Dec);
- return Dec;
- }
并将C实现添加到工程。建立头文件,并添加到工程
- #ifndef __HEXTOINT_H
- #define __HEXTOINT_H
- extern "C" _declspec(dllexport) int Hexstoinit(char *p); /*必需声明为动态库export*/
-
- #endif
头文件就是对外的接口,提供给对外使用的手册
编译后,在Debug目录下就产生了DLLlib.dll动态库DLLlib.lib 注意这里的.lib不是真正的静态库,它只是.dll的一个符号表。
动态库使用方法:
文件---新建---win32 application
创建C应用代码,并添加到头文件
将 .h,.lib,.dll都放到当前目录下
- #include<stdio.h>
- #include"Hextoint.h"
- #pragma comment(lib,"DLLlib.lib")/*这里的.lib只是一个符号表,不是一个真正的静态库*/
- void main()
- {
- printf("hello,This is %d",Hexstoinit("0xffF"));
- return ;
- }
- #ifndef __HEXTOINT_H
- #define __HEXTOINT_H
-
- extern "C"_declspec(dllimport) int Hexstoinit(char *p);
- #endif
VC2008的做法
类似VC6.0
创建一个动态库
文件--新建---工程---win32 project---根据向导选择win32 Dynamic-link library
然后project--add new item 添加头文件和C文件
编译后,在Debug目录下就产生了MyDLL.dll动态库和MyDLL.礼拜。
- #ifndef __HEXTOINT_H
- #define __HEXTOINT_H
- #define ICILIB_UTIL_API __declspec(dllexport)
- extern "C" ICILIB_UTIL_API int Hexstoinit(char *p);
- #endif
- #include<stdio.h>
- #include<string.h>
- #include<math.h>
- #include "MyDLL.h"
-
-
- /*计算字符串是否有四个字节*/
- int GetLeghtofHEX(char *p)
- {
- int count=0;
- char *temp =p;
- while(*temp++!='\0')
- count++;
- return count ;
- }
- /*如果不足四个字节,前4-count位填0*/
- char *modifbit(char *p)
- {
- char tab[5];
- int i=0,j=0;
- char * temp;
- int count=GetLeghtofHEX(p);
- if (count!=4)
- {
- for(i=0;i<4-count;i++)
- tab[i]='0';
- while(i<4)
- {
- tab[i]=*(p+j);
- j++;
- i++;
- }
- tab[i]='\0';
- temp = tab;
-
- }
- else
- temp=p;
- return temp;
- }
-
- int Hexstoinit(char *p)
- {
- int i,j=0;
- char string[5]="0000";
- int Dec=0,temp;
- char *tempoint=p;
- char *temp2;
- if(strstr(p,"0x"))
- {
- tempoint=strstr(p,"0x")+2;
- }
- printf("string=%s\n",tempoint);
- temp2=modifbit(tempoint);
- //printf("count=%s\n",temp2);
- tempoint=temp2;
- for(i=3;i>=0;i--)
- {
- string[j++]=*(tempoint+i);
- }
- string[4]='\0' ;
-
- printf("string1=%s\n",string);
- for(j=0;j<4;j++)
- {
- if(string[j]<='9')
- {
- temp=string[j]-'0';
- }
- else if(string[j]=='a'||string[j]=='A')
- temp=10;
- else if(string[j]=='b'||string[j]=='B')
- temp=11;
- else if(string[j]=='c'||string[j]=='C')
- temp=12;
- else if(string[j]=='d'||string[j]=='D')
- temp=13;
- else if(string[j]=='e'||string[j]=='E')
- temp=14;
- else if(string[j]=='f'||string[j]=='F')
- temp=15;
-
- Dec+=temp*pow(16.0,j);
- }
- printf("string=%d\n",Dec);
- return Dec;
- }
使用动态库
文件--新建---工程---win32 project---根据向导选择win32 application
然后project--add new item 添加头文件和C文件,将头文件,.dll动态库, .lib 符号表文件放在和.cpp所在的工程下
编译后,链接,执行即可 (执行是.dll.和.exe必需在同一个目录下才能执行)
- #ifndef __HEXTOINT_H
- #define __HEXTOINT_H
-
- extern "C"_declspec(dllimport) int Hexstoinit(char *p);
- #endif
-
- // testmydll.cpp : Defines the entry point for the console application.
- //
-
- #include "stdafx.h"
- #include<stdio.h>
- #include"MyDLL.h"
- #pragma comment(lib,"MyDLL.lib")
- int _tmain(int argc, _TCHAR* argv[])
- {
- printf("hello,This is %d",Hexstoinit("0xffF"));
- return 0;
- }
-
-
注意:动态库引用,仅在编译APP和执行APP是链接到DLL,, 所以这个.exe 是包括APP+l动态库的符号表,故该镜像比较小,执行时必需和DLL库放在同一个目录下,多个APP都可以链接它,便于程序的共享。
注意:也可以不在代码中通过#pragma comment显示指出,可以在工程中设置配置库和头文件路径
如果不用#pragma comment指定,则可以直接在VC++中设置,如图2,依次选择tools、options、directories、library files菜单或选项,填入库文件路径。图2中加红圈的部分为我们添加的libTest.lib文件的路径。