|
Thanks for your reply Pallini, I need to interface external hardware in this application that come with C++ api than C#, so I am a bit stuckw ith C++/MFC combo I guess.
Basically I get a 512x512 8bit pixel data from a framegrabber which I need to display in realtime and even draw some graphics on this image as needed (mostly squares, dots and lines) based on a colorbar.
PKNT
|
|
|
|
|
You need to double buffer the output. Create a bitmap in memory; copy from your frame to that bitmap, then draw your extra graphics on that bitmap. Then when it's done draw it to the screen in one blit. That's how you get rid of the flicker. Gdi+ will do it fine.
|
|
|
|
|
Hi
I have the following code
INT16 asid; // has a value of 1
CString buffer = ",Asid="
buffer.Format("%s,%x",buffer,asid);
the result is ",Asid,1";
My question is why the trailing comma
Thanks
|
|
|
|
|
What trailng comma? This is what you have (and the use of <pre> tags make it so much easier to read):
INT16 asid = 1;
CString buffer = ",Asid=";
buffer.Format("%s,%x",buffer,asid);
|
|
|
|
|
I meant the comma following the asid,
Jochen explained my mistake
|
|
|
|
|
You put it there in your format string.
|
|
|
|
|
|
You are passing the object itself as parameter to the Format function. This will lead to unpredictable results.
See CStringT::Format[^]:
Quote: The call will fail if the string object itself is offered as a parameter to Format.
|
|
|
|
|
So if I did
CString buffer = ",Asid="
buffer.format("%2x",asid);
Would I get ",Asid=01"
Which is what I am looking for
|
|
|
|
|
ForNow wrote: Would I get ",Asid=01" I don't believe so, I think you'd get "01" as the format() would overwrite your initial value.
How about trying.
CString buffer;
buffer.Format(",Asid=%2x", asid);
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Thanks so much I got to get into the habit of using CString
|
|
|
|
|
Try making a small test project and play around with the format syntax and the specifiers. Knowing it will help you with other (non-CString) areas as well.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Hi,
I'm developing a client-server C language in Windows.
The client connects to server and every 5 seconds send a message. The communication is OK and server receives messages.
I try to develop multi-client (.bat file starts client.exe) - server but I have problem: A segmentation fault appears after n messages.
This is my code:
main
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0){
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET){
printf("Could not create socket : %d" , WSAGetLastError());
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR){
printf("Bind failed with error code : %d" , WSAGetLastError());
}
listen(s , 10000);
puts("Waiting for incoming connections...");
while( TRUE ) {
new_socket = accept(s , (struct sockaddr *)&client, &c);
if (new_socket == INVALID_SOCKET){
printf("accept failed with error code : %d" , WSAGetLastError());
}else{
clientInfo[cont].clientAddr = client ;
clientInfo[cont].hClientSocket = new_socket;
hClientThread[cont] = CreateThread(NULL,0,( LPTHREAD_START_ROUTINE ) cliTh1,( LPVOID ) &clientInfo[cont],0,&dwThreadId ) ;
if ( hClientThread[cont] == NULL ){
printf("Unable to create client thread");
} else {
printf("thread OK\n");
CloseHandle( hClientThread[cont] ) ;
cont++;
}
}
}
Thread function (cliTh1)
DWORD WINAPI cliTh1( LPVOID lpData ){
struct CLIENT_INFO *pClientInfo;
char szClientMsg[250];
static int j = 0;
int i = 0;
char *message;
pClientInfo = (struct CLIENT_INFO *)lpData ;
printf("SOCKET:%d - THREAD_ID:%ld\n", pClientInfo->hClientSocket, GetCurrentThreadId());
while ( 1 ){
if(WSAGetLastError()){
printf("CURRENT:%ld\n", GetCurrentThreadId());
closesocket(pClientInfo->hClientSocket);
ExitThread(GetCurrentThreadId());
}
if(recv( pClientInfo -> hClientSocket, szClientMsg, sizeof( szClientMsg ), 0 ) > 0){
if(j>5000){j=0;};
strcpy(bufferRx[j].packet, szClientMsg);
for(i=0; i < sizeof( szClientMsg ); i++){
szClientMsg[i] = 0;
}
printf("RECEIVE %d: %s",j, bufferRx[j].packet);
j++;
}
}
return(TRUE);
}
I read on Internet, but I don't know where is Error.
Thanks.
|
|
|
|
|
You are accepting an unlimited number of connections but did not check if cont reaches the limit of your clientInfo and hClientThread array sizes. This results in segmentation faults when cont is equal to the number of array items.
|
|
|
|
|
Thanks for reply.
I declared:
HANDLE hClientThread[65000]
struct CLIENT_INFO clientInfo[10000]
I launch 20 client process.
|
|
|
|
|
I find my error!!!! thanks a lot. You advised me to control array size....
|
|
|
|
|
So that is not the error source. But why you are using different sizes?
Another possible source is in these lines in the thread function:
if(j>5000){j=0;};
strcpy(bufferRx[j].packet, szClientMsg);
This requires that the packet contains a null terminated string. You are also using a constant value for the bufferRx size without showing the definition or allocation. It would be better to use a variable when it is allocated or sizeof() when using a fixed size.
|
|
|
|
|
Thanks for your advices!!!! I will correct code source.
|
|
|
|
|
I picked up a function RecursiveDelete on the internet, as I needed to erase all files and folders on an SD card (as part of a larger project). I was having problems insofar as the function would fail on removing the folders after CFileDialog DoModal - there were two nested folders, the deepest failing with code 32 ("in use by another proicess") and the shallowest with 145 ("folder not empty" - expected !)
However, if I call the function BEFORE the DoModal, then everything works fine.
I have extracted the relevent code into a simpler project and it still fails
Perhaps some kind soul can throw some light on why this is so !!
CFileDialog dlgFileBrowse(true);
UINT uiFileBrowseDlgRC;
RecursiveDelete("F:");
uiFileBrowseDlgRC = dlgFileBrowse.DoModal();
void CDeleteFolderTestDlg::RecursiveDelete(CString szPath)
{
CFileFind ff;
BOOL bResult;
CString path = szPath;
if(path.Right(1) != '\\')
path += '\\';
path += "*.*";
bResult = ff.FindFile(path);
BOOL bItemDelete;
DWORD dwLastError;
CString szFilePath;
while(bResult)
{
bResult = ff.FindNextFile();
if (!ff.IsDots() && !ff.IsDirectory())
{
szFilePath = ff.GetFilePath();
bItemDelete = DeleteFile(szFilePath);
if(!bItemDelete)
dwLastError = GetLastError();
}
else if (ff.IsDirectory() && !ff.IsDots())
{
path = ff.GetFilePath();
RecursiveDelete(path);
bItemDelete = RemoveDirectory(path);
if(!bItemDelete)
dwLastError = GetLastError();
}
}
}
Doug
modified 16-Jun-15 3:39am.
|
|
|
|
|
I assume that the CFileDialog object contains a handle to the directory once it's opened and that the handle is freed when the dialog object is destroyed. You could try the following:
CFileDialog * dialog = new CFileDialog(true);
dialog->DoModal();
delete dialog;
RecursiveDelete("F:");
Alternatively you could try:
{
CFileDialog dialog(true);
dialog.DoModal();
}
RecursiveDelete("F:");
The good thing about pessimism is, that you are always either right or pleasently surprised.
|
|
|
|
|
Snap.
You beat me by seconds with the solution.
|
|
|
|
|
Freak30 wrote: CFileDialog * dialog = new CFileDialog(true);
dialog->DoModal();
delete dialog;
RecursiveDelete("F:");
Hi Freak30,
Tried the dynamic allocation of the CFileDialog (and deletion before the call to RecursiveDelete)as you suggested, but still get the same result (files deleted, but deepest folder gets RC=32 and shallowest gets RC=145.
Very confused !!
Doug
|
|
|
|
|
When dlgFileBrowse.DoModal() returns, the dialog window has been closed but the object itself still exists and may have open file handles. These should be closed by the destructor of the browse dialog. I have not tested this but it seems a probable reason.
So you should try to call your delete function after the browse dialog object is destroyed:
UINT uiFileBrowseDlgRC;
{
CFileDialog dlgFileBrowse(true);
uiFileBrowseDlgRC = dlgFileBrowse.DoModal();
}
RecursiveDelete("F:");
Alternatively you can create the dialog using new and delete it before calling RecursiveDelete .
|
|
|
|
|
Apologies for the double posting - thought that the original hadn't worked !!
Tried dynamic allocation of dialog and subsequent deletion before calling RecursiveDelete as Freak32 suggested, but get exactly the same result. Wierd !!
Could someone else run this and see if they can reproduce this behaviour ? (Driving me mad !)
Doug
|
|
|
|
|
A quick web research brought some more infomation.
CFileDialog will change the current working directory which may lead to the locked directory. So you may try to also set the current working directory to something else (e.g. to another drive) before calling your delete function:
SetCurrentDirectory(_T("C:\\"));
|
|
|
|