我能够按照本教程http://tutplusplus.blogspot.com/2011/04/c-tutorial-create-dll-in-vc-20102008.html 中指示的说明编译基于 dll 库。我编译了它,没有问题。我还在一个空的 win32 控制台应用程序解决方案上测试了我的 dll 库,通过添加头文件、源文件和更改链接器设置,它编译得很好。问题是这次我无法在通用 Windows 应用程序解决方案上使用 dll 库。我添加了头文件和源文件并更改了链接器设置。
当我编译项目解决方案时,它要求我包含pch.h
到我的库的源文件中,所以我#include "pch.h"
在顶部添加了这一行。
之后,出现了其他编译器错误。
'CreateFile': identifier not found
和 'SetCommState': identifier not found
我相信我当前项目解决方案的编译器重新编译了所有内容,而没有考虑 dll 库的编译。如何让项目解决方案只编译自己的源文件和头文件?
库标题:
#pragma once
#include <Windows.h>
namespace nmspace
{
class SerialPort
{
public:
static __declspec(dllexport) int connect();
};
}
库来源:
#include <iostream>
using namespace std;
#include "SimpleH.h"
#include <Windows.h>
namespace nmspace
{
int SerialPort::connect()
{
int error = 0;
DCB dcb;
memset(&dcb, 0, sizeof(dcb));
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = 57600;
dcb.Parity = NOPARITY;
dcb.fParity = 0;
dcb.StopBits = ONESTOPBIT;
dcb.ByteSize = 8;
HANDLE m_serialPortHandle = CreateFile(L"COM7", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, NULL, NULL);
if (m_serialPortHandle != INVALID_HANDLE_VALUE) {
if (!SetCommState(m_serialPortHandle, &dcb))
error = 2;
}
else
error = 1;
return error;
}
}
您将无法使用此 DLL,因为它使用SetCommState
仅适用于桌面应用程序的功能。您应该在可用于 UWP 应用程序的桌面 API 列表中查找您尝试使用的函数。
至于CreateFile
它不是一个函数,而是一个扩展到CreateFileA
或CreateFileW
取决于项目 Unicode 设置的宏。您应该CreateFile2
改用它可用于桌面和 UWP 应用程序。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句