Proper way to use TF_GetServer()

Hi Again 🙂

My objective now is to get TurboFloat's saved server address and port to appear in my UI. So, I'm making use of the TF_GetServer() function but I'm not sure what I'm doing wrong. My C++ string / buffer skills are severly lacking, so I apologize if this is not obvious.

I've tried this:

HRESULT hr;STRTYPE savedServerAddress = NULL;short * savedServerPort = NULL;

hr = TF_GetServer(tfHandle, savedServerAddress, 0, savedServerPort);

if (hr == TF_OK){ savedServerAddress = (TCHAR *)malloc(hr * sizeof(TCHAR)); hr = TF_GetServer(tfHandle, savedServerAddress, *savedServerAddress, savedServerPort); if (hr == TF_OK) { //convert saved server into a string for UI. }}

On the first call to TF_GetServer() in the above code, I pass in 0 for the cchHost so I can get the correct size of the buffer. However, when I break on the line that allows me to see the returned result into the hr variable, I see the following:

hr 0x0000000a : The environment is incorrect

So, I thought that meant something is not right when I actually save the server, but I'm saving it with TF_SYSTEM in the TF_SaveServer() function. Any idea what this means? And, is my code proper for calling TF_GetServer()?

Thanks again for your help!

--Arie

On the first call to TF_GetServer() in the above code, I pass in 0 for the cchHost so I can get the correct size of the buffer. However, when I break on the line that allows me to see the returned result into the hr variable, I see the following:

hr 0x0000000a : The environment is incorrect

Well, that's because it's not a return code in that instance. It's a length. Which is why you use "hr" like this:

savedServerAddress = (TCHAR *)malloc(hr * sizeof(TCHAR));

If "hr" were an error code in this case, then you wouldn't be able to generate a buffer based on "hr".

Right, I totally forgot 🙂

This is what I've come up with, but now I get an Unhandled Exception thrown only in some cases:

TCHAR * savedServerAddress;short * savedServerPort = NULL;

hr = TF_GetServer(tfHandle, savedServerAddress, 0, savedServerPort);savedServerAddress = (TCHAR *)malloc(hr * sizeof(TCHAR));hr = TF_GetServer(tfHandle, savedServerAddress, hr, savedServerPort);

The first call to TF_GetServer() appears to work, no exception thrown there. However, on the second call, Visual Studio pops up a window that states:

First-chance exception at 0x00007FFFCD479392 (TurboFloat.dll) in {hostapp}.exe:0xC0000005: Access violation writing location 0x00000....

I'm thinking there is something wrong with the way I'm declaring savedServerAddress or the way I'm sending it. I'm sorry to be such a bother on this, but is there anything you can see that I'm doing wrong?

Thanks,Arie

Okay the issue was that the savedServerPort variable I declared did not have any memory allocated to it, so I did the following:

TCHAR * savedServerAddress;short * savedServerPort = new short();string sLicenseServer = " ";

hr = TF_GetServer(tfHandle, 0, 0, 0);savedServerAddress = (TCHAR *)malloc(hr * sizeof(TCHAR));hr = TF_GetServer(tfHandle, savedServerAddress, hr, savedServerPort);

It was that I didn't have `new short();` in the savedServerPort declaration

Whew, the C++ plus stuff really stresses me out 🤔

Oh, I didn't even spot that in your code. An alternative is to just declare the short and then pass the reference to the function. Like so:

TCHAR * savedServerAddress;short savedServerPort = 0;

hr = TF_GetServer(tfHandle, 0, 0, 0);savedServerAddress = (TCHAR *)malloc(hr * sizeof(TCHAR));hr = TF_GetServer(tfHandle, savedServerAddress, hr, &savedServerPort);