2016-10-28 6 views
0

私のCコードでdllファイルを使用したいが、構文に関して非常に混乱している。Cプログラムで動的ライブラリdllを使用

マイ・ストーリー:Matlab(f(x1、x2)= x1 * x2)でシンプルな関数を作成し、 "Matlab Coder"を使ってCコードに変換してexeを生成しました。 arguments.Nowとのターミナルは、exeの代わりにDLLを生成し、DLLを使用したい。

私はコードの説明をすることができなかったので、私はグーグルで私のために仕事をしました。私はhttp://en.cppreference.com/w/の構文を見上げますが、驚いたことに、例えばGetProcAddressまたはLoadLirbary。ここで

は、私は、DLLを使用したいしたC-コード:dllファイルを生成した後

#include <stdio.h> 
#include <stdlib.h> 

/* 
* In my dream I would load the dll function here 
* with something like Load(mytimes4.dll) 
*/ 

int main(int argc, char *argv[]) { 

double x1,x2,myresult; 
//Load Arguments from Terminal 
sscanf(argv[1], "%lf", &x1); 
sscanf(argv[2], "%lf", &x2); 

// Use and print the function from mytimes4.dll 
myresult = mytimes4(x1,x2); 
printf("%3.2f\n",myresult); 

return 0; 
} 

を、Matlabのは私に次のフォルダました: "dll-folder" produced by Matlab

を誰かが私に与えることができます私の例で動作するシンプルで完全なコードです。どのファイルが必要ですか(おそらく.defか.exp)?また、dllを使用している行の説明については、私は感謝しています。そうでない場合は、複雑な構文を合理的にする背景知識があるかもしれません。事前に感謝しています!

システム情報:Windows 7 Pro 64、Matlab 642016b、gcc cygwin 64、Eclipse IDE。

+0

私はcppreferenceがOpenLibraryとのGetProcAddressのエントリを持っていなかったことに驚いていないよ - これらの関数は、Windows API(C++標準の一部ではない)の一部です。 OpenLibraryの説明については、https://msdn.microsoft.com/en-us/library/windows/desktop/ms684175(v=vs.85).aspxを参照してください。たとえば、https://msdn.microsoft.comを参照してください。 OpenLibraryとGetProcAddressの使用例については/en-us/library/windows/desktop/ms686944(v=vs.85).aspxを参照してください。 – thurizas

+0

私はその記事[[Pelles CのDLLへのリンク](http://stackoverflow.com/questions/40282524/link-to-a-dll-in-pelles-c)に提案したソリューションを確認できます。 –

+0

'mytimes4.h'と' mytimes4.def'の内容は何ですか? –

答えて

0

thurizasのリンクで私の問題を解決することができました。 https://msdn.microsoft.com/en-us/library/windows/desktop/ms686944(v=vs.85).aspx サイドからコードをコピーしました。下に、私の意見と、より明確な名前を付けて、私のコメントが追加されたコードを見ることができます。したがって、おそらく初心者の方が私のように使いやすくなります。

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

/*Declaration of the function,contained in dll, as pointer with the arbitrary pointer name 
"*MYFUNCTIONPOINTER" (not sure if it has to be in big letters). 
In my case the function means simply f(x1,x2) = x1*x2 and is thus as double declared*/ 
typedef double (*MYFUNCTIONPOINTER)(double, double); 

int main() { 

    HINSTANCE hinstLib; 
    //"myfunction" is the arbitrary name the function will be called later 
    MYFUNCTIONPOINTER myfunction; 
    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 

    //Tell the dll file 
    hinstLib = LoadLibrary(TEXT("mypersonal.dll")); 

    if (hinstLib != NULL) 
     { 
     /* At this line "myfunction" gets its definition from "MYFUNCTIONPOINTER" 
     and can be used as any other function.The relevant function in the dll has 
     to be told here.*/ 
     myfunction = (MYFUNCTIONPOINTER) GetProcAddress(hinstLib, "mydllfunction"); 

      // If the function address is valid, call the function. 
      if (NULL != myfunction) 
      { 
       fRunTimeLinkSuccess = TRUE; 

      // The function can be used. 
      double myoutput; 
      myoutput = myfunction(5,7); 
      printf("%f\n",myoutput); 
      getchar(); 
      } 
      // Free the DLL module. 

      fFreeResult = FreeLibrary(hinstLib); 
     } 

     // If unable to call the DLL function, use an alternative. 
     if (! fRunTimeLinkSuccess) 
      printf("Message printed from executable\n"); 

    return 0; 
} 
関連する問題