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

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

环境: 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 to load the DLL and GetProcAddress to get the pointer to a function you would like to call. There are many examples online, for instance, this SO post