Last active
November 10, 2022 09:13
-
-
Save rkttu/943f43ceec9b43d3f3242806c3790b52 to your computer and use it in GitHub Desktop.
C++/CLI and C# Interop Sample - For Complete Source Code, Go to https://github.com/level120/CsharpCppSample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "pch.h" | |
#include "CTest.h" | |
using System::Runtime::InteropServices::Marshal; | |
CTest::CTest() | |
{ | |
this->pVaListener = new CVaListenerUnmanaged(); | |
this->pVaListenerManaged = gcnew CVaListenerManaged(); | |
} | |
CTest::~CTest() | |
{ | |
if (this->pVaListener) | |
{ | |
delete this->pVaListener; | |
this->pVaListener = NULL; | |
} | |
this->pVaListenerManaged = nullptr; | |
} | |
void CTest::Init(DELE_ERROR^ deleError, DELE_SUCCESS^ deleSuccess) | |
{ | |
this->pVaListener->onError = (DELE_ERROR_UNMANAGED)Marshal::GetFunctionPointerForDelegate(deleError).ToPointer(); | |
this->pVaListener->onSuccess = (DELE_SUCCESS_UNMANAGED)Marshal::GetFunctionPointerForDelegate(deleSuccess).ToPointer(); | |
this->pVaListenerManaged->onError = deleError; | |
this->pVaListenerManaged->onSuccess = deleSuccess; | |
} | |
void CTest::Delete() | |
{ | |
this->pVaListener->onError(10, _T("Hello!")); | |
this->pVaListener->onSuccess(0, _T("Succeed!"), 100); | |
this->pVaListenerManaged->onError(20, _T("Hello! 2")); | |
this->pVaListenerManaged->onSuccess(10, _T("Succeed! 2"), 200); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "CVaListenerManaged.h" | |
#include "CVaListenerUnmanaged.h" | |
#include "Delegates.h" | |
public ref class CTest | |
{ | |
public: | |
CTest(void); | |
~CTest(void); | |
private: | |
CVaListenerManaged^ pVaListenerManaged; | |
CVaListenerUnmanaged* pVaListener; | |
public: | |
void Init(DELE_ERROR^ deleError, DELE_SUCCESS^ deleSuccess); | |
void Delete(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "pch.h" | |
#include "CVaListenerManaged.h" | |
CVaListenerManaged::CVaListenerManaged() | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "Delegates.h" | |
public ref class CVaListenerManaged | |
{ | |
public: | |
CVaListenerManaged(); | |
public: | |
DELE_ERROR^ onError; | |
DELE_SUCCESS^ onSuccess; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "pch.h" | |
#include "CVaListenerUnmanaged.h" | |
CVaListenerUnmanaged::CVaListenerUnmanaged() | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "Delegates.h" | |
public class CVaListenerUnmanaged | |
{ | |
public: | |
CVaListenerUnmanaged(); | |
public: | |
DELE_ERROR_UNMANAGED onError; | |
DELE_SUCCESS_UNMANAGED onSuccess; | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <cstdint> | |
#include <tchar.h> | |
using System::Int32; | |
using System::String; | |
using System::Runtime::InteropServices::MarshalAsAttribute; | |
using System::Runtime::InteropServices::UnmanagedFunctionPointerAttribute; | |
using System::Runtime::InteropServices::CharSet; | |
using System::Runtime::InteropServices::CallingConvention; | |
using System::Runtime::InteropServices::UnmanagedType; | |
[UnmanagedFunctionPointerAttribute(CallingConvention::StdCall, CharSet = CharSet::Unicode)] | |
public delegate void DELE_ERROR(int32_t errorCode, [MarshalAs(UnmanagedType::LPTStr)] String^ errorText); | |
[UnmanagedFunctionPointerAttribute(CallingConvention::StdCall, CharSet = CharSet::Unicode)] | |
public delegate void DELE_SUCCESS(int32_t id, [MarshalAs(UnmanagedType::LPTStr)] String^ keyword, int32_t length); | |
typedef void (__stdcall *DELE_ERROR_UNMANAGED)(int32_t errorCode, const _TCHAR* errorText); | |
typedef void (__stdcall *DELE_SUCCESS_UNMANAGED)(int32_t errorCode, const _TCHAR* keyword, int32_t length); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// dllmain.cpp : Defines the entry point for the DLL application. | |
#include "pch.h" | |
/* | |
BOOL APIENTRY DllMain( HMODULE hModule, | |
DWORD ul_reason_for_call, | |
LPVOID lpReserved | |
) | |
{ | |
switch (ul_reason_for_call) | |
{ | |
case DLL_PROCESS_ATTACH: | |
case DLL_THREAD_ATTACH: | |
case DLL_THREAD_DETACH: | |
case DLL_PROCESS_DETACH: | |
break; | |
} | |
return TRUE; | |
} | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers | |
// Windows Header Files | |
#include <windows.h> | |
#include <tchar.h> | |
#include <cstdint> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pch.cpp: source file corresponding to the pre-compiled header | |
#include "pch.h" | |
// When you are using pre-compiled headers, this source file is necessary for compilation to succeed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pch.h: This is a precompiled header file. | |
// Files listed below are compiled only once, improving build performance for future builds. | |
// This also affects IntelliSense performance, including code completion and many code browsing features. | |
// However, files listed here are ALL re-compiled if any one of them is updated between builds. | |
// Do not add files here that you will be updating frequently as this negates the performance advantage. | |
#ifndef PCH_H | |
#define PCH_H | |
// add headers that you want to pre-compile here | |
#include "framework.h" | |
#endif //PCH_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace CsharpManaged | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
CTest test = new CTest(); | |
test.Init(OnError, OnSuccess); | |
test.Delete(); | |
Console.ReadLine(); | |
} | |
public static void OnError(Int32 errCode, String errText) | |
{ | |
Console.WriteLine($"!!!!!!!!!!!!!!!!!!! OnError {errText}"); | |
} | |
public static void OnSuccess(Int32 id, String keyword, Int32 length) | |
{ | |
Console.WriteLine($"############## OnSuccess {keyword}, {length}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment