在C++程序中实现DLL时遇到问题

Having Trouble Implementing DLL in a C++ Program

本文关键字:遇到 问题 DLL 实现 C++ 程序      更新时间:2023-10-16

我是C++新手,被要求从质谱设备中检索和操作数据。我得到了一些DLL库和它们的文档来做这件事。通过在赛默飞世尔公司注册账户,可以找到这些库及其文档:

https://thermo.flexnetoperations.com/control/thmo/RegisterMemberToAccount

软件包是MSFileReader 3.0。文件本身可以在这里查看:

http://www.thermoscientific.de/content/dam/tfs/ATG/CMD/cmd-documents/oper/oper/ms/lc-ms/soft/Man-XCALI-97542-MSFileReader-30-Ref-ManXCALI97542-A-EN.pdf

我有64位版本的软件。我正在使用MS Visual Studio Community 2015。我选择使用运行时术语。我的代码在下面。

#include <Windows.h>
#include <iostream>
#include <string>
int main()
{
    HINSTANCE hInst = LoadLibrary(L"C:\Nathan\C++\GetEvenOdd\Debug\XRawfile2_x64.dll");
    if (!hInst)
    {
        std::cout << "ncould Not Load the Library" << std::endl;
        return EXIT_FAILURE; 
    }
    else
    {
        std::cout << "nSuccess" << std::endl;
    }
    //Resolve the function address 
    FreeLibrary(hInst);
    return EXIT_SUCCESS;
}

让我困惑的是,当LoadLibrary()中的路径被设置为我创建的另一个DLL时,程序表示加载成功;当我尝试加载XRawfile2_x64.dll时,程序返回"无法加载库"。编译时,VS返回以下内容。

1>------ Build started: Project: Loader, Configuration: Debug Win32 ------
1>  Source.cpp
1>  Loader.vcxproj -> C:NathanC++LoaderDebugLoader.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

我创建的DLL如下。标题-

#ifndef EVENODD_H_
#define EVENODD_H_
#include <string>
class EvenOdd
{
public:
    std::string CheckEvenOdd(const int iNum);
};
#endif //EVENODD_H_

源文件-

#include "EvenOdd.h"
extern "C"
{
    __declspec(dllexport) std::string CheckEvenOdd(const int iNum)
    {
        std::string strRes = "NULL";
        if (iNum % 2 == 0)
        {
            strRes = "The given number is Even";
        }
        else {
            strRes = "The given number is Odd";
        }
        return strRes;
    }
}

有了这些信息,我如何加载MSFileReader包中包含的DLL文件?

Visual Studio中的默认设置是构建一个32位应用程序。您无法从32位应用程序加载64位DLL。将生成配置更改为目标"x64"平台。以下是您的操作方法:

  1. 打开项目属性
  2. 单击"Configuration Manager"
  3. 在"平台"列中,添加一个新条目"x64"(从Win32平台配置复制设置)