dllのExport名と違う名前で使う

概要

dllのExport名と違う名前で使う

C#側でEntryPointでオリジナルのExtern名を指定することで、結果として別名で使うことが出来ます。

IntPtr型

  • C++側のソース

    Dll1.dllとしてコンパイル
    #include <windows.h>
    #include <cstddef>
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <string>
    
    using namespace std;
    
    #define DLLEXPORT __declspec(dllexport) WINAPI
    
    
    
    // intptr_tの受け渡し
    extern "C" int32_t DLLEXPORT dllOriginalName(int32_t v) {
    	return 300 + v;
    }
    
  • C#側のソース

    test1.cs
    using System;
    using System.Runtime.InteropServices;
    
    namespace test1
    {
        internal class Program
        {
            [DllImport("Dll1.dll", EntryPoint = "dllOriginalName")]
            static extern Int32 dllNewNameFunction(Int32 v);
    
            static void Main(string[] args)
            {
                var ret = dllNewNameFunction(100);
    
                Console.WriteLine(ret);
    
                Console.ReadKey();
            }
        }
    }