zoukankan      html  css  js  c++  java
  • # 2017-2018-2 20155228 《信息安全系统设计原理》 使用VirtualStudio2008创建和调用静态库和使用VirtualC++6.0创建和调用动态库

    使用virtual c++ 6.0创建和调用动态库

    不得不说一下关于环境的问题

    只要我打一个响指,一半的安装在win7上的VC6.0都会因为兼容性问题直接崩掉

    懒得研究怎么解决兼容性的问题了,直接开一个winXP虚拟机完美运行vc6.0,省时省心,岂不美哉

    研究大佬的博客的时候

    Windows下静态库与动态库的创建与使用

    尝试使用.def文件生成动态库并使用隐式链接到工程时,发现这个方法仅适用于动态库所在的工程和调用动态库的工程同时处于一个工作空间

    如图所示,0527helloworld是动态库所在的工程,0527testhelloworld是调用动态库的工程,两个工程都是处于名为0527helloworld的工作空间里面的

    #include "stdafx.h"
    #include "../0527helloworld/0527helloworld.h"
    
    int main(int argc, char* argv[])
    {
        int iRet;
        printf("Hello World!
    ");
        iRet = cmp(3,8);
        printf("return value is: %d
    ",iRet);
        return 0;
    }
    

    可以看到声明头文件的时候是声明了位于其他工程的头文件:#include "../0527helloworld/0527helloworld.h"

    这应该就是其他工作空间的工程就算把.dll和.lib拷到工作目录下也添加了.h文件也无法调用动态库的原因

    后来在网上找到另外一个方案,解决了这个问题

    VC++ 6.0如何创建与调用动态链接库

    现在简单介绍一下这个方案的流程

    算了,今天太晚了我要睡觉了,明天再说


    20180531更新

    关于在VirtualStudio2008生成和使用静态库的方法在网上有很多,但是很多都没有用,这一篇博客有用

    vs2008添加连接静态库和动态库


    20180601更新

    今天就把使用VC6.0创建和使用动态库的问题写成傻瓜教程,不用带着脑子,直接跟着一步一步做就可以了

    我想写一个名为SUM的求和函数,输入是两个int类型变量,输出是一个int类型变量,函数代码如下:

    int SUM(int a,int b)
    {
        return a+b;
    }
    

    1. 创建动态库

    1.1 新建动态库项目0601testDLLver3

    打开VC6.0,左上角菜单栏:文件->新建

    项目创建完成,切换到file view,可以看到一堆预生成的文件,只需要对其中三个文件进行修改

    1.2 修改0601testDLLver3.h文件

    // 0601testDLLver3.h : main header file for the 0601TESTDLLVER3 DLL
    //
    
    #if !defined(AFX_0601TESTDLLVER3_H__16DBBD8E_146C_4508_8E8C_20D288314FEF__INCLUDED_)
    #define AFX_0601TESTDLLVER3_H__16DBBD8E_146C_4508_8E8C_20D288314FEF__INCLUDED_
    
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    
    #ifndef __AFXWIN_H__
    	#error include 'stdafx.h' before including this file for PCH
    #endif
    
    #include "resource.h"		// main symbols
    
    /////////////////////////////////////////////////////////////////////////////
    // CMy0601testDLLver3App
    // See 0601testDLLver3.cpp for the implementation of this class
    //
    
    class CMy0601testDLLver3App : public CWinApp
    {
    public:
    	CMy0601testDLLver3App();
    
    // Overrides
    	// ClassWizard generated virtual function overrides
    	//{{AFX_VIRTUAL(CMy0601testDLLver3App)
    	//}}AFX_VIRTUAL
    
    	//{{AFX_MSG(CMy0601testDLLver3App)
    		// NOTE - the ClassWizard will add and remove member functions here.
    		//    DO NOT EDIT what you see in these blocks of generated code !
    	//}}AFX_MSG
    	DECLARE_MESSAGE_MAP()
    };
    
    extern "C" __declspec(dllexport) int __stdcall SUM(int a,int b);
    
    /////////////////////////////////////////////////////////////////////////////
    
    //{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
    
    #endif // !defined(AFX_0601TESTDLLVER3_H__16DBBD8E_146C_4508_8E8C_20D288314FEF__INCLUDED_)
    

    1.3 修改0601testDLLver3.def文件

    ; 0601testDLLver3.def : Declares the module parameters for the DLL.
    
    LIBRARY      "0601testDLLver3"
    DESCRIPTION  '0601testDLLver3 Windows Dynamic Link Library'
    
    EXPORTS
        ; Explicit exports can go here
    	SUM
    

    1.4 修改0601testDLLver3.cpp文件

    // 0601testDLLver3.cpp : Defines the initialization routines for the DLL.
    //
    
    #include "stdafx.h"
    #include "0601testDLLver3.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    //
    //	Note!
    //
    //		If this DLL is dynamically linked against the MFC
    //		DLLs, any functions exported from this DLL which
    //		call into MFC must have the AFX_MANAGE_STATE macro
    //		added at the very beginning of the function.
    //
    //		For example:
    //
    //		extern "C" BOOL PASCAL EXPORT ExportedFunction()
    //		{
    //			AFX_MANAGE_STATE(AfxGetStaticModuleState());
    //			// normal function body here
    //		}
    //
    //		It is very important that this macro appear in each
    //		function, prior to any calls into MFC.  This means that
    //		it must appear as the first statement within the 
    //		function, even before any object variable declarations
    //		as their constructors may generate calls into the MFC
    //		DLL.
    //
    //		Please see MFC Technical Notes 33 and 58 for additional
    //		details.
    //
    
    /////////////////////////////////////////////////////////////////////////////
    // CMy0601testDLLver3App
    
    extern "C" __declspec(dllexport) int __stdcall SUM(int a,int b)
    {
    	return a+b;
    }
    
    BEGIN_MESSAGE_MAP(CMy0601testDLLver3App, CWinApp)
    	//{{AFX_MSG_MAP(CMy0601testDLLver3App)
    		// NOTE - the ClassWizard will add and remove mapping macros here.
    		//    DO NOT EDIT what you see in these blocks of generated code!
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    /////////////////////////////////////////////////////////////////////////////
    // CMy0601testDLLver3App construction
    
    CMy0601testDLLver3App::CMy0601testDLLver3App()
    {
    	// TODO: add construction code here,
    	// Place all significant initialization in InitInstance
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // The one and only CMy0601testDLLver3App object
    
    CMy0601testDLLver3App theApp;
    
    
    
    
    

    1.5 编译代码创建动态库

    使用一阳指大力出奇迹按下f7开始编译

    生成动态库文件0601testDLLver3.lib和0601testDLLver3.dll放在0601testDLLver3工程目录下的debug文件夹中

    2. 使用动态库

    2.1 创建测试工程0601testDLLver4

    项目创建完成,切换到file view,可以看到预生成的文件0601testDLLver4.cpp,除了对这个文件进行修改我们还要创建一个0601testDLLver4.h头文件

    2.2 把动态库文件0601testDLLver3.lib和0601testDLLver3.dll放在0601testDLLver4工程目录下

    2.3 新建0601testDLLver4.h文件

    #include "stdafx.h"
    
    #pragma comment(lib,"0601testDLLver3.lib")
    
    extern "C" _declspec(dllimport) int _stdcall SUM(int a,int b);
    

    2.4 修改0601testDLLver4.cpp文件

    #include "stdafx.h"
    #include <stdio.h>
    #include "0601testDLLver4.h"
    
    int main(int argc, char* argv[])
    {
    	printf("Hello World!
    ");
    	int a=1;
    	int b=2;
    	int c=SUM(a,b);
    	printf("%d",c);
    	return 0;
    }
    

    2.5 编译运行,万事大吉


    20180602更新

    使用VirtualStudio2008创建和调用静态库

    1. 创建静态库

    1.1 创建静态库项目0602RSAlib

    打开VS2008,左上角菜单栏:文件->新建

    项目创建完成,切换到解决方案资源管理器,可以看到一些预生成的文件,只需要创建两个新文件0602RSAlib.h和0602RSAlib.cpp

    1.2 创建0602RSAlib.cpp文件

    #include "stdafx.h"
    #include <stdio.h>
    
    int MessageToCipher(void)
    {
        int p,q,e,d,m,n,t,c,r;
        int x,y,z;
        char s;
        printf("请输入P和Q的值,中间用空格隔开,P和Q的值不相同且P和Q都是素数:");
        scanf("%d%d",&p,&q);
        printf("计算得到N的值为%d
    ",n);
        printf("计算得到T的值为%d
    ",t);
        printf("请输入E的值:");
        scanf("%d",&e);
        if(1==1)
        {
            printf("输入不合要求,请重新输入
    ",t);
            scanf("%d",&e);
        }
        d=1;
        while(((e*d)%t)!=1)
        {
            d++;
        }
        printf("计算得到D的值为%d
    请输入明文M的值:",d);
        scanf("%d",&m);
        x=m;
        y=e;
        z=n;
        c=1;
        y=y+1;
        while(y!=1)
        {
            c=c*x;
            c=c%z;
            y--;
        }
        return c;
    }
    
    int CipherToMessage(void)
    {
        int p,q,e,d,m,n,t,c,r;
        int x,y,z;
        char s;
        printf("请输入P和Q的值,中间用空格隔开,P和Q的值不相同且P和Q都是素数:");
        scanf("%d%d",&p,&q);
        printf("计算得到N的值为%d
    ",n);
        printf("计算得到T的值为%d
    ",t);
        printf("请输入E的值:");
        scanf("%d",&e);
        if(1==1)
        {
            printf("输入不合要求,请重新输入
    ",t);
            scanf("%d",&e);
        }
        d=1;
        while(((e*d)%t)!=1)
        {
            d++;
        }
        printf("计算得到D的值为%d
    请输入密文C的值:",d);
        scanf("%d",&c);
        x=c;
        y=d;
        z=n;
        m=1;
        y=y+1;
        while(y!=1)
        {
            m=m*x;
            m=m%z;
            y--;
        }
        return m;
    }
    
    

    1.3 创建0602RSAlib.h文件

    #include "stdafx.h"
    
    int MessageToCipher(void);
    int CipherToMessage(void);
    
    

    1.4 编译代码创建静态库

    生成静态库文件0602RSAlib.lib放0602RSAlib工程目录下的debug文件夹中

    另外0602RSAlib工程目录下的0602RSAlib.h待会也会用到

    2. 调用静态库

    2.1 创建测试工程0602testRSAlib

    2.2 把静态库文件0602RSAlib.lib和0602RSAlib.h放在0602testRSAlib工程目录下

    2.3 修改0602testRSAlib.cpp文件

    // 0602testRSAlib.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include "0602RSAlib.h"
    #include <stdio.h>
    #pragma comment(lib,"0602RSAlib.lib")
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int temp;
        printf("非对称密码算法RSA
    ");
    A:
        printf("请输入数字1或0选择进行加密运算或是解密运算:");
        scanf("%d",&temp);
        if(temp==1)
        {
            int c=MessageToCipher();
            printf("密文C的值为:%d
    ",c);
            goto A;
        }
        else if(temp==0)
        {
            int m=CipherToMessage();
            printf("明文M的值为:%d
    ",m);
            goto A;
        }
        else
        {
            printf("输入不合要求,请重新输入
    ");
            goto A;
        }
    	return 0;
    }
    

    2.4 编译运行,万事大吉

  • 相关阅读:
    使用Spring Boot来加速Java web项目的开发
    Spring IOC的理解
    CentOS查看系统信息-CentOS查看命令
    mybatis传递Map和List集合示例
    java文件读写工具类
    MongoDB数据库操作
    linux中给PHP安装mongodb的扩展
    深度学习与计算机视觉系列(2)_图像分类与KNN
    深度学习与计算机视觉系列(3)_线性SVM与SoftMax分类器
    深度学习与计算机视觉系列(6)_神经网络结构与神经元激励函数
  • 原文地址:https://www.cnblogs.com/besti20155228/p/9098026.html
Copyright © 2011-2022 走看看