Problem with retrieved data from C function.

So I'm using a API for card readers "eVehicle Registration API" which was made by my government. And it's simple to use, but when I call function GetReaderName(0, &name, &size) that will put values in name and size, the cout of name is weird, it prints weird symbols. I googled for a solution and I found it was a null-terminator, but I'm not quite sure if that is a solution, and if it is I don't know how to add null terminator to it. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <eVehicleRegistrationAPI.h>
#include <iostream>

void readDataFromCard() {

	auto status = sdStartup(0);

	std::cout << status << std::endl;
	std::cin.get();

        //name variable. Is this right way to do it?
	char name;
	long size;

        //initializing data to variables.
	GetReaderName(0, &name, &size);

        //printing name. but in my case, it's not working.
	std::cout << name << std::endl;
	std::cin.get();

	//SelectReader(name);

	//sdProcessNewCard();

	//SD_DOCUMENT_DATA sd_document_data;

	//sdReadDocumentData(&sd_document_data);

}

int main() {

	readDataFromCard();

	return 0;
}

Output:
0 (CARD READER WAS FOUND)
╠ (WEIRD SYMBOL)


Thanks :)!
Last edited on
You need to create an array of sufficient size to hold all the characters (plus one more for the null terminating character) in the name. For instance:

1
2
3
4
const std::size_t MAX_NAME_SIZE = 1023 ;
char name[MAX_NAME_SIZE+1] = "" ;
long size ;
GetReaderName( 0, name, &size );

Now it's just empty :/.
Try (this is based on pure guesswork):
1
2
3
4
const long MAX_NAME_SIZE = 1023 ;
char name[MAX_NAME_SIZE+1] = "" ;
long size = MAX_NAME_SIZE ;
GetReaderName( 0, name, &size );


Hard to give any other meaningful suggestion in the absence of any information about the function being called.
one of 2 things looks like it MAY work.

1) ...
char dest[100] = {0}; //pre-fill the array with zeros (null terminal chars)
//100 is just a value... use whatever the maximum you expect to get + 1 as said already
GetReaderName(0,dest,&size); //hopefully leaves the zeros in place making it work

or

2)
GetReaderName(0,dest,&size);
dest[size] = 0; //insert null terminal here.

Last edited on
1
2
3
4
const long MAX_NAME_SIZE = 1023 ;
char name[MAX_NAME_SIZE+1] = "" ;
long size = MAX_NAME_SIZE ;
GetReaderName( 0, name, &size );


It's working! Thanks! The error code was 0x80100008 which means "The data buffer to receive returned data is too small for the returned data" SCARD_E_INSUFFICIENT_BUFFER.
Last edited on
Registered users can post here. Sign in or register to post.