Last active
February 23, 2025 15:47
-
-
Save baobao/230a59882cdf85e6dd9fd2843aa2f425 to your computer and use it in GitHub Desktop.
C#からC++に文字列を送信する記事 https://shibuya24.info/entry/unity-cs-cpp-string 内のサンプルソースコード
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
// C#からC++に文字列を送信するC++側のコード | |
#define _CRT_SECURE_NO_WARNINGS | |
#include <iostream> | |
#include <windows.h> | |
extern "C" __declspec(dllexport) void ReceiveString(const char* str) | |
{ | |
// コンソールを起動 | |
FreeConsole(); | |
AllocConsole(); | |
freopen("CONOUT$", "w", stdout); | |
freopen("CONOUT$", "w", stderr); | |
if (str == NULL) { | |
std::cout << "str is null." << std::endl; | |
return; | |
} | |
std::cout << str << std::endl; | |
} |
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
// C#からC++に文字列を送信するC#側のコード | |
using System.Runtime.InteropServices; | |
using UnityEngine; | |
public class InvokeCpp : MonoBehaviour | |
{ | |
[DllImport("TestDll.dll", CallingConvention = CallingConvention.Cdecl)] | |
static extern void ReceiveString(string hoge); | |
void Awake() | |
{ | |
ReceiveString("Hello World C# to C++!!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment