LoginSignup
0
1

More than 5 years have passed since last update.

Azure Functions でアンマネージドコードをPInvokeを使って呼ぶ

Last updated at Posted at 2019-01-19

これはなに?

Azure Functions 上で PInvok 経由で呼び出されたアンマネージドコードが動作するかを確認しました。

環境
- Windows 10
- Visual Studio Community 2017

参考
- https://docs.microsoft.com/ja-jp/azure/azure-functions/functions-develop-vs

プロジェクト作成

Visual Studio の [ツール] メニューの [拡張機能と更新プログラム]から「Azure Functions」を検索して「Azure Functions と Web ジョブ ツール」をダウンロードします。自分は英語版を使っているので英語ですが ...
image.png

次に新しいプロジェクトを作成します。メニューの「ファイル」から新しいプロジェクト作成を選び、Azure Functions を選び、Azure Functions のプロジェクトを作ります。
image.png
トリガーはとりあえず http trigger を選択。
image.png

コード

mymath.dll には half という引数を半分にするだけの dll があります。これがアンマネージドコードで、マネージドコードの C# から Azure Functions 上で動作するかを確認します。

using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;

using System.Runtime.InteropServices;

namespace AzureFuncMyMath
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
        {
            int ret = half(10);
            return req.CreateResponse(HttpStatusCode.OK, "Ans: " + $"{ret}") ;
        }
        [DllImport("nymath.dll", CallingConvention = CallingConvention.StdCall)]
        static extern int half(int n);
    }
}

コンパイルしてとりえずローカルのシミュレータ上で動作確認します。

image.png

とりえず動きそうです。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1