「ライブラリの基礎 - C++DLL」の版間の差分

ナビゲーションに移動 検索に移動
858行目: 858行目:
  // SampleDLL.h
  // SampleDLL.h
   
   
  void  __stdcall SampleFunc01(SampleStruct1 Structure)
  void  __stdcall DisplayFunc(SampleStruct1 Structure)
  void  __stdcall SampleFunc02(SampleStruct2 *pStructure)
  void  __stdcall GetFunc(SampleStruct1 Structure)
void  __stdcall SetFunc(SampleStruct2 *pStructure)
  </syntaxhighlight>
  </syntaxhighlight>
<br>
<br>
882行目: 883行目:
  } SampleStruct2, *pSampleStruct2;
  } SampleStruct2, *pSampleStruct2;
   
   
  void __stdcall SampleFunc01(SampleStruct1 st)
  void __stdcall DisplayFunc(SampleStruct1 st)
  {
  {
     printf("--<SampleDll:SampleFunc01>--\r\n");
     printf("--<SampleDll:DisplayFunc>--\r\n");
     printf("index = %d\r\n", st.index);
     printf("index = %d\r\n", st.index);
     printf("name = %s\r\n", st.name);
     printf("name = %s\r\n", st.name);
891行目: 892行目:
  }
  }
   
   
  void __stdcall SampleFunc02(SampleStruct2 *pStructure)
  void __stdcall GetFunc(SampleStruct1 *pst)
  {
  {
     printf("--<SampleDll:SampleFunc02>--\r\n");
     printf("--<SampleDll:GetFunc>--\r\n");
    pst->index = 10;
    memcpy_s(pst->name, sizeof(pst->name), "Tarou Yamada", sizeof("Tarou Yamada"));
    memset(pst->data, 20, sizeof(pst->data));
    printf("------------------------\r\n");
}
void __stdcall SetFunc(SampleStruct2 *pStructure)
{
    printf("--<SampleDll:SetFunc>--\r\n");
   
   
     memset(pStructure, 0, sizeof(SampleStruct2));
     memset(pStructure, 0, sizeof(SampleStruct2));
918行目: 928行目:
  EXPORTS
  EXPORTS
           ; 公開する関数名をリストアップ
           ; 公開する関数名をリストアップ
           SampleFunc01   @1
           DisplayFunc   @1
           SampleFunc02  @2
           GetFunc      @2
          SetFunc      @3
  </syntaxhighlight>
  </syntaxhighlight>
<br>
<br>
942行目: 953行目:
       /// </summary>
       /// </summary>
       /// <param name="st">DLL 側に渡す構造体を指定します</param>
       /// <param name="st">DLL 側に渡す構造体を指定します</param>
       [DllImport("SampleDLL.dll", EntryPoint = "SampleFunc01", CallingConvention = CallingConvention.Cdecl)]
       [DllImport("SampleDLL.dll", EntryPoint = "DisplayFunc", CallingConvention = CallingConvention.Cdecl)]
       private static extern void _SampleFunc01(SampleStruct1 st);
       private static extern void _DisplayFunc(SampleStruct1 st);
   
   
       /// <summary>
       /// <summary>
949行目: 960行目:
       /// </summary>
       /// </summary>
       /// <param name="pst">受け渡す構造体の先頭アドレスを示すポインタを指定する</param>
       /// <param name="pst">受け渡す構造体の先頭アドレスを示すポインタを指定する</param>
       [DllImport("SampleDLL.dll", EntryPoint = "SampleFunc02", CallingConvention = CallingConvention.Cdecl)]
       [DllImport("SampleDLL.dll", EntryPoint = "GetFunc", CallingConvention = CallingConvention.Cdecl)]
       private static extern void _SampleFunc02(IntPtr pst);
       private static extern void _GetFunc(IntPtr pst);
      /// <summary>
      /// DLL側からメンバにポインタを含む構造体を受け取る関数のインポート例
      /// </summary>
      /// <param name="pst">受け渡す構造体の先頭アドレスを示すポインタを指定する</param>
      [DllImport("SampleDLL.dll", EntryPoint = "SetFunc", CallingConvention = CallingConvention.Cdecl)]
      private static extern void _SetFunc(IntPtr pst);
   
   
       /// <summary>
       /// <summary>
993行目: 1,011行目:
           SampleFunc01();
           SampleFunc01();
           SampleFunc02();
           SampleFunc02();
          SampleFunc03();
   
   
           Console.ReadKey();
           Console.ReadKey();
1,014行目: 1,033行目:
   
   
       private static void SampleFunc02()
       private static void SampleFunc02()
      {
          var structure = new SampleStruct1()
          {
            index = 0,
            name = "",
            data = new int[50],
          };
          // COMタスクメモリアロケータから、C#の構造体のサイズ分のメモリブロックを割り当てる
          IntPtr structurePtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(structure));
          _GetFunc(structurePtr);
          // Marshal.PtrToStructureメソッドを使用して、IntPtr変数が示すメモリに格納された情報をC#の構造体にコピーする
          structure = (SampleStruct1)Marshal.PtrToStructure(structurePtr, structure.GetType());
          Marshal.FreeCoTaskMem(structurePtr);
          Console.WriteLine($"index : {structure.index}");
          Console.WriteLine($"name : {structure.name}");
          Console.WriteLine($"data : {structure.data}");
      }
      private static void SampleFunc03()
       {
       {
           // SampleStruct2構造体のサイズを取得する
           // SampleStruct2構造体のサイズを取得する
1,021行目: 1,064行目:
           {
           {
             // C++ DLLのSampleFunc02関数の実行
             // C++ DLLのSampleFunc02関数の実行
             SampleFunc02(structure1);
             SetFunc(structure1);
   
   
             // 受け取ったstructure1からSampleStruct2構造体の情報に構築し直す
             // 受け取ったstructure1からSampleStruct2構造体の情報に構築し直す

案内メニュー