|
Naveen and Saurabh,
Thanks for the quick answer. I believe I will go ahead and get VS2008 and start learning it. Since I will be able to keep VC6++ installed I can maintain existing projects until I get them switched over.
|
|
|
|
|
You are welcome.
-Saurabh
|
|
|
|
|
I have a program in c builder and I have several buttons that access sounds. How do you play several sounds at once? For example if you press a button while running a sound it will interrupt the present one. I use the PlaySound function. Thanks!
|
|
|
|
|
What are the files to keep with the static c/c++ lib after it was built in debug mode to enable its source code step into debugging from some executable linked to it?
E.g. there are .obj, .ncb, .idb, .pdb etc... files after the build. Which ones are related to step into the lib code?
Чесноков
|
|
|
|
|
.pdb is required for this - it's the debug symbols. I believe that's all you need.
L u n a t i c F r i n g e
|
|
|
|
|
Yes you only need PDB files.
-Saurabh
|
|
|
|
|
Hi,
I want to make a game of TicTacToe. WhenI resize the window I want to appear more buttons on the interface. From a matrix of 3x3 to 4x4 etc up to 9x9, depending on how much I resize the window. How do I do this?
I will make a free web site design to whoever provides me with a working answer (and something extra for the full program of playing tictactoe).
Thank you!
modified on Wednesday, January 6, 2010 3:28 PM
|
|
|
|
|
can somebody help me in understanding basic c++ connectivity with sql 2005...
i am good in c# trying to learn C++ database part..
i want to know which header file i need to use..?
and how different the programming is from C#
vikas da
|
|
|
|
|
tasumisra wrote: can somebody help me in understanding basic c++ connectivity with sql 2005...
Yes, if you come up with a specific query.
tasumisra wrote: i am good in c# trying to learn C++ database part..
If you're using MFC, you may lookup the CDatabase class. You have other options like ADO, ODBC (without MFC as well), etc., There are a whole lot of articles on CP at all levels of complexity. You may want to read some of those from the beginner's section.
tasumisra wrote: and how different the programming is from C#
There's no comparison between C# and C++.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Rajesh R Subramanian wrote: There's no comparison between C# and C++
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]
|
|
|
|
|
What is mean by canonical assignment operator? If any one knows, pls help.
|
|
|
|
|
|
Hi.
I have the follow pieces of code. A Server and Client code I wrote. I have a few problems. If I disable the sending data from the client and the receving data from the server. Everything works ok for somewhere between 900-4000 times. I then get WriteFileEx returned GetLastError 1450. If I disconnect the client and rerun the application, the Server works again for another 900-4000 times before another 1450 error.
As the code stands, at some point, between 1 and 5 times, both programs just freeze. No errors, nothing.
I can't see the money leak and I am only sending 34bytes to the client and retrieving 4bytes. I'm not doing anything more than 64bytes of memory. I really don't know what the problem is.
Can anyone see the problems I have?
Thanks,
#pragma warning(disable:4995)
#pragma comment(lib, "shell32.lib")
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include <strsafe.h>
#include <time.h>
#include "psapi.h"
#define FNAME_LEN 1000
#define PNAME_LEN 1000
#define SEND_LEN 1024
#define RECV_LEN 64
typedef struct _PIPEINST
{
OVERLAPPED Overlapped;
HANDLE hPipe;
TCHAR tzSend[SEND_LEN];
DWORD dwSendLength;
TCHAR tzRecv[RECV_LEN];
DWORD dwRecvLength;
} PIPEINST, *PPIPEINST;
typedef struct _THREAD_CONTEXT
{
HANDLE hPort;
HANDLE hCompletion;
DWORD uTemporary;
} THREAD_CONTEXT, *PTHREAD_CONTEXT;
HANDLE NewPipe(LPWSTR PipeName, LPOVERLAPPED lpOverlapped)
{
HANDLE hPipe;
hPipe=CreateNamedPipe(PipeName, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, PNAME_LEN*sizeof(TCHAR), PNAME_LEN*sizeof(TCHAR), 1, NULL);
if(hPipe == INVALID_HANDLE_VALUE)
{
printf("CreateNamedPipe failed.\n");
return NULL;
}
if((ConnectNamedPipe(hPipe, lpOverlapped)))
{
printf("ConnectNamedPipe failed.\n");
return NULL;
}
return hPipe;
}
VOID WINAPI CompletedWrite(DWORD dwErr, DWORD dwWritten, LPOVERLAPPED lpOverLap)
{
printf("Written %d.\n", dwWritten);
}
BOOLEAN SendToGUI(HANDLE hPipe, PPIPEINST pPipeInst, DWORD numb, PWCHAR str)
{
PWCHAR buffer;
buffer = (PWCHAR) malloc (SEND_LEN);
swprintf(buffer, L"%d\t%ws\0\0", numb, str);
printf("Sending.. %ws\n", buffer);
StringCchCopy(pPipeInst->tzSend, wcslen(buffer)+2, buffer);
pPipeInst->dwSendLength = (wcslen(pPipeInst->tzSend) * 2);
if(WriteFileEx(hPipe, pPipeInst->tzSend, pPipeInst->dwSendLength + 2, (LPOVERLAPPED) pPipeInst, (LPOVERLAPPED_COMPLETION_ROUTINE) CompletedWrite))
{
free(buffer);
return TRUE;
}
else
{
printf("WriteFileEx failed '%d'.\n", GetLastError());
free(buffer);
return FALSE;
}
}
VOID WINAPI CompletedRead(DWORD dwErr, DWORD dwRead, LPOVERLAPPED lpOverLap)
{
printf("Read %d bytes.\n", dwRead);
}
DWORD ReceiveFromGUI(HANDLE hPipe, PPIPEINST pPipeInst)
{
DWORD BytesRead = 0;
pPipeInst->dwRecvLength = 0;
if(ReadFileEx(hPipe, pPipeInst->tzRecv, RECV_LEN, (LPOVERLAPPED) pPipeInst, (LPOVERLAPPED_COMPLETION_ROUTINE) CompletedRead))
{
return TRUE;
}
else
{
printf("ReadFileEx failed.\n");
return FALSE;
}
}
DWORD Thread(PTHREAD_CONTEXT pThreadContext)
{
OVERLAPPED hEventOverlapped;
PWCHAR PipeName;
HANDLE hPipe;
HANDLE hEvent;
PPIPEINST pPipeInst;
BOOLEAN IsConnected;
if((hEvent=CreateEvent(NULL, TRUE, TRUE, NULL)) == NULL)
{
printf("CreateEvent failed.\n");
goto end_thread;
}
else
{
hEventOverlapped.hEvent = hEvent;
}
if((PipeName=(PWCHAR) malloc(PNAME_LEN+2)) == NULL)
{
printf("malloc(PipeName) failed.\n");
goto end_thread;
}
swprintf(PipeName, L"\\\\.\\pipe\\PipeName%d\0", pThreadContext->uTemporary);
printf("PipeName %ws.\n", PipeName);
pThreadContext->uTemporary = 1;
IsConnected = FALSE;
if((pPipeInst = (PPIPEINST) GlobalAlloc(GPTR, sizeof(PIPEINST)+2)) !=NULL)
if((hPipe = NewPipe (PipeName, &hEventOverlapped)) !=NULL)
while(TRUE)
{
if(!IsConnected)
{
WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
printf("Client has connected.\n");
IsConnected = TRUE;
}
ConnectNamedPipe(hPipe, &hEventOverlapped);
if(GetLastError() != ERROR_PIPE_CONNECTED || GetLastError() == ERROR_PIPE_LISTENING)
{
printf("Client has disconnected.\n");
IsConnected = FALSE;
DisconnectNamedPipe(hPipe);
CloseHandle(hPipe);
if((hPipe = NewPipe (PipeName, &hEventOverlapped)) == NULL)
{
printf("Unable to recreate new pipe.\n");
break;
}
}
else
{
if((SendToGUI(hPipe, pPipeInst, 1, L"string")) == FALSE)
{
printf("SendToGUI failed '%d'\n", GetLastError());
}
else
{
WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
ReceiveFromGUI(hPipe, pPipeInst);
WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
printf("Retrieved %c\n", pPipeInst->tzRecv[0]);
}
}
}
end_thread:
printf("%ws pipe exiting..\n", PipeName);
if(PipeName == NULL); else free(PipeName);
if(hPipe == NULL); else CloseHandle(hPipe);
if(pPipeInst == NULL); else free(pPipeInst);
return 1;
}
void __cdecl main()
{
HANDLE hThread[1];
THREAD_CONTEXT ThreadContext;
DWORD dwThreadId;
DWORD dwi;
for (dwi=0; dwi<1; dwi++)
{
ThreadContext.uTemporary = dwi;
hThread[dwi] = CreateThread(NULL, 0, Thread, &ThreadContext, 0, &dwThreadId);
if(hThread[dwi] == NULL)
{
printf("CreateThread failed.\n");
return;
}
while(ThreadContext.uTemporary == dwi);
}
WaitForMultipleObjectsEx(dwi, hThread, TRUE, INFINITE, FALSE);
printf("Exit success.\n");
return;
}
Client Code
#pragma warning(disable:4995)
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <winioctl.h>
#include <string.h>
#include <crtdbg.h>
#include <assert.h>
#include <fltuser.h>
#include <wchar.h>
#include <strsafe.h>
#include <conio.h>
#define RECV_LEN 1000
#define SEND_LEN 1000
#define PNAME_LEN 1000
int OpenPipe(DWORD PipeNumber, PCHAR PipeName);
int ReadPipe(DWORD PipeNumber, PCHAR PipeRead, DWORD ReadLength);
int WritePipe(DWORD PipeNumber, PCHAR PipeWrite, DWORD WriteLength);
int ClosePipe(DWORD PipeNumber);
typedef struct _PIPEINST
{
OVERLAPPED Overlapped;
HANDLE hPipe;
TCHAR tzSend[SEND_LEN];
DWORD dwSendLength;
TCHAR tzRecv[RECV_LEN];
DWORD dwRecvLength;
DWORD dwState;
BOOL fPendingIO;
} PIPEINST, *PPIPEINST;
PPIPEINST pPipeInst[1];
HANDLE hEvents[1];
int WriteToLog(char* uString)
{
FILE* pFile;
if((pFile = fopen("logfile.log", "a+")) == NULL) return -1;
printf("%s\n", uString);
fclose(pFile);
return 0;
}
int __stdcall OpenPipe(DWORD PipeNumber, PCHAR PipeName)
{
DWORD dwMode;
BOOL fSuccess = FALSE;
WCHAR wBuffer[PNAME_LEN];
UCHAR ErrorMessage[1024];
pPipeInst[PipeNumber] = (PPIPEINST) malloc (sizeof(PIPEINST));
MultiByteToWideChar(CP_ACP, 0, PipeName, -1, wBuffer, strlen(PipeName));
wBuffer[strlen(PipeName)] = '\0';
hEvents[PipeNumber] = CreateEvent(NULL, TRUE, TRUE, NULL);
if(hEvents[PipeNumber] == NULL)
{
printf("CreateEvent failed.\n");
return 1;
}
pPipeInst[PipeNumber]->Overlapped.hEvent = hEvents[PipeNumber];
while (1)
{
pPipeInst[PipeNumber]->hPipe = CreateFile(wBuffer, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (pPipeInst[PipeNumber]->hPipe != INVALID_HANDLE_VALUE) break;
if (GetLastError() != ERROR_PIPE_BUSY) return 1;
if (!WaitNamedPipe(wBuffer, 20000)) return 1;
}
dwMode = PIPE_TYPE_BYTE;
fSuccess = SetNamedPipeHandleState(pPipeInst[PipeNumber]->hPipe, &dwMode, NULL, NULL);
if (!fSuccess) return 1;
return 0;
}
int __stdcall ReadPipe(DWORD PipeNumber, PCHAR pPipeRead, DWORD ReadLength)
{
PWCHAR PipeRead;
BOOL fSuccess = FALSE;
PipeRead = (PWCHAR) malloc (RECV_LEN);
if(PipeRead == NULL)
{
printf("malloc(PipeRead) failed.\n");
return 0;
}
ReadFile(pPipeInst[PipeNumber]->hPipe, PipeRead, RECV_LEN, NULL, &pPipeInst[PipeNumber]->Overlapped);
fSuccess = GetOverlappedResult(pPipeInst[PipeNumber]->hPipe, &pPipeInst[PipeNumber]->Overlapped, &ReadLength, FALSE);
if(fSuccess)
{
sprintf(pPipeRead, "%ws\0", PipeRead);
free(PipeRead);
return ReadLength;
}
free(PipeRead);
return 0;
}
int __stdcall WritePipe(DWORD PipeNumber, PCHAR PipeWrite, DWORD WriteLength)
{
BOOL fSuccess = FALSE;
PWCHAR wBuffer;
DWORD cbWritten;
wBuffer = (PWCHAR) malloc (SEND_LEN);
if(wBuffer == NULL)
{
printf("malloc(wBuffer) failed.\n");
return 1;
}
MultiByteToWideChar(CP_ACP, 0, PipeWrite, -1, wBuffer, strlen(PipeWrite));
wBuffer[strlen(PipeWrite)] = '\0';
fSuccess = WriteFile(pPipeInst[PipeNumber]->hPipe, wBuffer, wcslen(wBuffer) * 2, &cbWritten, NULL);
if (!fSuccess)
{
free(wBuffer);
return 1;
}
else
{
free(wBuffer);
return 0;
}
}
int __stdcall ClosePipe(DWORD PipeNumber)
{
CloseHandle(pPipeInst[PipeNumber]->hPipe);
return 0;
}
void __cdecl main()
{
PCHAR Pipe;
DWORD n = 0;
DWORD ReadLength = 0;
Pipe = (PCHAR) malloc (1024);
if(OpenPipe(0, "\\\\.\\Pipe\\PipeName0") != 0) return;
printf ("Connected.\n");
while (TRUE)
{
if((ReadLength = ReadPipe(n, Pipe, 0)) > 0)
{
printf("Read (%i bytes):\n %s\n", ReadLength, Pipe);
printf("Sending..\n");
printf("Responce: %i (1 is failure.)\n", WritePipe(n, "a", 1));
}
}
return;
}
modified on Wednesday, January 6, 2010 1:57 PM
|
|
|
|
|
Well, your first problem is that you did not format your code properly i.e. within <pre></pre> tags and correctly indented. As such no-one is likely to spend much time trying to read it. Secondly, you need to either run it in the debugger or add debugging code to try and narrow down to at least the general area where it is going wrong.
|
|
|
|
|
The problem exists somewhere in the send and receive on either or both the client and the server but where and what it is I cannot figure it out. Which is why I am here.
|
|
|
|
|
noalias___ wrote: I have a few problems
You're right.
I don't read tons of unformatted code. PRE tags would take care of most formatting.
noalias___ wrote: the money leak
If R&D is exhausting your funds, the only solution is to put your product on the market ASAP, and possibly fix the remaining problems in a future service pack.
BTW: Error 1450 = Insufficient system resources exist to complete the requested service. Which probably means you forgot to free, deallocate, close, ... some memory or other resource.
|
|
|
|
|
Hi.
I have reposted it with pre tags.
I cannot see where I forgot to close anything. CLosing hPipe doesn't count as it would need to leave the while loop for that.
I am really struggling with fixing this problem. It seems so simple.
|
|
|
|
|

#pragma warning(disable:4995)
#pragma comment(lib, "shell32.lib")
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include <strsafe.h>
#include <time.h>
#include "psapi.h"
#define FNAME_LEN 1000
#define PNAME_LEN 1000
#define SEND_LEN 1024
#define RECV_LEN 64
typedef struct _PIPEINST
{
OVERLAPPED Overlapped;
HANDLE hPipe;
TCHAR tzSend[SEND_LEN];
DWORD dwSendLength;
TCHAR tzRecv[RECV_LEN];
DWORD dwRecvLength;
} PIPEINST, *PPIPEINST;
typedef struct _THREAD_CONTEXT
{
HANDLE hPort;
HANDLE hCompletion;
DWORD uTemporary;
} THREAD_CONTEXT, *PTHREAD_CONTEXT;
HANDLE NewPipe(LPWSTR PipeName, LPOVERLAPPED lpOverlapped)
{
HANDLE hPipe;
hPipe=CreateNamedPipe(PipeName, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, PNAME_LEN*sizeof(TCHAR), PNAME_LEN*sizeof(TCHAR), 1, NULL);
if(hPipe == INVALID_HANDLE_VALUE)
{
printf("CreateNamedPipe failed.\n");
return NULL;
}
if((ConnectNamedPipe(hPipe, lpOverlapped)))
{
printf("ConnectNamedPipe failed.\n");
return NULL;
}
return hPipe;
}
VOID WINAPI CompletedWrite(DWORD dwErr, DWORD dwWritten, LPOVERLAPPED lpOverLap)
{
printf("Written %d.\n", dwWritten);
}
BOOLEAN sendd(HANDLE hPipe, PPIPEINST pPipeInst, DWORD numb, PWCHAR str)
{
PWCHAR buffer;
buffer = (PWCHAR) malloc (SEND_LEN);
swprintf(buffer, L"%d\t%ws\0\0", numb, str);
printf("Sending.. %ws\n", buffer);
StringCchCopy(pPipeInst->tzSend, wcslen(buffer)+2, buffer);
pPipeInst->dwSendLength = (wcslen(pPipeInst->tzSend) * 2);
if(WriteFileEx(hPipe, pPipeInst->tzSend, pPipeInst->dwSendLength + 2, (LPOVERLAPPED) pPipeInst, (LPOVERLAPPED_COMPLETION_ROUTINE) CompletedWrite))
{
free(buffer);
return TRUE;
}
else
{
printf("WriteFileEx failed '%d'.\n", GetLastError());
free(buffer);
return FALSE;
}
}
VOID WINAPI CompletedRead(DWORD dwErr, DWORD dwRead, LPOVERLAPPED lpOverLap)
{
printf("Read %d bytes.\n", dwRead);
}
DWORD recvv(HANDLE hPipe, PPIPEINST pPipeInst)
{
DWORD BytesRead = 0;
pPipeInst->dwRecvLength = 0;
if(ReadFileEx(hPipe, pPipeInst->tzRecv, RECV_LEN, (LPOVERLAPPED) pPipeInst, (LPOVERLAPPED_COMPLETION_ROUTINE) CompletedRead))
{
return TRUE;
}
else
{
printf("ReadFileEx failed.\n");
return FALSE;
}
}
DWORD Thread(PTHREAD_CONTEXT pThreadContext)
{
OVERLAPPED hEventOverlapped;
PWCHAR PipeName;
HANDLE hPipe;
HANDLE hEvent;
PPIPEINST pPipeInst;
BOOLEAN IsConnected;
if((hEvent=CreateEvent(NULL, TRUE, TRUE, NULL)) == NULL)
{
printf("CreateEvent failed.\n");
goto end_thread;
}
else
{
hEventOverlapped.hEvent = hEvent;
}
if((PipeName=(PWCHAR) malloc(PNAME_LEN+2)) == NULL)
{
printf("malloc(PipeName) failed.\n");
goto end_thread;
}
swprintf(PipeName, L"\\\\.\\pipe\\PipeName%d\0", pThreadContext->uTemporary);
printf("PipeName %ws.\n", PipeName);
pThreadContext->uTemporary = 1;
IsConnected = FALSE;
if((pPipeInst = (PPIPEINST) GlobalAlloc(GPTR, sizeof(PIPEINST)+2)) !=NULL)
if((hPipe = NewPipe (PipeName, &hEventOverlapped)) !=NULL)
while(TRUE)
{
if(!IsConnected)
{
WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
printf("Client has connected.\n");
IsConnected = TRUE;
}
ConnectNamedPipe(hPipe, &hEventOverlapped);
if(GetLastError() != ERROR_PIPE_CONNECTED || GetLastError() == ERROR_PIPE_LISTENING)
{
printf("Client has disconnected.\n");
IsConnected = FALSE;
DisconnectNamedPipe(hPipe);
CloseHandle(hPipe);
if((hPipe = NewPipe (PipeName, &hEventOverlapped)) == NULL)
{
printf("Unable to recreate new pipe.\n");
break;
}
}
else
{
if((sendd(hPipe, pPipeInst, 1, L"string")) == FALSE)
{
printf("SendToGUI failed '%d'\n", GetLastError());
}
else
{
WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
recvv(hPipe, pPipeInst);
WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
printf("Retrieved %c\n", pPipeInst->tzRecv[0]);
}
}
}
end_thread:
printf("%ws pipe exiting..\n", PipeName);
if(PipeName == NULL); else free(PipeName);
if(hPipe == NULL); else CloseHandle(hPipe);
if(pPipeInst == NULL); else free(pPipeInst);
return 1;
}
void __cdecl main()
{
HANDLE hThread[1];
THREAD_CONTEXT ThreadContext;
DWORD dwThreadId;
DWORD dwi;
for (dwi=0; dwi<1; dwi++)
{
ThreadContext.uTemporary = dwi;
hThread[dwi] = CreateThread(NULL, 0, Thread, &ThreadContext, 0, &dwThreadId);
if(hThread[dwi] == NULL)
{
printf("CreateThread failed.\n");
return;
}
while(ThreadContext.uTemporary == dwi);
}
WaitForMultipleObjectsEx(dwi, hThread, TRUE, INFINITE, FALSE);
printf("Exit success.\n");
return;
}
The Client code.
#pragma warning(disable:4995)
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <winioctl.h>
#include <string.h>
#include <crtdbg.h>
#include <assert.h>
#include <fltuser.h>
#include <wchar.h>
#include <strsafe.h>
#include <conio.h>
#define RECV_LEN 1000
#define SEND_LEN 1000
#define PNAME_LEN 1000
int OpenPipe(DWORD PipeNumber, PCHAR PipeName);
int ReadPipe(DWORD PipeNumber, PCHAR PipeRead, DWORD ReadLength);
int WritePipe(DWORD PipeNumber, PCHAR PipeWrite, DWORD WriteLength);
int ClosePipe(DWORD PipeNumber);
typedef struct _PIPEINST
{
OVERLAPPED Overlapped;
HANDLE hPipe;
TCHAR tzSend[SEND_LEN];
DWORD dwSendLength;
TCHAR tzRecv[RECV_LEN];
DWORD dwRecvLength;
DWORD dwState;
BOOL fPendingIO;
} PIPEINST, *PPIPEINST;
PPIPEINST pPipeInst[1];
HANDLE hEvents[1];
int WriteToLog(char* uString)
{
FILE* pFile;
if((pFile = fopen("logfile.log", "a+")) == NULL) return -1;
printf("%s\n", uString);
fclose(pFile);
return 0;
}
int __stdcall OpenPipe(DWORD PipeNumber, PCHAR PipeName)
{
DWORD dwMode;
BOOL fSuccess = FALSE;
WCHAR wBuffer[PNAME_LEN];
UCHAR ErrorMessage[1024];
pPipeInst[PipeNumber] = (PPIPEINST) malloc (sizeof(PIPEINST));
MultiByteToWideChar(CP_ACP, 0, PipeName, -1, wBuffer, strlen(PipeName));
wBuffer[strlen(PipeName)] = '\0';
hEvents[PipeNumber] = CreateEvent(NULL, TRUE, TRUE, NULL);
if(hEvents[PipeNumber] == NULL)
{
printf("CreateEvent failed.\n");
return 1;
}
pPipeInst[PipeNumber]->Overlapped.hEvent = hEvents[PipeNumber];
while (1)
{
pPipeInst[PipeNumber]->hPipe = CreateFile(wBuffer, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (pPipeInst[PipeNumber]->hPipe != INVALID_HANDLE_VALUE) break;
if (GetLastError() != ERROR_PIPE_BUSY) return 1;
if (!WaitNamedPipe(wBuffer, 20000)) return 1;
}
dwMode = PIPE_TYPE_BYTE;
fSuccess = SetNamedPipeHandleState(pPipeInst[PipeNumber]->hPipe, &dwMode, NULL, NULL);
if (!fSuccess) return 1;
return 0;
}
int __stdcall ReadPipe(DWORD PipeNumber, PCHAR pPipeRead, DWORD ReadLength)
{
PWCHAR PipeRead;
BOOL fSuccess = FALSE;
PipeRead = (PWCHAR) malloc (RECV_LEN);
if(PipeRead == NULL)
{
printf("malloc(PipeRead) failed.\n");
return 0;
}
ReadFile(pPipeInst[PipeNumber]->hPipe, PipeRead, RECV_LEN, NULL, &pPipeInst[PipeNumber]->Overlapped);
fSuccess = GetOverlappedResult(pPipeInst[PipeNumber]->hPipe, &pPipeInst[PipeNumber]->Overlapped, &ReadLength, FALSE);
if(fSuccess)
{
sprintf(pPipeRead, "%ws\0", PipeRead);
free(PipeRead);
return ReadLength;
}
free(PipeRead);
return 0;
}
int __stdcall WritePipe(DWORD PipeNumber, PCHAR PipeWrite, DWORD WriteLength)
{
BOOL fSuccess = FALSE;
PWCHAR wBuffer;
DWORD cbWritten;
wBuffer = (PWCHAR) malloc (SEND_LEN);
if(wBuffer == NULL)
{
printf("malloc(wBuffer) failed.\n");
return 1;
}
MultiByteToWideChar(CP_ACP, 0, PipeWrite, -1, wBuffer, strlen(PipeWrite));
wBuffer[strlen(PipeWrite)] = '\0';
fSuccess = WriteFile(pPipeInst[PipeNumber]->hPipe, wBuffer, wcslen(wBuffer) * 2, &cbWritten, NULL);
if (!fSuccess)
{
free(wBuffer);
return 1;
}
else
{
free(wBuffer);
return 0;
}
}
int __stdcall ClosePipe(DWORD PipeNumber)
{
CloseHandle(pPipeInst[PipeNumber]->hPipe);
return 0;
}
void __cdecl main()
{
PCHAR Pipe;
DWORD n = 0;
DWORD ReadLength = 0;
Pipe = (PCHAR) malloc (1024);
if(OpenPipe(0, "\\\\.\\Pipe\\PipeName0") != 0) return;
printf ("Connected.\n");
while (TRUE)
{
if((ReadLength = ReadPipe(n, Pipe, 0)) > 0)
{
printf("Read (%i bytes):\n %s\n", ReadLength, Pipe);
printf("Sending..\n");
printf("Responce: %i (1 is failure.)\n", WritePipe(n, "a", 1));
}
}
return;
}
|
|
|
|
|
A quick glance didn't reveal any dangling resource.
Do you always write code without proper indentation?
|
|
|
|
|
 No I don't and I've had a look at the code too and no I can't see a problem with it. It compiles fine, the connect and disconnect code is ok. It is only when I un-rem the receive code that things majorly play up and both programs freeze. I'm stuck, really I am.
Server Code.
#pragma warning(disable:4995)
#pragma comment(lib, "shell32.lib")
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include <strsafe.h>
#include <time.h>
#include "psapi.h"
#define FNAME_LEN 1000
#define PNAME_LEN 1000
#define SEND_LEN 1024
#define RECV_LEN 64
typedef struct _PIPEINST
{
OVERLAPPED Overlapped;
HANDLE hPipe;
TCHAR tzSend[SEND_LEN];
DWORD dwSendLength;
TCHAR tzRecv[RECV_LEN];
DWORD dwRecvLength;
} PIPEINST, *PPIPEINST;
typedef struct _THREAD_CONTEXT
{
HANDLE hPort;
HANDLE hCompletion;
DWORD uTemporary;
} THREAD_CONTEXT, *PTHREAD_CONTEXT;
HANDLE NewPipe(LPWSTR PipeName, LPOVERLAPPED lpOverlapped)
{
HANDLE hPipe;
hPipe=CreateNamedPipe(PipeName, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, PNAME_LEN*sizeof(TCHAR), PNAME_LEN*sizeof(TCHAR), 1, NULL);
if(hPipe == INVALID_HANDLE_VALUE)
{
printf("CreateNamedPipe failed.\n");
return NULL;
}
if((ConnectNamedPipe(hPipe, lpOverlapped)))
{
printf("ConnectNamedPipe failed.\n");
return NULL;
}
return hPipe;
}
VOID WINAPI CompletedWrite(DWORD dwErr, DWORD dwWritten, LPOVERLAPPED lpOverLap)
{
printf("Written %d.\n", dwWritten);
}
BOOLEAN SendToGUI(HANDLE hPipe, PPIPEINST pPipeInst, DWORD numb, PWCHAR str)
{
PWCHAR buffer;
buffer = (PWCHAR) malloc (SEND_LEN);
swprintf(buffer, L"%d\t%ws\0\0", numb, str);
printf("Sending.. %ws\n", buffer);
StringCchCopy(pPipeInst->tzSend, wcslen(buffer)+2, buffer);
pPipeInst->dwSendLength = (wcslen(pPipeInst->tzSend) * 2);
if(WriteFileEx(hPipe, pPipeInst->tzSend, pPipeInst->dwSendLength + 2, (LPOVERLAPPED) pPipeInst, (LPOVERLAPPED_COMPLETION_ROUTINE) CompletedWrite))
{
free(buffer);
return TRUE;
}
else
{
printf("WriteFileEx failed '%d'.\n", GetLastError());
free(buffer);
return FALSE;
}
}
VOID WINAPI CompletedRead(DWORD dwErr, DWORD dwRead, LPOVERLAPPED lpOverLap)
{
printf("Read %d bytes.\n", dwRead);
}
DWORD ReceiveFromGUI(HANDLE hPipe, PPIPEINST pPipeInst)
{
DWORD BytesRead = 0;
pPipeInst->dwRecvLength = 0;
if(ReadFileEx(hPipe, pPipeInst->tzRecv, RECV_LEN, (LPOVERLAPPED) pPipeInst, (LPOVERLAPPED_COMPLETION_ROUTINE) CompletedRead))
{
return TRUE;
}
else
{
printf("ReadFileEx failed.\n");
return FALSE;
}
}
DWORD Thread(PTHREAD_CONTEXT pThreadContext)
{
OVERLAPPED hEventOverlapped;
PWCHAR PipeName;
HANDLE hPipe;
HANDLE hEvent;
PPIPEINST pPipeInst;
BOOLEAN IsConnected;
if((hEvent=CreateEvent(NULL, TRUE, TRUE, NULL)) == NULL)
{
printf("CreateEvent failed.\n");
goto end_thread;
}
else
{
hEventOverlapped.hEvent = hEvent;
}
if((PipeName=(PWCHAR) malloc(PNAME_LEN+2)) == NULL)
{
printf("malloc(PipeName) failed.\n");
goto end_thread;
}
swprintf(PipeName, L"\\\\.\\pipe\\PipeName%d\0", pThreadContext->uTemporary);
printf("PipeName %ws.\n", PipeName);
pThreadContext->uTemporary = 1;
IsConnected = FALSE;
if((pPipeInst = (PPIPEINST) GlobalAlloc(GPTR, sizeof(PIPEINST)+2)) !=NULL)
if((hPipe = NewPipe (PipeName, &hEventOverlapped)) !=NULL)
while(TRUE)
{
if(!IsConnected)
{
WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
printf("Client has connected.\n");
IsConnected = TRUE;
}
ConnectNamedPipe(hPipe, &hEventOverlapped);
if(GetLastError() != ERROR_PIPE_CONNECTED || GetLastError() == ERROR_PIPE_LISTENING)
{
printf("Client has disconnected.\n");
IsConnected = FALSE;
DisconnectNamedPipe(hPipe);
CloseHandle(hPipe);
if((hPipe = NewPipe (PipeName, &hEventOverlapped)) == NULL)
{
printf("Unable to recreate new pipe.\n");
break;
}
}
else
{
if((SendToGUI(hPipe, pPipeInst, 1, L"string")) == FALSE)
{
printf("SendToGUI failed '%d'\n", GetLastError());
}
else
{
WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
ReceiveFromGUI(hPipe, pPipeInst);
WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
printf("Retrieved %c\n", pPipeInst->tzRecv[0]);
}
}
}
end_thread:
printf("%ws pipe exiting..\n", PipeName);
if(PipeName == NULL); else free(PipeName);
if(hPipe == NULL); else CloseHandle(hPipe);
if(pPipeInst == NULL); else free(pPipeInst);
return 1;
}
void __cdecl main()
{
HANDLE hThread[1];
THREAD_CONTEXT ThreadContext;
DWORD dwThreadId;
DWORD dwi;
for (dwi=0; dwi<1; dwi++)
{
ThreadContext.uTemporary = dwi;
hThread[dwi] = CreateThread(NULL, 0, Thread, &ThreadContext, 0, &dwThreadId);
if(hThread[dwi] == NULL)
{
printf("CreateThread failed.\n");
return;
}
while(ThreadContext.uTemporary == dwi);
}
WaitForMultipleObjectsEx(dwi, hThread, TRUE, INFINITE, FALSE);
printf("Exit success.\n");
return;
}
Client Code
#pragma warning(disable:4995)
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <winioctl.h>
#include <string.h>
#include <crtdbg.h>
#include <assert.h>
#include <fltuser.h>
#include <wchar.h>
#include <strsafe.h>
#include <conio.h>
#define RECV_LEN 1000
#define SEND_LEN 1000
#define PNAME_LEN 1000
int OpenPipe(DWORD PipeNumber, PCHAR PipeName);
int ReadPipe(DWORD PipeNumber, PCHAR PipeRead, DWORD ReadLength);
int WritePipe(DWORD PipeNumber, PCHAR PipeWrite, DWORD WriteLength);
int ClosePipe(DWORD PipeNumber);
typedef struct _PIPEINST
{
OVERLAPPED Overlapped;
HANDLE hPipe;
TCHAR tzSend[SEND_LEN];
DWORD dwSendLength;
TCHAR tzRecv[RECV_LEN];
DWORD dwRecvLength;
DWORD dwState;
BOOL fPendingIO;
} PIPEINST, *PPIPEINST;
PPIPEINST pPipeInst[1];
HANDLE hEvents[1];
int WriteToLog(char* uString)
{
FILE* pFile;
if((pFile = fopen("logfile.log", "a+")) == NULL) return -1;
printf("%s\n", uString);
fclose(pFile);
return 0;
}
int __stdcall OpenPipe(DWORD PipeNumber, PCHAR PipeName)
{
DWORD dwMode;
BOOL fSuccess = FALSE;
WCHAR wBuffer[PNAME_LEN];
UCHAR ErrorMessage[1024];
pPipeInst[PipeNumber] = (PPIPEINST) malloc (sizeof(PIPEINST));
MultiByteToWideChar(CP_ACP, 0, PipeName, -1, wBuffer, strlen(PipeName));
wBuffer[strlen(PipeName)] = '\0';
hEvents[PipeNumber] = CreateEvent(NULL, TRUE, TRUE, NULL);
if(hEvents[PipeNumber] == NULL)
{
printf("CreateEvent failed.\n");
return 1;
}
pPipeInst[PipeNumber]->Overlapped.hEvent = hEvents[PipeNumber];
while (1)
{
pPipeInst[PipeNumber]->hPipe = CreateFile(wBuffer, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (pPipeInst[PipeNumber]->hPipe != INVALID_HANDLE_VALUE) break;
if (GetLastError() != ERROR_PIPE_BUSY) return 1;
if (!WaitNamedPipe(wBuffer, 20000)) return 1;
}
dwMode = PIPE_TYPE_BYTE;
fSuccess = SetNamedPipeHandleState(pPipeInst[PipeNumber]->hPipe, &dwMode, NULL, NULL);
if (!fSuccess) return 1;
return 0;
}
int __stdcall ReadPipe(DWORD PipeNumber, PCHAR pPipeRead, DWORD ReadLength)
{
PWCHAR PipeRead;
BOOL fSuccess = FALSE;
PipeRead = (PWCHAR) malloc (RECV_LEN);
if(PipeRead == NULL)
{
printf("malloc(PipeRead) failed.\n");
return 0;
}
ReadFile(pPipeInst[PipeNumber]->hPipe, PipeRead, RECV_LEN, NULL, &pPipeInst[PipeNumber]->Overlapped);
fSuccess = GetOverlappedResult(pPipeInst[PipeNumber]->hPipe, &pPipeInst[PipeNumber]->Overlapped, &ReadLength, FALSE);
if(fSuccess)
{
sprintf(pPipeRead, "%ws\0", PipeRead);
free(PipeRead);
return ReadLength;
}
free(PipeRead);
return 0;
}
int __stdcall WritePipe(DWORD PipeNumber, PCHAR PipeWrite, DWORD WriteLength)
{
BOOL fSuccess = FALSE;
PWCHAR wBuffer;
DWORD cbWritten;
wBuffer = (PWCHAR) malloc (SEND_LEN);
if(wBuffer == NULL)
{
printf("malloc(wBuffer) failed.\n");
return 1;
}
MultiByteToWideChar(CP_ACP, 0, PipeWrite, -1, wBuffer, strlen(PipeWrite));
wBuffer[strlen(PipeWrite)] = '\0';
fSuccess = WriteFile(pPipeInst[PipeNumber]->hPipe, wBuffer, wcslen(wBuffer) * 2, &cbWritten, NULL);
if (!fSuccess)
{
free(wBuffer);
return 1;
}
else
{
free(wBuffer);
return 0;
}
}
int __stdcall ClosePipe(DWORD PipeNumber)
{
CloseHandle(pPipeInst[PipeNumber]->hPipe);
return 0;
}
void __cdecl main()
{
PCHAR Pipe;
DWORD n = 0;
DWORD ReadLength = 0;
Pipe = (PCHAR) malloc (1024);
if(OpenPipe(0, "\\\\.\\Pipe\\PipeName0") != 0) return;
printf ("Connected.\n");
while (TRUE)
{
if((ReadLength = ReadPipe(n, Pipe, 0)) > 0)
{
printf("Read (%i bytes):\n %s\n", ReadLength, Pipe);
printf("Sending..\n");
printf("Responce: %i (1 is failure.)\n", WritePipe(n, "a", 1));
}
}
return;
}
|
|
|
|
|
Three posts of the same information! Why not just edit the original?
|
|
|
|
|
I didn't know I could.
Done.
I hope someone can help.
|
|
|
|
|
1. Initialize all variables!
2. Use calloc if you aren't going to set the contents right away, or they will be set by another function.
e.g. Client - OpenPipe()
pPipeInst[PipeNumber] = (PPIPEINST) calloc (1, sizeof(PIPEINST));
Without this your first call to ReadFile will have random garbage in the overlapped structure which may cause it to fail or corrupt or hang.
3. you aren't using overlapped as intended. Your client main() spins on a non-blocking ReadPipe(). I expect that ReadFile() posts an overlapped read but doesn't finish, main calls again, pending still not done but tries to post another overlapped read using the same overlapped structure. This likely hoses the state of the first read and who knows what else. Easy fix: use GetOverlappedResult(..., TRUE) i.e. make it wait for the read to finish. Proper fix: Redo design to use overlapped properly.
...cmk
The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.
- John Carmack
|
|
|
|
|
Hello folks!
This is probably a very simple question i just don't know how to/where to look for the answer...so, i better give you an example:
typedef int(* FUNC_ONE)(int);
typedef int(* FUNC_TWO)(int, int);
int Func(int x)
{
return x;
}
int Func(int x, int y)
{
return x + y;
}
FUNC_ONE pFuncOne = Func;
FUNC_TWO pFuncTwo = Func;
My question is: how do i tell the compiler to use the one parameter version of Func when assigning the value to pFuncOne and use the two param version of Func when assigning the value to pFuncTwo . As it is written in the example it passes the first pointer assignment but gives error on the second one as it is trying to assign the first, one parameter version of Func to the FUNC_TWO type pointer too. I wonder, if i explicitly specify the types, like this:
FUNC_ONE pFuncOne = (FUNC_ONE)Func;
FUNC_TWO pFuncTwo = (FUNC_TWO)Func; will it use the correct function address or will it in both cases use the one parameter version just convert it to FUNC_TWO in the second assignment.
Thanks for any help in advance. Oh, and happy new year to you all!
p.s: i would like to solve this without having to rename the functions.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Sometimes you just have to hate coding to do it well. <
|
|
|
|
|
What happened when you compiled and ran it?
On my system it worked correctly, as the compiler figured out the correct call from the typedef and function signature.
|
|
|
|
|