|
먼저, C++ DLL 프로젝트를 생성하세요
CPPTest.cpp 수정하여 읽으세요: CPPTest.cpp : DLL 애플리케이션의 진입 지점을 정의합니다.
// #include "stdafx.h" #include #ifdef _MANAGED #pragma 관리(밀고, 끄짐) #endif BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserveserved
)
{ TRUE 반환;
} 외부 "C"
{ struct TestStruct
{ 지능 I; 캐릭터* 스트로; }; _declspec(dllexport) size_t __stdcall GetStringLength(char* & str)
{ Strlen(Str) 반환;
} _declspec(dllexport) int __stdcall AddTwoNumber(const int a, const int b)
{ a+b 반환;
} _declspec(dllexport) void __stdcall StructTest(TestStruct&s)
{ 네임스페이스 STD를 사용; Cout<<s.i<<endl; Cout<<S.Str<<endl; s.i=54321; s.str="모두 죽었어!";
}
} #ifdef _MANAGED #pragma 관리(팝) #endif
2. C# 테스트 프로젝트 생성
프로그램 내용을 다음과 같이 수정하세요: System을 사용; System.Collections.Generic을 사용; System.Text 사용; System.Runtime.InteropServices를 사용; namespace ConsoleApplicationTestCSharp
{ 수업 프로그램
{ [DllImport("CPPTest.dll")] static extern int GetStringLength(ref string str); [DllImport("CPPTest.dll")] static extern int AddTwoNumber(int a, int b); [DllImport("CPPTest.dll")] static extern void StructTest(참조 TestStruct s); static void Main(string[] args)
{ string testString = "하하! 나는 시험 줄이다"; Console.WriteLine("length(C++)=" + GetStringLength(ref testString). ToString()); Console.WriteLine("length(C#)=" + testString.Length.ToString()); Console.WriteLine("a+b(C++)=" + AddTwoNumber(10, 1000)); Console.WriteLine("a+b(C#)=" + (10 + 1000)); TestStruct s = new TestStruct(); s.i = 12345; s.str = "무텐 위의 악을 쏘러 가라!"; StructTest(참고문헌); 콘솔.WriteLine(s.i); 콘솔.WriteLine(s.str); Console.ReadKey();
} [구조 레이아웃(레이아웃 종류.순차적)] 공개 구조체 TestStruct
{ 공공 정보 I; 공용 문자열 스트리트;
}
}
} 위의 DllImportAttribute 태그를 참고하세요메서드。 득점 결과: length(C++)=24 길이(C#)=24 a+b(C++)=10¹⁵ a+b(C#)=10¹⁵
12345 무탱 위의 악령을 쏘러 가!
54321 모두 죽었어! 셋째, C/C++에서 여러 키워드에 대한 설명 _declspec __stdcall 외부
"C" (dllexport) DLL에서 __declspec (dllexport)를 사용하여 내보내는 것입니다. </s.str<<endl; </s.i<<endl;
|