2

このコマンドを使用して、Matlab で単純な関数の dll を作成しました。

mcc -t -L C -W lib:testfunctionLib -T link:lib testfunction.m libmmfile.mlib

単純な関数は次のようになります。

function y = testfunction(x) 
y = x + 10;
end

Cコードを介してdllを呼び出す必要があります。これは、dll関数を使用した計算の結果をテキストファイルに取得するために使用しているものです。

#include <windows.h>
#include <stdio.h>

int main()
{
    int z = 1;
    FILE *Testfile;

    typedef int(*BinaryFunction_t) (int);
    BinaryFunction_t  AddNumbers;
    int            result;
    BOOL              fFreeResult;
    HINSTANCE         hinstLib = LoadLibraryA("testfunctionLib.dll");

if (hinstLib != NULL)
    {
    AddNumbers = (BinaryFunction_t)GetProcAddress(hinstLib, "testfunction");

    if (AddNumbers != NULL)
        result = (*AddNumbers) (z);

    fFreeResult = FreeLibrary(hinstLib);

    Testfile = fopen("Testfile.txt", "a");
    fprintf(Testfile, "%i\n", result);
    fclose(Testfile);
    }
else
    {
    Testfile = fopen("Testfile.txt", "a");
    fprintf(Testfile, "NOT");
    fclose(Testfile);
    }
}

Cコードはdllから関数を抽出できないため、テキストファイルに常に「NOT」が表示されます。なぜこれが機能しないのですか?dll 関数を取得するための C コードは問題ないはずです。Visual Studio 内で作成された dll でテストしました。

4

1 に答える 1