|
Hi all,
I need to generate and display a GUID everytime the user is clicking a button. I am using MFC and its a dialog based application. How can I do this?
Aljechin.
|
|
|
|
|
void CMyCoolDialog::OnButtonGenerateGUID()
{
GUID guid;
::CoCreateGuid(&guid);
}
Ovidiu Cucu
Microsoft MVP - Visual C++
|
|
|
|
|
From one of my client applications, I open non-blocking TCP sockets to multiple servers and then immediately call select() on all these sockets with a time out value of 30 seconds. The select() always returns before timeout value and its value is always less than the available servers.
For example I open 3 non blocking TCP connections for connecting to 3 up and running servers on same machine. Then I call select() on these 3 sockets. The return value of select() is 2 rather than 3. Even the logs of 3 servers shows connection attempts.
Following is code snippets:
SOCKET fds[MAX_SOCKETS];
struct sockaddr_in ads [MAX_SOCKETS];
for (int nNumConnections = 0; nNumConnections < 3; nNumConnections++)
{
ads[nNumConnections].sin_addr.s_addr = inet_addr(lpszIpAddr);
ads[nNumConnections].sin_family = AF_INET;
ads[nNumConnections].sin_port = htons((unsigned short) (nPort[nNumConnections]) );
}
fd_set writeFDS,exceptFDS;
FD_ZERO (&writeFDS);
FD_ZERO (&exceptFDS);
int nMaxFd = 0;
int nSocketCount = 0;
for (int i = 0; i < 3; i++)
{
fds[i] = socket( AF_INET, SOCK_STREAM , IPPROTO_TCP );
unsigned long setBlocking = 1;
if ((ioctlsocket (fds[i], FIONBIO, &setBlocking)) != 0)
{
return -1;
}
int nResult = connect(fds[i], (SOCKCONST sockaddr *) &ads[i], sizeof (ads[i]));
if (nResult == 0)
{
if (fds[i] >= nMaxFd) nMaxFd = fds[i] + 1;
FD_SET (fds[i], &writeFDS);
FD_SET (fds[i], &exceptFDS);
nSocketCount ++
}
else if (nResult == SOCKET_ERROR)
{
if(WSAGetLastError() == WSAEWOULDBLOCK)
{
if (fds[i] >= nMaxFd) nMaxFd = fds[i] + 1;
FD_SET (fds[i], &writeFDS);
FD_SET (fds[i], &exceptFDS);
nSocketCount++;
}
}
}
cout << "Socket count in the FD_SET :" << nSocketCount;
if (nSocketCount > 0)
{
struct timeval tv;
tv.tv_sec = 60;
tv.tv_usec = 0;
nSelectStatus = select (nMaxFd, NULL, &writeFDS, &exceptFDS, &tv);
cout <<"Return value of select :" << nSelectStatus;
if (nSelectStatus <=0 || nSelectStatus == SOCKET_ERROR)
{
}
else
{
for (int i = 0; i < 3; i++)
{
if (FD_ISSET (fds[nIndex], &exceptFDS))
{
}
else if (FD_ISSET (fds[nIndex], &writeFDS))
{
}
}
}
}
return 0;
}
Output:
Socket count in the FD_SET :3
Return value of select :2 (sometime it comes 1 or 3)
I am not very clear the functionality of select() in case of multiple sockets.
Does it wait for all sockets to be ready OR return as soon as get some of the sockets running ?
-- modified at 0:06 Friday 1st September, 2006
|
|
|
|
|
first of all, edit your post (don't repost all, use the edit link), and put all your code into <pre></pre> tags, so that we can rean your question more clearly.
use also the < and > buttons for your for loops
|
|
|
|
|
Hi there!
I’m trying to use the CAnimateCtrl in a CDialog based class so that I can use it as a kind of CWaitCursor thing, that will show a “Please wait…” message an plays a AVI.
Now, since I would also like to use the “XP Theme”, I’ve defined a “manifest” as a resource, and as a result the AVI file will not play.
In order to make it work, I tried to use a thread as the one in the MSDN sample (CAnimateCtrl::CAnimateCtrl in MFC Library Reference).
The problem is when the CAnimateCtrl is being destroyed it takes too long (?), and it also makes the application crash. The MSDN says that "... If you create the CAnimateCtrl object on the stack, it is destroyed automatically."
Below you can see the most relevant (I think) parts of the code. Can someone please tell me what am I doing wrong?
Thanks in advance.
CWorkingDlg::CWorkingDlg( LPCSTR szLabel, bool bProgress, int nMaxProgress, bool bShowCancel, UINT nIdAVI, CWnd* pParent)
: CDialog(CWorkingDlg::IDD, pParent)
{
m_bCancelState = false;
m_dwThreadId = 0;
m_hThreadAvi = NULL;
m_hEventAvi = NULL;
m_bCancel = bShowCancel;
m_bProgress = bProgress;
m_nMaxProgress = nMaxProgress;
CString strLabel(szLabel);
if(strLabel.IsEmpty())
strLabel = "A processar. Aguarde por favor...";
m_strLabel = strLabel;
if (nIdAVI==0)
nIdAVI=IDR_XX_AVI_RODAS;
m_nAVI=nIdAVI;
EnableModal(pParent);
if(!Create(CWorkingDlg::IDD, pParent))
AfxThrowUserException();
ShowWindow(SW_SHOW);
DispatchMsg();
}
CWorkingDlg::~CWorkingDlg()
{
if (m_hThreadAvi)
{
::PostThreadMessage(m_dwThreadId, WM_STOPCLIP, 0L, 0L);
::WaitForSingleObject(m_hEventAvi, INFINITE);
m_hThreadAvi=NULL;
}
if (m_hEventAvi)
{
::CloseHandle(m_hEventAvi);
m_hEventAvi=NULL;
}
DisableModal(GetParent());
if (GetSafeHwnd())
DestroyWindow();
}
void CWorkingDlg::DispatchMsg()
{
MSG msg;
DWORD dwi=GetTickCount();
while (::PeekMessage(&msg, NULL, 0L, 0L, PM_NOREMOVE))
{
if (::GetMessage(&msg,NULL,0L,0L))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
else
return;
if ((GetTickCount()-dwi)>2000) // 2s
return;
}
}
UINT CWorkingDlg::MyClipThreadProc(LPVOID pParam)
{
AviInfo *pInfo=(AviInfo *)pParam;
CWnd* pParentWnd = CWnd::FromHandle(pInfo->hWndParent);
CRect rc(10,10,100,100);
CWnd *pAvi=pParentWnd->GetDlgItem(IDC_XX_AVI);
if (pAvi)
{
pAvi->GetWindowRect(&rc);
pParentWnd->ScreenToClient(&rc);
}
UINT nIdAVI=pInfo->nIdAvi;
HANDLE hEvent=pInfo->hEvento;
delete pInfo;
pInfo=NULL;
// Create the animation control.
try
{
CAnimateCtrl cAnimCtrl;
if (!cAnimCtrl.Create(WS_CHILD|WS_VISIBLE|ACS_CENTER,
rc, pParentWnd, IDC_XX_AVI))
{
::SetEvent(hEvent);
return false;
}
// Open the AVI file.
if (!cAnimCtrl.Open(nIdAVI))
{
::SetEvent(hEvent);
return false;
}
// Pump message from the queue until the stop play message is received.
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) && (msg.message != WM_STOPCLIP))
{
switch (msg.message)
{
// Start playing from the first frame to the last,
// continuously repeating.
case WM_PLAYCLIP:
if (!cAnimCtrl.Play(0, -1, -1))
return false;
break;
}
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
cAnimCtrl.Stop();
cAnimCtrl.Close();
}
catch(...)
{
_com_error e(::GetLastError());
TRACE("\n--> Error: %s", e.ErrorMessage());
}
::SetEvent(hEvent);
return true;
}
BOOL CWorkingDlg::OnInitDialog()
{
CDialog::OnInitDialog();
CenterWindow();
m_hEventAvi=::CreateEvent(NULL,TRUE,FALSE,"Event_AVI");
AviInfo *pInfo=new AviInfo;
pInfo->hWndParent=GetSafeHwnd();
pInfo->nIdAvi=m_nAVI;
pInfo->hEvento=m_hEventAvi;
CWinThread *pThread=AfxBeginThread(MyClipThreadProc, pInfo);
if (pThread)
{
m_hThreadAvi=pThread->m_hThread;
m_dwThreadId=pThread->m_nThreadID;
::PostThreadMessage(m_dwThreadId, WM_PLAYCLIP, 0L, 0L);
}
else
delete pInfo;
return TRUE;
}
ALMC
|
|
|
|
|
Did you use your debugger to see where it crashes exactly ?
Moreover, don't post a large segment of code like that (and also use the pre tag to format it a little bit better so that it's readable).
|
|
|
|
|
Hi,
Sorry about the formating...
The debugger is only showing some assembly, and the call stack is somewhere in the MFC...
Not much help...
ALMC
|
|
|
|
|
almc wrote: Not much help...
So start peeling away little bits of code until you are left with only what's necessary to reproduce the problem.
"Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Hello,
For what I can see, if I change the thread so that I can call it as a function, the problem also happens... So it's not a thread thing.
But I don't know hat else can I do...
Any ideas?
Thanks for you time.
ALMC
|
|
|
|
|
Hi,
Can we use a bitmap as a background to a dialog by writing the suitable code in OninitDialog function .
Thanks in advance,
Radhika.
|
|
|
|
|
radhika28 wrote: bitmap as a background to a dialog
See WM_PAINT:
Use BitBlt()/StretchBlt() if you want to set the image as a background on the dialog
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
See
<br />
#include AtlImage.h<br />
CImage m_Image;<br />
<br />
m_Image.Load("c:\\0.bmp");<br />
<br />
void CanswerDlg::OnPaint() <br />
{<br />
CPaintDC dc(this);
m_Image.BitBlt(dc.m_hDC,0,0,640,480,0,0);<br />
}<br />
<br />
|
|
|
|
|
I need to find the Spanish and Italian translation of the text which is displayed on the windows message box buttons: OK, Yes, No and Retry. Can anyone provide them, or point me to somewhere where I'd find them myself? I also need the translation of the the word "Back" (as in back to the previous menu) for French, German, Spanish and Italian - google translator seems to interpret it as back-side!
Joel Holdsworth
|
|
|
|
|
Joel, i think you can ask this in the Lounge... it is general enough to be asked there
moreover, you'll get more answers, as the Lounge counts much more people, coming from different technical forums...
|
|
|
|
|
AFAIK, microsoft has a web page with translation of the common "microsoft" control text. Search the msdn for that, or even this forum, there was a thread about that recently.
~RaGE();
I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
|
|
|
|
|
yes, he asked it this morning
|
|
|
|
|
I have an edit box and its variable is m_strEdit.Its a multiline edit box.
I want to shwo two string in two lines in that edit box and i have written like this,
......<br />
UpdateData();<br />
m_strEdit = "one"+"\n"+"two";<br />
UpdateData(false);<br />
......
but its showing it as
one|two
in the edit box.
How to solve this.
KIRAN PINJARLA
|
|
|
|
|
Turn the want return property of the edit box to true.(If you like to)
m_strEdit = "one"+"\r\n"+"two";
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
Hi,
I need to detect whether is left mouse button is down..
Do you want how do it?
I uset ::GetCursorPos() but I need to detect mouse flags too..
Thanks
|
|
|
|
|
|
In the properties pane there are messages which you can override.
If you override WM_LBUTTONDOWN it should generate a routine called OnLButtonDown.
At least this is the way I do it.
Hope this is what you are looking for.
|
|
|
|
|
majco333 wrote: ::GetCursorPos() but I need to detect mouse flags too..
case WM_MOUSEMOVE:
if(wParam & MK_LBUTTON)
{
}
break;
Hope that I understood you correctly
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
I know but I need to detect mouse flags out of child window...
|
|
|
|
|
Can you clarify what exactly do you want to do?
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
I have own CWnd object. I need to detect mouse flags when mouse is out of this CWnd object. For example, I to handle mouse move in this
CWnd object but I want to handle mouse move when mouse is out this object.
I wand correct to handle mouse move in this object. Because in this object
is image and I need to change XOffset and YOffset by mouse move - do not only in CWnd object but out of CWnd object too...
|
|
|
|
|