main.cpp(11):错误C2059:语法错误:'string'

问题描述 投票:-1回答:1

环境: 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.

  }
}
c++ visual-studio dllimport
1个回答
0
投票

嗯,问题出在这一行:

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

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

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

此外,正如上面的评论所指出的,您拥有';'在定义DllImport之后。最后,要在Windows上以C ++加载DLL并使用该DLL中的特定功能,应使用Win32 API函数LoadLibrary加载DLL,并使用GetProcAddress获取指向您要调用的函数的指针。在线上有很多示例,例如this SO post

© www.soinside.com 2019 - 2024. All rights reserved.