-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[recovery mode]

, 14 2017 . 16:14 +
image , [DllImport] , MSDN. . , . , , . , .


:
  1. MixedLibrary C++/CLI
  2. SimpleLibrary C#
  3. Win32App C++

image

SimpleLibrary. , :

public class Service
    {
        public void Add(Int32 a, Int32 b)
        {
            Console.WriteLine("Hello from Managed Code!");
            Console.WriteLine(String.Format("Result: {0}", a + b));
        }
    }


MixedLibrary. - SimpleService. CppService.h:

//   ,    
//     
#ifdef INSIDE_MANAGED_CODE
#    define DECLSPECIFIER __declspec(dllexport)
#    define EXPIMP_TEMPLATE
#else
#    define DECLSPECIFIER __declspec(dllimport)
#    define EXPIMP_TEMPLATE extern
#endif


namespace MixedLibrary
{

	class DECLSPECIFIER CppService
	{
	public:
		CppService();
		virtual ~CppService();

	public:
		void Add(int a, int b);

	private:
		void * m_impl;
	};
}

CppService.cpp:

#include "CppService.h"

using namespace System;
using namespace System::Runtime::InteropServices;
using namespace SimpleLibrary;

namespace MixedLibrary
{
	CppService::CppService()
	{
		Service^ service = gcnew Service();
		m_impl = GCHandle::ToIntPtr(GCHandle::Alloc(service)).ToPointer();
	}

	CppService::~CppService()
	{
		GCHandle handle = GCHandle::FromIntPtr(IntPtr(m_impl));
		handle.Free();
	}

	void CppService::Add(int a, int b)
	{
		GCHandle handle = GCHandle::FromIntPtr(IntPtr(m_impl));
		Service^ service = safe_cast(handle.Target);
		service->Add(a, b);
	}
}


INSIDE_MANAGED_CODE:

image

:

#include "stdafx.h"

#pragma comment(lib, "../Debug/MixedLibrary.lib")

#include 
#include "../MixedLibrary/CppService.h"


using namespace std;
using namespace MixedLibrary;


int main()
{
	CppService* service = new CppService();
	service->Add(5, 6);

	cout << "press any key..." << endl;
	getchar();
}

, , :

image

: nikitam
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/335620/

:  

: [1] []
 

:
: 

: ( )

:

  URL