1

c で書かれた配列に何かを出力し、dll 経由で c# 呼び出しから情報を取得したいと思っていましたが、失敗しました。警告はありませんが、情報を正しく取得できます。次のようにコードをテストします。

@ips は出力情報を保存します

UDPDLL_API int get_by_csharp_tst(char *** ips){
    char **ip = NULL;
    int i = 0;
    *ips = (char**)malloc(sizeof(char*)*10);
    if(ips == NULL){
        perror("overflow");
    }
    ip = *ips;
    for(i =0 ; i <10 ; i++){
        *ip = (char*)malloc(16);
        memcpy(*ip,"255.255.255.255",16);
        *ip++;
    }
    return 0;
}

次のようにC#から呼び出す:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace dll_call
{
    class Program
    {
        [DllImport("udpdll.dll",EntryPoint="get_by_csharp_tst")]
        public static extern int get_by_csharp_tst(byte [,] ai);
        static void Main(string[] args)
        {
            int i = 0;
            byte[,] ips = new byte[10, 16];
            Program.get_by_csharp_tst(ips);
            for (i = 0; i < 10; i++) {
                Console.WriteLine(ips);
            }
            Console.Read();
        }
    }
}

ありがとうございました。どんな助けでも大歓迎です!

4

1 に答える 1