Skip to content

Instantly share code, notes, and snippets.

@baobao
Last active February 23, 2025 15:47
Show Gist options
  • Save baobao/230a59882cdf85e6dd9fd2843aa2f425 to your computer and use it in GitHub Desktop.
Save baobao/230a59882cdf85e6dd9fd2843aa2f425 to your computer and use it in GitHub Desktop.
C#からC++に文字列を送信する記事 https://shibuya24.info/entry/unity-cs-cpp-string 内のサンプルソースコード
// 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;
}
// 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