Byte型とChar型

概要

Byte型とChar型のデータのやりとり

Byte型とChar型

  • C++側のソース

    Dll1.dllとしてコンパイル
    #include <windows.h>
    #include <cstddef>
    #include <iostream>
    
    using namespace std;
    
    #define DLLEXPORT __declspec(dllexport) WINAPI
    
    // BYTE受け取り
    extern "C" void DLLEXPORT dllbyte(const BYTE a) {
    	cout << a << "," << (int)a << endl;
    }
    
    // Char受け取り
    extern "C" void DLLEXPORT dllchar(const char b) {
    	cout << b << endl;
    }
      
  • C#側のソース

    test1.cs
    using System;
    using System.Runtime.InteropServices;
    
    namespace test1
    {
        internal class Program
        {
            [DllImport("Dll1.dll")]
            static extern void dllbyte(byte a);
    
            [DllImport("Dll1.dll")]
            static extern void dllchar(char b);
    
    
            static void Main(string[] args)
            {
                byte a = 0x41;
    
                dllbyte(a);
    
                char b = 'b';
    
                dllchar(b);
    
                Console.ReadKey();
            }
        }
    }