|
You mean this article is not good?
but i felt this is what you are looking for.
Величие не Бога может быть недооценена.
|
|
|
|
|
No this is not my mean,
i mean to say that its diplay the number of combination instead of all diffrent combination.
i really need all cobination,not the number of combination.
as i already mention in my thread.
like for::
AB i want "AB";"BA";"AA";"BB";
if this code generate this type of combinations please tell me how can i do this.
thanks.
|
|
|
|
|
You may go recursive for that.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Hi All
I am try to make number of possible key combination.I use google then i found number of example permutation and combination.There is common result in permutation and combination ignore repeated
Can any any help to permutation and combination ignore allowed?
Like CString test="AB";
out put->
AB
BA
AA
BB
Please help me
|
|
|
|
|
You may go recursive for that.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Hi,
Do the following will cause memory leak?
CString temp; //global variable
temp = "hello"; //Assignment 1
temp = "world"; //Assignment 2
What will happen to the memory allocated for "hello"? Will it be released at assignment of "world"?
Thanks
JC
|
|
|
|
|
TechAvtar wrote: What will happen to the memory allocated for "hello"? Will it be released at assignment of "world"?
Yes. The current content of a CString variable will be automatically released when the variable goes out of scope, as well.
[edit]
Would someone like to explain why this has been 1-voted? The answer is correct. There may be some hedging involved as to whether new memory is actually allocated depending on the length of the two strings, as Joe pointed out, but the point is the reassignment of the variable will not result in a leak.
THAT was the question.
I assume the individual who voted it a 1 will now present some proof indicating that a leak WILL occur, justifying the 1?
[/edit]
L u n a t i c F r i n g e
|
|
|
|
|
CString is MFC class.
And the buffer it uses inside this class is handled by CString, it has nothing to do with user unless you are creating a pointer of the CString( Which is not at a good practice.)
Inside this CString class, destructor is having the code to delete the memory allocated for its internal buffer. So dont worrry, CString will delete that memory for you when it goes out of scope
Величие не Бога может быть недооценена.
modified on Monday, April 5, 2010 3:06 AM
|
|
|
|
|
But what if the usage is as below? The variable temp is not going out of scope.
Will it cause leak?
CString temp; //global variable
while(1)
{
temp = "hello"; //Assignment 1
temp = "world"; //Assignment 2
}
|
|
|
|
|
It will release the memory of previously allocated pointer on the = operator of CString.
See the code of CString.
The specified function is called inside the = operator.
void CString::AllocBeforeWrite(int nLen)
{
if (GetData()->nRefs > 1 || nLen > GetData()->nAllocLength)
{
Release();
AllocBuffer(nLen);
}
ASSERT(GetData()->nRefs <= 1);
}
Hope you are now clear with it
Величие не Бога может быть недооценена.
modified on Monday, April 5, 2010 3:53 AM
|
|
|
|
|
Since "world" is the same length as "hello", no reallocation will be done; "world" will just overwrite "hello".
If you set a string "hello world", the internal buffer would be reallocated in a safe manner, resulting in no leak. (For optimization reasons, CString sometimes allocates a larger buffer than requested, so even setting a larger string won't result in a reallocation.)
|
|
|
|
|
I've written a Win32 application that runs and displays a window, but I'm trying to run a second window.
After pressing a button or any other similar event.
The problem is that everywhere I looked, the second dialog was created using the Dialog function.
I was wondering is their any possible way to start one using Win32 API.
Appreciate any notes on this matter/subject.
|
|
|
|
|
Hia.
Yeah, sure this is possible - in perhaps fewer lines of code than you may expect.
You'll want to declare a static HWND in the windowProcedure for the first window, or declare it as a global.
Then it's just a matter of creating the new window.
I keep a flag that tells me if the child window currently exists or not, then I act accordingly - either setting the window text, or creating the window.
if (!isChildOpen)
{
GetWindowRect(hwndDlg, &myRect);
imgChildHWND = CreateWindowEx(0, "picWnd", myWad.getLumpName(resNum), WS_SYSMENU|WS_CAPTION|WS_POPUP|WS_VISIBLE|WS_CHILD, myRect.right, myRect.top, 300, 300, hwndDlg, NULL, hInst, 0);
isChildOpen = true;
}
else
SetWindowText(imgChildHWND, myWad.getLumpName(resNum));
The child window sends a message to the controlling window whenever it is closed so I know if the window is open or not without resorting to findWindow,
i.e
case WM_CLOSE:
PostMessage(GetParent(hwnd), IDC_CHILDCLOSING, 0, 0);
Where IDC_CHILDCLOSING is a message I've defined (#define IDC_CHILDCLOSING 1011)
|
|
|
|
|
I got it to work thank you very much.
However their is other problems.
How will I differentiate the window messages between parent and child window.
For the controls, parent has a ListView child has a ComboBox.
|
|
|
|
|
That's okay, it's my pleasure.
Each window receives messages from its own controls, so the parent gets WM_NOTIFY and LVN_xxxxxx messages, while the child window gets WM_COMMAND and CBN_xxxxxxx messages.
The key to the scheme is that you need to treat the child window like a custom control, and remember to pass messages from it to it's parent window. You'll need to tell the parent
when something in the child window has happened - e.g a combobox event, you may also wish to send messages from the parent to the child - e.g take this array of strings and add each one to the combobox.
In short, just think of the child window as a control and remember to pass messages between the control and it's parent whenever something needs to be done(e.g fill combo with array of strings), or whenever something has happened (e.g chosen item in combobox has changed)
|
|
|
|
|
I tried to detect which window handle is reporting to WM_CLOSE.
So I set up
if (hwnd == g_hWindow)
{
}
if (hwnd == g_hChildWindow)
{
}
However when I close the child window the message box goes smooth and I call destroy window with the g_hCildWindow passed along the function.
But then my main window gets killed along the way after it.
Even the background color of the child window is messed up.
I did setup the PAINSTRUCT like on the default main window.
But it doesn't seem to kick in.
|
|
|
|
|
I still can't help but get the feeling that you're using a single WindowProcedure to handle both the Parent window and the Child window.
As far as I know, you have to use _seperate_ procs for each window. With that in mind, there is no issue when it comes to finding out who generated a message - the message automatically goes to it's own parent. Perhaps I'm missing something?
Here's about the smallest, simplest (& unfortunately perhaps the nastiest) example I can think of at 4:30am
#include <windows.h>
LRESULT CALLBACK childWndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK parentWndProc(HWND, UINT, WPARAM, LPARAM);
bool myRegClass(WNDPROC lpfnWndProc, char *szClassName, HINSTANCE hInst);
char *szParentClassName = "parentClass", *szChildClassName = "childClass";
HINSTANCE hThisInstance;
HWND parentWnd;
#define IDC_FROMPARENT 1000
#define IDC_FROMCHILD 1001
int WINAPI WinMain (HINSTANCE hCurInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwndParent;
MSG messages;
hThisInstance = hCurInstance;
if (myRegClass(parentWndProc, szParentClassName, hThisInstance) == false)
return 0;
if (myRegClass(childWndProc, szChildClassName, hThisInstance) == false)
return 0;
parentWnd = CreateWindowEx(
0,
szParentClassName,
"Code::Blocks Template Windows App - parent",
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
544,
375,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
bool myRegClass(WNDPROC lpfnWndProc, char *szClassName, HINSTANCE hInst)
{
WNDCLASSEX wincl;
wincl.hInstance = hInst;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = lpfnWndProc;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (RegisterClassEx (&wincl))
return true;
else
return false;
}
LRESULT CALLBACK parentWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND childWnd, listBox;
switch (message)
{
case WM_CREATE:
childWnd = CreateWindowEx(
0,
szChildClassName,
"Code::Blocks Template Windows App - child",
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
544/2,
375/2,
NULL,
NULL,
hThisInstance,
NULL
);
CreateWindowEx(0, "BUTTON", "Msg to Child", WS_VISIBLE | WS_CHILD | WS_TABSTOP | 0x00000001, 8, 8, 150, 23, hwnd, (HMENU)IDOK, hThisInstance, 0);
return 0;
break;
case IDC_FROMCHILD:
MessageBox(NULL, "Child window sent me a message", "Parent", MB_OK);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
PostMessage(childWnd, IDC_FROMPARENT, 0, 0);
break;
}
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK childWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
CreateWindowEx(0, "BUTTON", "Msg to Parent", WS_VISIBLE | WS_CHILD | WS_TABSTOP | 0x00000001, 8, 8, 150, 23, hwnd, (HMENU)IDOK, hThisInstance, 0);
return 0;
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
case IDC_FROMPARENT:
MessageBox(NULL, "Parent window sent me a message", "Child", MB_OK);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
PostMessage(parentWnd, IDC_FROMCHILD, 0, 0);
break;
}
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
|
|
|
|
|
As I see your experienced with Win32 API, what is the effect of hInstance. I usually ignore it and pass it as NULL in CreateWindow, what problems can plague by doing so.
|
|
|
|
|
I've played with it a little over the years. I think I've got vague memories of passing NULL as well, but had forgotten about the trick. It's a perfect time you should ask what may go wrong, as I see there's a thread that asks just the same question right here[^].
|
|
|
|
|
I'm the one asking.
But I still can't understand a thing from the blog, I guess my mind is a bit clouded atm.
|
|
|
|
|
Ah crap!
Guess I should've looked a little closer. :grin:
From what I understand of the blog, this is used as a way of differentiating between different modules (dlls) that are loaded by an app.
Imagine I had an app that loaded 3 dlls, each one of them has an icon in it with the resource ID of 1000, yet each of the icons are entirely different. In order to reliably load the desired resource, you specify the hInstance of the DLL that contains it.
Well, that's my take on it anyway.
|
|
|
|
|
I've run this code in Visual Studio 2010.
I now have a deeper aspect of WS_VISIBLE, instead of having ShowWindow.
And I can understand the buttons and each message passing a message to the other.
It's a new example of Win32 communication taking place.
I'm really thankful for your help.
My program creates a main window and some time along the way I create a second window.
Hopefully that will be through the Win7 JumpList or a Shortcut Key, currently I place the code for triggering this in the message queue to trigger it. The problem was how to start another window, while the parent is running.
Please advise on how to create a new window, while the main window is running.
Although the Win32 documentation is a clear desert, MFC is just an encapsulation of that desert.
I can't figure it out yet, although I have worked with other GUI code.
Native is the way to go, but hard to walk the path if your mapless.
|
|
|
|
|
|
i'm doing a simple program in socket programming on c i have server that can handle 2clients in a single machine i'm running ubuntu linux so i got it work but the probelm when clients send a message the server will echo it but i cant distinguish which client send the message client 1 or client 2 here is the code for the server whenever a client sent a message it will be shown in the server side as( client sent : hi)i cant know which client sent it please help
#include <stdio.h><br />
#include <stdlib.h><br />
#include <string.h><br />
#include <arpa/inet.h><br />
#include <sys/types.h><br />
#include <sys/socket.h><br />
<br />
#define BUFFER_SIZE 1024<br />
<br />
int serverfd;<br />
int clientfd;<br />
<br />
int n;<br />
int addrSize;<br />
int serverPort;<br />
char clientIP[25];<br />
char buffer[BUFFER_SIZE];<br />
struct sockaddr_in serverAddr;<br />
struct sockaddr_in clientAddr;<br />
<br />
int main(int argc, char* argv[])<br />
{<br />
if(argc < 2)<br />
{<br />
printf("Usage: ./server <port>\n");<br />
exit(0);<br />
}<br />
else<br />
{<br />
serverPort = atoi(argv[1]);<br />
}<br />
<br />
addrSize = sizeof(struct sockaddr);<br />
<br />
<br />
serverfd = socket(AF_INET, SOCK_STREAM, 0);<br />
<br />
if(serverfd < 0)<br />
printf("Error creating server socket");<br />
serverAddr.sin_family = AF_INET; <br />
serverAddr.sin_port = htons(serverPort); <br />
serverAddr.sin_addr.s_addr = INADDR_ANY; <br />
memset(&(serverAddr.sin_zero), '\0', 8); <br />
<br />
<br />
if(bind(serverfd, (struct sockaddr*)&serverAddr, sizeof(struct sockaddr)) == -1)<br />
printf("Could not bind()\n");<br />
<br />
<br />
if(listen(serverfd, 10) == -1)<br />
printf("Could not listen()\n");<br />
fork();
<br />
clientfd = accept(serverfd, (struct sockaddr*)&clientAddr, &addrSize);<br />
<br />
<br />
snprintf(clientIP, sizeof(clientIP), "%s", inet_ntoa(clientAddr.sin_addr));<br />
printf("Got connection from %s\n", clientIP);<br />
<br />
do<br />
{<br />
n = recv(clientfd, buffer, BUFFER_SIZE, 0); <br />
buffer[n] = '\0'; <br />
<br />
send(clientfd, buffer, BUFFER_SIZE, 0); <br />
printf("Client sent: %s\n" , buffer);<br />
}<br />
while(strncmp(buffer, "exit", 4) != 0); <br />
<br />
close(serverfd); <br />
<br />
return 0;<br />
}
here is the client.c code
#include <stdio.h><br />
#include <stdlib.h><br />
#include <string.h><br />
#include<br />
<arpa/inet.h><br />
#include<br />
<sys/types.h><br />
#include<br />
<sys/socket.h><br />
<br />
#define<br />
BUFFER_SIZE 1024<br />
<br />
int sockfd;<br />
<br />
int n;<br />
int addrSize;<br />
int serverPort;<br />
char serverIP[25];<br />
char<br />
buffer[BUFFER_SIZE];<br />
struct sockaddr_in serverAddr;<br />
<br />
int main(int argc, char* argv[])<br />
{<br />
if(argc < 3)<br />
{<br />
printf("Usage: ./client <server ip> <server port>\n");<br />
exit(0);<br />
}<br />
else<br />
{<br />
snprintf(serverIP, sizeof(serverIP), "%s", argv[1]);<br />
serverPort = atoi(argv[2]);<br />
}<br />
<br />
addrSize = sizeof(struct sockaddr);<br />
<br />
<br />
sockfd = socket(AF_INET, SOCK_STREAM, 0);<br />
<br />
if(sockfd < 0)<br />
printf("Error creating socket");<br />
<br />
serverAddr.sin_family = AF_INET;<br />
serverAddr.sin_port = htons(serverPort); <br />
inet_pton(AF_INET, serverIP, &serverAddr.sin_addr); <br />
memset(&(serverAddr.sin_zero), '\0', 8); <br />
<br />
<br />
if(connect(sockfd, (struct sockaddr*)&serverAddr, addrSize) != 0)<br />
{<br />
printf("Error: Could not connect to %s on port %d\n", serverIP, serverPort);<br />
exit(0);<br />
}<br />
else<br />
printf("Connected to %s on port %d\n\n", serverIP, serverPort);<br />
<br />
do<br />
{<br />
printf("> ");<br />
fgets(buffer, BUFFER_SIZE, stdin);<br />
send(sockfd, buffer, strlen(buffer), 0); <br />
<br />
n = recv(sockfd, buffer, BUFFER_SIZE, 0); <br />
buffer[n] = '\0'; <br />
printf("< %s\n", buffer);<br />
}<br />
while(strncmp(buffer, "exit", 4) != 0); <br />
<br />
close(sockfd); <br />
<br />
return 0;<br />
}
|
|
|
|
|
One thing you have wrong is in the server. You are only accepting ONE connection before entering the receive loop.
You'll never have more than one client connect because of this.
To handle multiple clients, you should spawn a thread for each client, unless you want to learn asynchrnous socket techniques.
|
|
|
|
|