-1

Environment: Visual Studio 2008, C++ I am interfacing Barcode printer [TSC210] serially with controller [NXP A4], looked at other posts but not sure what is causing the problem and I am a beginner in C++. Can anyone suggest how to fix this error?

Getting error and warning case like:

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

I have the following code

#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.

  }
}
shreyash
  • 31
  • 5
  • 1
    This looks like you typed it in by hand and made mistakes. You have a semicolon after a define. And you have a ] at the end of DllImport, but no matching [ anywhere. Is there even supposed to be one? – Zan Lynx Dec 17 '19 at 06:55
  • @ZanLynx Thank you for your response, Warning is removed after I edited the code but string error is remaining – shreyash Dec 17 '19 at 07:05
  • It looks like you're trying to emulate the C# `DllImport` in C++. That's not the same as `__declspec(dllimport)`. You need to study some more. – molbdnilo Dec 17 '19 at 07:05
  • I understand that this is your first question on SO, but please pay attention to a few things. First of all, format you code nicely, so it is easier for people to understand. Second, when someone points to the errors you have in the code, don't edit your code to remove all the errors. This makes the previous answers seem pointless. – Marko Popovic Dec 17 '19 at 07:10
  • @molbdnilo yes, dll which I am trying to emulate the C# DllImport in C#. Can you provide some guidelines ? – shreyash Dec 17 '19 at 07:18
  • @MarkoPopovic Thank you, can you suggest how to fix this error? – shreyash Dec 17 '19 at 07:20
  • See my updated answer. I cannot write the whole code for you, but there are many examples on using LoadLibrary and GetProcAddress, I've added a link to one of them in the answer. And if you've found the answer useful, please consider marking it as accepted. – Marko Popovic Dec 17 '19 at 07:21
  • @shreyash You can't do that. I had a quick glance at the Visual C++ SDK example (it looks like you copied part of it and tried to adjust it) and it appears to be C++/CLI, which is Microsoft's proprietary "extended dialect" of C++. You most likely need to program in that dialect rather than in C++. – molbdnilo Dec 17 '19 at 08:34

1 Answers1

2

Well, the problem is in this line:

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

There are actually two issues here. The first one is that the statement ends with ']'. I'm guessing that might be just a typo, and that you've used ';' correctly. However, what is also the problem is EntryPoint = "about" part. You cannot call a method like that in C++. I am guessing that you wanted to do this:

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

Also, as pointed out in the comment above, you have ';' after the define of DllImport. Finally, to load a DLL in C++ on Windows and use a specific function from that DLL, you should use Win32 API functions 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

Marko Popovic
  • 3,999
  • 3
  • 22
  • 37