Code meaning- structs and typecast

Hi.

I have this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct SITAL_DEVICE_INTERNAL {
	SITAL_DEVICE_HANDLE hDevice;
	char UniqueDeviceName[512];
	char DeviceID[5];
	int Bars;
	SITAL_DEVICE_INTERNAL() : hDevice((SITAL_DEVICE_HANDLE)(long)-1), Bars(0) {
		::memset(UniqueDeviceName, 0, sizeof(UniqueDeviceName));
		::memset(DeviceID, 0, sizeof(DeviceID));
	}
};

typedef void* SITAL_DEVICE_HANDLE;

SITAL_DEVICE_INTERNAL* dev = static_cast<SITAL_DEVICE_INTERNAL*>(device);
if (dev->hDevice != ((SITAL_DEVICE_HANDLE)(long)-1) && dev->hDevice != NULL) return 0;


I struggle to understand the meaning of this.

1. what is this mean: (SITAL_DEVICE_HANDLE)(long) ?

2. i have function: int Sital_OpenDevice(SITAL_DEVICE_HANDLE device)

When i call the function, considering i have device 0, 1..
How can i insert argument (i can't insert '1' clearly)?
what is the meaning of '0' argument (which works)?

Thanks, i hope it is clear 🙏
1) -1 is cast to type long which is then cast to type SITAL_DEVICE_HANDLE

2) what is the underlying type of SITAL_DEVICE_HANDLE?

I see that SITAL_DEVICE_HANDLE is void*. ie it is a pointer. Hence passing 0 is valid (null ptr) but 1 is not as address 1 is not valid. There should be a function somewhere that returns a type SITAL_DEVICE_HANDLE when given a device number.
Last edited on
> 1. what is this mean: (SITAL_DEVICE_HANDLE)(long) ?
It's a double cast.
First you cast something to a long.
Then you cast it to a SITAL_DEVICE_HANDLE

> 2. i have function: int Sital_OpenDevice(SITAL_DEVICE_HANDLE device)
This seems like a broken API to me.

Actually, the first thing you should be calling is something that returns a SITAL_DEVICE_HANDLE.
Eg
1
2
SITAL_DEVICE_HANDLE h = Sital_CreateDevice(0);  // no idea, just made this up to illustrate
int foo = Sital_OpenDevice(h);  // pass the freshly minted handle on. 


You shouldn't need to care what SITAL_DEVICE_HANDLE actually is. For sure, you shouldn't be creating handles out of thin air just to pass into the API.

It's like good old stdio FILE.
1
2
3
4
FILE *fp = fopen("filename.txt","r");
int c = fgetc(fp);
fgets(buff,BUFSIZ,fp);
fclose(fp);

You don't care what a FILE actually is. Creating and deleting it is part of the API. You just need to store it and use it.
As far as you're concerned, it's an opaque type.

Is all this documented on some website you can direct us to?
Or are we restricted to peephole glimpses through the wall around your project?

Thanks.
i can give private access if someone would like. but i can't make it public.




Topic archived. No new replies allowed.