C # 예제 C ++ 클래스 라이브러리 호출

 

먼저, 솔루션을 만들고 FrameworkConsoleTest라는 솔루션에 .netframework 프로젝트를 추가 할 수 있습니다. EncryptBase라는 이름의 C ++ 동적 링크 라이브러리 DLL 프로젝트를 추가합니다.

 

둘째, 64 비트 세대의 배치와 C ++ 프로젝트 EncryptBase 세트입니다. (컴퓨터 인 경우 32 비트 시스템이 86에 위치하고 있으며, 64 비트 시스템이 64 있습니다)

 

 

셋째, "현재 선택"시작에 대한 해결책.

 

넷째, EncryptBase 헤더 파일, 오른쪽 - 추가 - 새로운 항목을하는 EncryptBase.h의 추가 헤더.

1, 다음 코드를 추가합니다 :

#ifndef _ENCRYPTBASE_H // 중복 참조를 헤더 파일을 방지하기 위해, _ENRYPTBASE_H 매크로를 정의 
#DEFINE _ENCRYPTBASE_H 

#ifdef와 __cplusplus //이 부분은 당신이하는 CPP 파일의 경우, (__cplusplus를 정의 할 경우, 컴파일러에게하는 
통근자 "C" {// CPP 파일 때문에 다음 컴파일하는 C 언어 사용) 기본적으로 정의 된 매크로 
#endif 다음 


#ifdef와 DLL_EXPORTS   
#DEFINE DLL_EXPORTS __declspec (dllexport)    
#else   
#DEFINE DLL_EXPORTS __declspec (같이 DllImport)    
#endif 다음   
    
    DLL_EXPORTS INT 합계 (INT의 값 1을, 값 2의 INT) 

__cplusplus #ifdef와 
} 
#endif 다음 


#endif 다음 // _ENCRYPTBASE_H!

  

2、添加后如图所示:

 

五、在FrameworkConsoleTest项目的Program修改为以下代码。

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace FrameworkConsoleTest
{
    class Program
    {
        [DllImport("EncryptBase.dll", CallingConvention = CallingConvention.Cdecl)]
        static extern int Sum(int value1, int value2);

        static void Main(string[] args)
        {
            int sumValue = Sum(1, 2);
            Console.WriteLine("sumValue:"+sumValue);
        }
    }
}

修改后如图所示:

 

六、在EncryptBase项目,右键--属性--生成事件--生成后事件--命令行--编辑。设置生成后事件之后,编辑EncryptBase项目就不用每次将bin的dll复制到FrameworkConsoleTest项目了。

 

 

2、输入以下内容:

copy "$(OutputPath)$(TargetFileName)" "E:\Project\ArticleProject\AuthorizationTest\FrameworkConsoleTest\bin\Debug"
copy "$(OutputPath)EncryptBase.pdb" "E:\Project\ArticleProject\AuthorizationTest\FrameworkConsoleTest\bin\Debug"

 

3、设置后如下所示:

 

4、生成EncryptBase项目。

 

六、在FrameworkConsoleTest,右键属性--生成--取消勾选"首选32位"。

 

七、运行。

在FrameworkConsoleTest项目,组合按Ctrl+F5,可显示调用结果。如下图所示。

 

八、文件已上传,可点击下载。

https://files.cnblogs.com/files/suterfo/AuthorizationTest(Demo1).rar

 

추천

출처www.cnblogs.com/suterfo/p/12291129.html