Question

I am very new in visual c++ and I meet a problem in my development with some provided API(.lib).

Here's my code:

in header file

ref class RFID{
public:
   int connect(char* p);
private:
   HANDLE port;
}

in cpp file

#include "stdafx.h"
#include "RFID.h"
int RFID::connect(char* p){
   return RmuOpenAndConnect(port,p,0);
}

The error line is: error C2664: 'RmuOpenAndConnect' : cannot convert parameter 1 from 'HANDLE' to 'HANDLE &'

Since I am very new in visual c++ and I don't know how to solve with this error, it seems that the parameter is not just an address of "HANDLE" so that I don't know how to soft it.

Thanks for any help.

Edit: Sorry that I add the details in comment. In header file, RmuOpenAndConnect is defined as following:

int WINAPI RmuOpenAndConnect (HANDLE &hCom, char* cPort, UCHAR flagCrc); 

Because I want to use Thread in my program(http://msdn.microsoft.com/en-us/library/system.threading.parameterizedthreadstart.aspx), and it seems the class should be a "ref class" so I did.

Was it helpful?

Solution

This is because HANDLE port is member of managed class, which is subject of garbage collection. Reference to managed class member cannot be used, because class instance can change its address. You can use local variable to fix this:

int RFID::connect(char* p){
   HANGLE h = port;
   int n = RmuOpenAndConnect(h,p,0);
   port = h;    // in the case RmuOpenAndConnect changed the handle
   return n;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top