|
Independent123 wrote: The overlapped method is brilliant in its simplicity. There is no need to call GetOverlappedResult() because we only need to know that the event has occurred!
Yeah, I like that I don't like any blocking functions - I want total control!
Cheers,
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
It was already answered, but don't try TerminateThread.. causes leaks most of the time. Properly signal the thread and close it from the inside to properly release your resources.
|
|
|
|
|
I'm trying to pass a pointer to a function as an argument to another function, but I am getting compile errors.
class CModule : public CDialog
{
public:
CModule (CWnd* pParent = NULL);
...
private:
unsigned char FunctTask1(unsigned char, unsigned char);
unsigned char FunctTask2(unsigned char, unsigned char);
unsigned char FunctTask3(unsigned char, unsigned char);
void ManageTasks(unsigned char, unsigned char, unsigned char (*)(unsigned char, unsigned char));
};
unsigned char CModule ::FunctTask1(unsigned char Param1, unsigned char Param2)
{
...
}
unsigned char CModule ::FunctTask1(unsigned char Param1, unsigned char Param2)
{
...
}
unsigned char CModule ::FunctTask1(unsigned char Param1, unsigned char Param2)
{
...
}
void CModule ::ManageTasks(unsigned char Param1, unsigned char Param2, unsigned char (*ptrFunct)(unsigned char, unsigned char)
{
...
switch (ptrFunction(1,1))
{
case 0:
case 1:
}
...
}
now based on a combobox selection, I will call a different function
void CModule::OnSelchangeCombobox()
{
switch(m_Combobox->GetCurSel())
{
case 0: ManageTask(1,1,FunctTask1); break;
case 1: ManageTask(1,1,FunctTask2); break;
case 2: ManageTask(1,1,FunctTask3); break;
}
}
but i get this error on compile
'ManageTasks' : cannot convert parameter 3 from 'unsigned char (unsigned char,unsigned char)' to 'unsigned char (__cdec<br />
l *)(unsigned char,unsigned char)'
What am I doing wrong? FYI I'm using VC++ 6.0.
|
|
|
|
|
acerunner316 wrote: What am I doing wrong?
From a quick glance at your code: you are trying to pass a pointer to a member-function where a pointer to a function is expected
|
|
|
|
|
so i have to specify in the argument list of 'ManageTasks' to expect a member function and not a regular function?
How do I do that?
|
|
|
|
|
The methods FunctTask1() , FunctTask2() , FunctTask3() either need to be static or not a member of CModule .
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
In that case, I can't use it the way I had intended. I need FunctTask1,2,&3 to be able to access other member variables and functions that aren't static, so making it static wont work.
Time to rethink. Thanks for the help.
|
|
|
|
|
acerunner316 wrote: I need FunctTask1,2,&3 to be able to access other member variables and functions that aren't static, so making it static wont work.
You're not out of luck just yet. Two plausible solutions still exist. One is to change the third parameter of ManageTasks() to include CModule . I don't have the syntax readily available, but it's not uncommon and can easily be searched.
The other would be to create two versions of FunctTask1() , FunctTask2() , and FunctTask3() . One would be static and the other would not. That would look something like:
BYTE CModule::FunctTask1(BYTE Param1, BYTE Param2)
{
}
static BYTE CModule::FunctTask1(BYTE Param1, BYTE Param2, CModule *pThis )
{
return pThis->FunctTask1(Param1, Param2);
}
void CModule::ManageTasks(BYTE Param1, BYTE Param2, BYTE (*ptrFunct)(BYTE, BYTE), CModule *pThis )
{
...
switch (ptrFunction(1, 1, pThis))
{
case 0:
case 1:
}
...
}
void CModule::OnSelchangeCombobox()
{
switch(m_Combobox->GetCurSel())
{
case 0: ManageTask(1, 1, FunctTask1, this); break;
case 1: ManageTask(1, 1, FunctTask2, this); break;
case 2: ManageTask(1, 1, FunctTask3, this); break;
}
}
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
DavidCrow wrote: You're not out of luck just yet. Two plausible solutions still exist. One is to change the third parameter of ManageTasks() to include CModule. I don't have the syntax readily available, but it's not uncommon and can easily be searched.
Tried that, see by reply to Randor below.
I will try the overloaded function way. However, one of the reason I wrote my code the way it is was so that I can easily add more functions later (ie FunctTask4,5,6 etc) without having to change much code. Just add a new case in the OnSelchangeCombobox function. I can still call ManageTask to do the same things. Having to overload every single one seems inefficient to me.
|
|
|
|
|
acerunner316 wrote: Having to overload every single one seems inefficient to me.
Indeed, I was just offering all the possibilities I could think of, and let you figure out which one would be best for your situation.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
In addition to DavidCrow's reply....
You can pass pointers to member functions but to
use them you have to have an object of the class
they are a member of, just like any class method call.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
typedef unsigned char (CModule::*FUNCPTR)(unsigned char,unsigned char);
ManageTask(1,1,&CModule::FunctTask1);
|
|
|
|
|
tried that, but when I call ptrFunct(0,0); from inside ManageTask, I get an error saying "term does not evaluate to a function".
Edit: nevermind. just realized i have to call it like this:
(this->*ptrFunct)(0,0);<br />
-- modified at 18:42 Thursday 1st November, 2007
|
|
|
|
|
Modify the class as follows:
CModule : public CDialog
{
public:
CModule (CWnd* pParent = NULL);
...
private:
unsigned char FunctTask1(unsigned char, unsigned char);
unsigned char FunctTask2(unsigned char, unsigned char);
unsigned char FunctTask3(unsigned char, unsigned char);
typedef unsigned char (CModule::*PFunctTask_t)(unsigned char, unsigned char);
void ManageTasks(unsigned char, unsigned char, PFunctTask_t);
};
void CModule ::ManageTasks(unsigned char Param1, unsigned char Param2, unsigned char (*ptrFunct)(unsigned char, unsigned char)
{
...
switch ((this->*ptrFunction)(1,1))
{
case 0:
case 1:
}
...
}
And modify the calls like this:
void CModule::OnSelchangeCombobox()
{
switch(m_Combobox->GetCurSel())
{
case 0: ManageTask(1, 1, &CModule::FunctTask1); break;
case 1: ManageTask(1, 1, &CModule::FunctTask2); break;
case 2: ManageTask(1, 1, &CModule::FunctTask3); break;
}
}
-- modified at 20:49 Thursday 1st November, 2007
Steve
|
|
|
|
|
Hi all. Im trying to make a simple FTP client that uploads files. Again im trying to make a client that uploads files and not downloads. I specified that so that we could get an understanding of what im trying to do here. Anyway i was refered to a site that deals with programming for FTP Servers. Heres the url: http://www.stanford.edu/class/ee284/pa.html
he instructions were written for a UNIX programming but WinSock supports all the functions it calls for. The functions are the following:
socket()
bind()
listen()
getsockname()
accept()
Mind everyone finding a simple way to use these functions is NOT a simple task. But after spending countless hours using google/yahoo i came up with the following code:
<br />
#include <iostream><br />
#include <winsock.h><br />
using namespace std;<br />
<br />
<br />
#define port 21<br />
bool ftpmsg(const char *message);<br />
SOCKET dataSock;<br />
SOCKET socketbuffer;<br />
<br />
int main(){<br />
const int SIZE=100;<br />
char msg[SIZE];<br />
char server[SIZE];<br />
char recvbuffer[900];<br />
WORD wVersionRequested;<br />
SOCKADDR_IN addr;<br />
WSADATA wsa;<br />
struct sockaddr_in structSock;<br />
struct hostent *ftpserveraddy;<br />
wVersionRequested=MAKEWORD(2, 0);<br />
WSAStartup(wVersionRequested, &wsa);<br />
dataSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);<br />
<br />
printf("Please enter the Host name to connect to:");<br />
cin.getline(server, SIZE);<br />
printf("Connecting to FTP Server: '%s'\n",server);<br />
if((ftpserveraddy = gethostbyname(server)) == NULL){<br />
printf("DNS Lookup of '%s' failed!\n",msg);<br />
}<br />
structSock.sin_family = AF_INET; <br />
structSock.sin_port = htons(port);<br />
structSock.sin_addr.s_addr = *((unsigned long*)ftpserveraddy->h_addr_list[0]);<br />
memset(structSock.sin_zero, 0, 8);<br />
if(connect(dataSock, (sockaddr*)&structSock, sizeof(sockaddr)) == SOCKET_ERROR){<br />
cout << "Error " << endl;<br />
}<br />
recv(dataSock, recvbuffer, 512, 0);<br />
cout << recvbuffer << endl;<br />
<br />
while(1){<br />
socketbuffer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);<br />
getsockname(socketbuffer, (LPSOCKADDR)&addr, NULL);<br />
bind(socketbuffer, reinterpret_cast<SOCKADDR*>(&addr), sizeof(SOCKADDR_IN));<br />
listen(socketbuffer, 10);<br />
accept(socketbuffer, NULL,NULL);<br />
<br />
Sleep(1000);<br />
ftpmsg("USER user\r\n");<br />
Sleep(1000);<br />
ftpmsg("PASS password\r\n");<br />
Sleep(1000);<br />
ftpmsg("PORT\r\n");<br />
Sleep(1000);<br />
ftpmsg("STOR file.txt\r\n");<br />
Sleep(9000);<br />
ftpmsg("QUIT\r\n");<br />
}<br />
WSACleanup();<br />
return 0;<br />
}<br />
<br />
<br />
bool ftpmsg(const char *message)<br />
{ <br />
if(send(dataSock, message, strlen(message), 0) == SOCKET_ERROR){<br />
exit(1);<br />
return false;<br />
}<br />
else{<br />
return true;<br />
}<br />
}<br />
Notice how i try to open a new socket in the while loop because you have to open a new socket so that the data can be transfered there. I understand that much. However im getting my binding or listen wrong. I've been told that i should thread the connection but i dont see why considering its meant to do one thing. If anyone has any suggestion please let me know. I've been up and down the FTP RFC and i pretty much understand how it works. Now i just need to get the coding part right. Thanx in advance!
|
|
|
|
|
I'm not familiar with the FTP protocol so I can't address that portion of your code. However, I see some problems inside the while loop.
1) You don't check the status of any of your calls to socket, bind, listen or accept - you need to do so. I bet one of them is failing, and checking the error code returned by WSAGetLastError will help you determine what is wrong.
2) Assuming you do successfully create the new socket, you never destroy it at the bottom of the loop.
3) There is no way out of your while loop.
Judy
|
|
|
|
|
Are you doing it this way because you don't want to use the support already in windows to do this?
It's fairly easy...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />
Peter Weyzen<br />
Staff Engineer<br />
<A HREF="http://www.soonr.com">SoonR Inc -- PC Power delivered to your phone</A>
|
|
|
|
|
Peter Weyzen wrote: Are you doing it this way because you don't want to use the support already in windows to do this?
It's fairly easy...
Its off topic questions like these that annoy the common person trying to accomplish something. But if you must know why I'm attempting to do this is because i want to. Now if you have a solution to the issue at hand please feel free to offer some advice based on the question. Does anyone else have a educational solution pertaining to the issue at hand? Thanx in advance!
|
|
|
|
|
You could have answered "no". But you had to get made and respond with 4 times as many words as I did.
You also were not clear in your intent.
I tried to help.
Lame-o!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />
Peter Weyzen<br />
Staff Engineer<br />
<A HREF="http://www.soonr.com">SoonR Inc -- PC Power delivered to your phone</A>
|
|
|
|
|
Peter Weyzen wrote: You could have answered "no". But you had to get made and respond with 4 times as many words as I did.
You also were not clear in your intent.
I tried to help.
Lame-o!
First of all my intention was already said hence the topic of the thread. But since i have to spell it out for you then i'll do that. Im Trying To Make A FTP Client That Uploads. Not Something That Downloads. Im Only Trying To Get It To Upload. I hope that was simple enough.
Second of all, for the second poster. Its not my intention to flame anyone or be a jerk. My goal is to figure something out. If you were going to provide helpful suggestions then I would have gladly accepted it. But if your going to ask lame questions like "why dont i just use another FTP client" when i clearly said im trying to 'learn' how to make one. Well then how would you reply if the tables were turned?
|
|
|
|
|
Dude, you weren't clear at all.
From reading your post, your goal could have been either:
* I need to FTP stuff to a server
* I want to implement the FTP protocol myself.
We're all here on CodeProject to get help and to share our experiences with others.
If someone's not answering the question that you think you asked, then maybe you didn't ask your question clearly.
And instead of flaming me for so-called off-topic... why not just phrase your question and your intent clearly.... and apologize for being such a wanker.
Then I might help answer the question you thought you were asking.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />
Peter Weyzen<br />
Staff Engineer<br />
<A HREF="http://www.soonr.com">SoonR Inc -- PC Power delivered to your phone</A>
|
|
|
|
|
I was going to suggest something, but now I won't in case I get my nose bitten off too.
LateNightsInNewry
|
|
|
|
|
From Robert Pittenger's Article on Wizard 2000 Style Wizard, I developed my own Wizard; and the Wizard works fine. I load the Wizard from a MDI application I'm building from a Menu Selection in the ???View class of the application.(Select the menu item and the message is passed to the ???View class function to load the Wizard.)
Further, when the User selects an item from one of the pages, the User should be able to provide data thru an already loaded modeless dialog (thats created in the Wizard that contaqins the pages of the Wizard.
My problem is I cannot get the Wnd of the Wizard passed to the individual pages it contains. Since the Wizard holds the creation of the modeless dialog, it alone can get and transfer data to it and from it. Therefore I need the WIZARD'S WND for a page to connect with the modeless dialog.
CWizzardDlg* Dlg(the Wizard) CModeless* m_pModeless(modeless dialog) CPage1 m_pPg1. How can m_pPg1 talk to m_pModeless(a member of Dlg ?
A C++ programming language novice, but striving to learn
|
|
|
|
|
Hi all...
My basic question is this: I have a function that accepts a variable number of arguments using '...' and I want to pass that argument list on to another routine without processing them. I figure this has to be able to be done, so... how?
Here is a bit more information... I am working in Visual Studio 2005, C++ in console mode. I have a class set up to handle the screen (cScreen) which has several instances of 'window' classes (cScreenWindow). The idea is to allow each individual window to take care of it's own maintenance. ONE of the windows will be defined in cScreen as the 'default print window' so any output sent to the screen that isn't targeted for a specific window will print in the default.
To allow formatted printing, I want to use printf styled commands. I already have cScreenWindow::cswPrintf(const char *format, ...) and that works well if I am sending the command from cScreen. But the main program can't access the windows directly, so there is a command cScreen::csPrint(const char *format) that basically just finds the default print window, and passes 'format' to it. Everything works like that so far...
Here is the tricky part... I want the main program to be able to call a function like cScreen::csPrintf(const char *format, ...) that in turn gets the default cScreenWindow::cswPrintf(const char *format, ...) and passes 'format' AND the variable argument '...' to it.
Here is some sample code:
main{}{
cScreen csr;
csr.csPrint("This works");
csr.csPrintf("how do I pass %s??", "this");
}
class cScreen{
public:
// vars
char printWin[20]; // the title of the default print window
// functions
cScreen *getWin(const char *winName); // this part works fine
void csPrint(const char *format); // this part works fine
void csPrintf(const char *format, ...); // how do I pass this???
};
void cScreen::csPrint(const char *format){
cScreenWindow tempWin = getWin(printWin);
tempWin.cswPrintf(format); // this part works fine
tempWin.cswPrintf("this %s too!!", "works"); // this part works fine
}
void cScreen::csPrintf(const char *format, ...){
cScreenWindow tempWin = getWin(printWin);
tempWin.cswPrintf(format, ?????????); // this is the question!!!
}
In cswPrintf, I use: 'va_list argp;' and parse through the arguments when I encounter a format token. I figure there must be a way to pass the arguments through using the va_list, but I'm not sure how.... anyone got any ideas???
My default (which can be done, but I'd rather not) is to allow csPrintf to go ahead and parse the string, and send a fully integrated string to cswPrintf, but that kind of defeats the whole purpose...
Thanx for any ideas/help!
=->Mocs<-=
|
|
|
|
|
there is documentation and Google; among them they know the answer.
And you got some of the keywords already: va_list, va_start, stdarg, vsprintf, ...
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- use PRE tags to preserve formatting when showing multi-line code snippets
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|