1. <i id='gMnjz'><tr id='gMnjz'><dt id='gMnjz'><q id='gMnjz'><span id='gMnjz'><b id='gMnjz'><form id='gMnjz'><ins id='gMnjz'></ins><ul id='gMnjz'></ul><sub id='gMnjz'></sub></form><legend id='gMnjz'></legend><bdo id='gMnjz'><pre id='gMnjz'><center id='gMnjz'></center></pre></bdo></b><th id='gMnjz'></th></span></q></dt></tr></i><div id='gMnjz'><tfoot id='gMnjz'></tfoot><dl id='gMnjz'><fieldset id='gMnjz'></fieldset></dl></div>
        <tfoot id='gMnjz'></tfoot>

        <legend id='gMnjz'><style id='gMnjz'><dir id='gMnjz'><q id='gMnjz'></q></dir></style></legend>
        • <bdo id='gMnjz'></bdo><ul id='gMnjz'></ul>

        <small id='gMnjz'></small><noframes id='gMnjz'>

        有没有更好的方法在 C++ 中加载 dll?

        时间:2023-08-28
      2. <i id='sf6GN'><tr id='sf6GN'><dt id='sf6GN'><q id='sf6GN'><span id='sf6GN'><b id='sf6GN'><form id='sf6GN'><ins id='sf6GN'></ins><ul id='sf6GN'></ul><sub id='sf6GN'></sub></form><legend id='sf6GN'></legend><bdo id='sf6GN'><pre id='sf6GN'><center id='sf6GN'></center></pre></bdo></b><th id='sf6GN'></th></span></q></dt></tr></i><div id='sf6GN'><tfoot id='sf6GN'></tfoot><dl id='sf6GN'><fieldset id='sf6GN'></fieldset></dl></div>
          <bdo id='sf6GN'></bdo><ul id='sf6GN'></ul>

                <tbody id='sf6GN'></tbody>
              <tfoot id='sf6GN'></tfoot>
                  <legend id='sf6GN'><style id='sf6GN'><dir id='sf6GN'><q id='sf6GN'></q></dir></style></legend>

                  <small id='sf6GN'></small><noframes id='sf6GN'>

                • 本文介绍了有没有更好的方法在 C++ 中加载 dll?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  现在我正在做这样的事情,如果我在我的 DLL 中拥有很多想要引用的函数,这看起来很混乱.是否有一种更好、更简洁的方法来访问函数,而无需为每个函数定义创建 typedef,以便它能够正确编译和加载函数.我的意思是函数定义已经在 .h 文件中,我不应该在加载函数后重新声明它们(或者我是吗?)有没有比使用 LoadLibary 更好的解决方案?如果有一种方法可以在 Visual Studio 2005 项目设置中做同样的事情,我不一定需要该功能.

                  Right now I do something like this and it seems messy if I end having a lot of functions I want to reference in my DLL. Is there a better and cleaner way of accessing the functions without having to create a typedef for each function definition so that it will compile and load the function properly. I mean the function definitions are already in the .h file and I shouldn't have to redeclare them after I load the function (or do I?) Is there a better solution than using LoadLibary? I don't necessarily need that function if there is a way I can do the same thing within Visual Studio 2005 project settings.

                  #include "stdafx.h"
                  #include <windows.h>
                  
                  #ifndef BHANNAN_TEST_CLASS_H_
                  #define BHANNAN_TEST_CLASS_H_
                  
                  extern "C" {
                  
                      // Returns n! (the factorial of n).  For negative n, n! is defined to be 1.
                      int __declspec (dllexport) Factorial(int n);
                  
                      // Returns true iff n is a prime number.
                      bool __declspec (dllexport) IsPrime(int n);
                  
                  }
                  
                  #endif  // BHANNAN_TEST_CLASS_H_
                  

                  <小时>

                  BHannan_Test_Class.cpp

                  #include "stdafx.h"
                  #include "BHannan_Test_Class.h"
                  
                  // Returns n! (the factorial of n).  For negative n, n! is defined to be 1.
                  int Factorial(int n) {
                    int result = 1;
                    for (int i = 1; i <= n; i++) {
                      result *= i;
                    }
                  
                    return result;
                  }
                  
                  // Returns true iff n is a prime number.
                  bool IsPrime(int n) {
                    // Trivial case 1: small numbers
                    if (n <= 1) return false;
                  
                    // Trivial case 2: even numbers
                    if (n % 2 == 0) return n == 2;
                  
                    // Now, we have that n is odd and n >= 3.
                  
                    // Try to divide n by every odd number i, starting from 3
                    for (int i = 3; ; i += 2) {
                      // We only have to try i up to the squre root of n
                      if (i > n/i) break;
                  
                      // Now, we have i <= n/i < n.
                      // If n is divisible by i, n is not prime.
                      if (n % i == 0) return false;
                    }
                  
                    // n has no integer factor in the range (1, n), and thus is prime.
                    return true;
                  }
                  

                  <小时>

                  dll_test.cpp

                  #include <BHannan_Test_Class.h>
                  
                  typedef int (*FactorialPtr) (int);
                  FactorialPtr myFactorial=NULL;
                  
                  // Tests factorial of negative numbers.
                  TEST(FactorialTest, Negative) {
                  
                      HMODULE myDLL = LoadLibrary("BHannan_Sample_DLL.dll");
                  
                      if(myDLL) {
                          myFactorial = (FactorialPtr) GetProcAddress(myDLL,"Factorial");
                  
                          if(myFactorial)
                          {
                              EXPECT_EQ(1, myFactorial(-5));
                              EXPECT_EQ(1, myFactorial(-1));
                              EXPECT_TRUE(myFactorial(-10) > 0);
                          }
                  
                          FreeLibrary(myDLL);
                      }
                  
                  }
                  

                  推荐答案

                  构建 .dll 后,获取附近的 .lib 文件并将您的测试应用程序与其链接.使用在 .h 中声明的函数

                  After building your .dll get the .lib file nearby and link your test application with it. Use functions as they are declared in .h

                  您需要在头文件中做一个小改动:

                  There's a minor change you need to do in your header file:

                  #ifdef EXPORTS_API
                    #define MY_API_EXPORT __declspec (dllexport)
                  #else
                    #define MY_API_EXPORT __declspec (dllimport)
                  #endif
                  
                  extern "C" {
                      int MY_API_EXPORT Factorial(int n);
                  
                      // do the same for other functions
                  }
                  

                  这样,在构建 dll 时,您在项目设置中定义 EXPORTS_API 并导出函数,在客户端应用程序中,无需定义任何内容.

                  This way, when building your dll you define EXPORTS_API in your project settings and functions get exported, in the client application, no need to define anything.

                  这篇关于有没有更好的方法在 C++ 中加载 dll?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:线程对象的 WaitForSingleObject 在 DLL 卸载中不起作用 下一篇:在 VS 中编译 C++,而无需在运行时使用 MSVCP120D.dll

                  相关文章

                    <tfoot id='LDuT8'></tfoot>

                    <i id='LDuT8'><tr id='LDuT8'><dt id='LDuT8'><q id='LDuT8'><span id='LDuT8'><b id='LDuT8'><form id='LDuT8'><ins id='LDuT8'></ins><ul id='LDuT8'></ul><sub id='LDuT8'></sub></form><legend id='LDuT8'></legend><bdo id='LDuT8'><pre id='LDuT8'><center id='LDuT8'></center></pre></bdo></b><th id='LDuT8'></th></span></q></dt></tr></i><div id='LDuT8'><tfoot id='LDuT8'></tfoot><dl id='LDuT8'><fieldset id='LDuT8'></fieldset></dl></div>

                    <small id='LDuT8'></small><noframes id='LDuT8'>

                  1. <legend id='LDuT8'><style id='LDuT8'><dir id='LDuT8'><q id='LDuT8'></q></dir></style></legend>
                      • <bdo id='LDuT8'></bdo><ul id='LDuT8'></ul>