main.cpp(11):错误 C2059:语法错误:"字符串"

main.cpp(11) : error C2059: syntax error : 'string'

本文关键字:错误 C2059 语法 字符串 main cpp      更新时间:2023-10-16

环境:Visual Studio 2008,C++ 我正在将条形码打印机[TSC210]与控制器[NXP A4]串行连接,查看了其他帖子,但不确定导致问题的原因,我是C++的初学者。任何人都可以建议如何解决此错误吗?

收到错误和警告情况,例如:

1] main.cpp(11) : error C2059: syntax error : 'string'
2] main.cpp(11) : warning C4091: '__declspec(dllimport)' : ignored on left of 'int' when no variable is declared

我有以下代码

#include <windows.h>
#define BUFFER_SIZE   32
#define Naked __declspec(naked)
#define DllImport   __declspec( dllimport )
namespace TSCLIB
{
DllImport("TSCLIB.dll", EntryPoint = "about")
int about();
}
BOOL PortOpen(HANDLE *port, DWORD baudRate)
{
DCB portDCB;                                              ///< COM port configuration structure.
BOOL returnValue = FALSE;
COMMTIMEOUTS comTimeOut;
/// Opens interface to reader.  
/// COM Port Configuration.
/// Changes the DCB structure settings.
/// Configures the port according to the specifications of the DCB structure.
/// Gets communication time out values.
/// Sets communication time out values.
return TRUE;
}
BOOL PortClose(HANDLE *port)
{
if (*port == NULL)
{
return FALSE;
}
CloseHandle(*port);
*port = NULL;
return TRUE;
}

int wmain(void)
{
HANDLE portHandle;
while (TRUE)
{
//  case WRITE:                 
}
// Closes the serial port.
}
}

好吧,问题出在这一行:

DllImport("TSCLIB.dll", EntryPoint = "about")]

这里实际上有两个问题。第一个是语句以"]"结尾。我猜这可能只是一个错字,你正确地使用了";"。然而,问题也是EntryPoint = "about"部分。您不能在C++中调用这样的方法。我猜你想这样做:

DllImport("TSCLIB.dll", "about");

此外,如上面的注释中所述,在 DllImport 的定义之后有";"。最后,若要在 Windows 上加载 C++ 中的 DLL 并从该 DLL 使用特定函数,应使用 Win32 API 函数 LoadLibrary 加载 DLL 和 GetProcAddress 以获取指向要调用的函数的指针。网上有很多例子,例如,这个SO帖子