Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Handle reuse

Status
Not open for further replies.

KodeMunki

Programmer
Feb 26, 2007
4
US
Hello all
kindof a noob here but Im having an issue with trying to write a class that allow me to create a method for socket handle reuse but its not working so good I can always create the directory but when I goto write the file I get handle invalid errors, its definately a handle issue Im thinking im missing something obvious here can anybody help, here is the basic pseudo code of what im trying to do.

class FTPjunk
{
public:
FTPjunk();
HINTERNET hOpenHandle;
HINTERNET hConnectionHandle;
bool SetHandles(char* mode);
bool CreateDirectory(char* directory_name);
bool FTPjunk::WriteFile(char* file_name);
private:
~FTPjunk();
}

FTPjunk::FTPjunk()
{
HINTERNET hOpenHandle = null;
HINTERNET hConnectionHandle = null;
}

FTPjunk::~FTPjunk()
{
}

bool FTPjunk::SetHandles(char * mode)
{
if(mode == "OPEN") {
hOpenHandle = InternetOpen(lpszAgent,
dwAccessType,
lpszProxyName,
lpszProxyBypass,
dwFlags);

hConnectionHandle = InternetConnect(hOpenHandle,
lpszServerName,
nServerPort,
lpszUsername,
lpszPassword,
dwService,
dwflags,
0);
}
else if(mode == "CLOSE") {

InternetCloseHandle(hOpenHandle);
InternetCloseHandle(hConnectionHandle);
}
}

bool FTPjunk::CreateDirectory(char* directory_name);
{
SetHandles("OPEN");
FtpCreateDirectory(hConnectionHandle, directory_name);
SetHandles("CLOSE");
return true;
}

bool FTPjunk::WriteFile(char* file_name);
{
SetHandles("OPEN");
FtpPutFile(hConnectionHandle,
file_name,
file_name,
dwflags,
0);
SetHandles("CLOSE");
}

}

int main()
{
FTPjunk ftpjunk = new FTPjunk();
ftpjunk->CreateDirectory("test_directory");
ftpjunk->WriteFile("test_file");
return 0;

}


 
1. Please use [code][/code] tags when posting code.

2. if(mode == "OPEN")
This is NOT how you compare a char* variable.
Try [tt]if( strcmp(mode,"OPEN") == 0 )[/tt]

It might just work if your compiler folds identical string constants (so "OPEN"=="OPEN" is true), but that's highly variable, and certainly won't work if you're dealing with any kind of user input.

Why are you using something like char* in a C++ program, when the std::string is much safer.



--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Its just pseudo code to give the idea of what im doing.
I dont have the actual code anymore because I rewrote my class to open and close handles as needed inside each method, which is really sloppy but I couldnt get the HANDLE reuse portion to work.

My question is about Handle reuse not the pseudo code its not really real
 
Well I figure that for you to know that "handle re-use doesn't work", you must have actually tried something.

So how about actually posting what you really tried, so we can actually debug it?

If you ask "something like" questions, expect "something like" answers which go nowhere near the actual problems.

> Its just pseudo code to give the idea of what im doing.
As pseudo-code, it looks quite nice, and on the face of it, the reasonable thing to try first.

> I dont have the actual code anymore because I rewrote my class to open and close handles as needed
That's unfortunate.

> InternetCloseHandle(hOpenHandle);
> InternetCloseHandle(hConnectionHandle);
Here's a thought, close them in the opposite order you created them in, so the last handle opened is the first one closed.

Also, since these things return a result
msdn said:
BOOL WINAPI InternetCloseHandle(
HINTERNET hInternet
);
maybe throw in some error checking code to make sure these things actually worked.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Im really not looking to get into all that, because then everyone tends to critique the way you did your error checking and exception handling and ask why dont you do it my way and why did you do this and that, losing scope of the initial question. Im just trying to keep it simple is all.

Yes I had plenty of exception handling in my original code and checked the values of what was returning and found as I already stated. "I can always create the directory but when I goto write the file I get handle invalid errors". Im suspecting that perhaps I may need to do something different in my approach, and am trying to see if someone has done something like this before and is noticing that I am missing some important step in what I am doing.

The code I provided shows all the steps I did in my original code to deal with the HANDLE reuse minus the exception handling and checking routines (since they are not germane to what Im seeking, because I know the error already "invalid handle on second use to writefile" this happens predictably).

 
Big upps to Mark, that worked like a champ I knew it was some stupid thing I was overlooking my code and I both thank you. trick was reversing the close order AND resetting
to NULL

Here was the change I did:
Code:
bool FTPjunk::SetHandles(char * mode)
{
   if(mode == "OPEN") {
      hOpenHandle = InternetOpen(lpszAgent,
                                 dwAccessType,
                                 lpszProxyName,
                                 lpszProxyBypass,
                                 dwFlags);
      
      hConnectionHandle = InternetConnect(hOpenHandle,
                                          lpszServerName,
                                          nServerPort,
                                          lpszUsername,
                                          lpszPassword,
                                          dwService,
                                          dwflags,
                                          0);
   }
   else if(mode == "CLOSE") {

      InternetCloseHandle(hConnectionHandle);
      InternetCloseHandle(hOpenHandle);
      
      hConnectionHandle = NULL;
      hOpenHandle = NULL;      
   }
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top