Author | Message | Time |
---|---|---|
vonLandenhausen | how to compile this? [code] // MiniChat - Copyright (C) 2003 Skywing #include <winsock2.h> #include <windows.h> #include <richedit.h> // Visual C++-specific #pragma intrinsic(memcmp, memcpy, memset, strcat, strcmp, strcpy, strlen) #define MAKEIP(b4,b3,b2,b1) ((LPARAM)(((DWORD)(b1)<<24)+((DWORD)(b2)<<16)+((DWORD)(b3)<<8)+((DWORD)(b4)))) #define RENDIAN_WORD(W) ((HIBYTE(WORD(W)) >> 0) | (LOBYTE(WORD(W)) << 8)) #define DATA_SIZE 4096 #pragma comment( lib, "ws2_32.lib" ) int InitWinsock(void) { WSAData Data; return WSAStartup(0x0202, &Data); } int Connect(SOCKET Sock) { SOCKADDR_IN Address; Address.sin_addr.s_addr = MAKEIP(63,240,202,138); //Address.sin_addr.s_addr = MAKEIP(211,233,0,76); Address.sin_port = RENDIAN_WORD(6112); Address.sin_family = AF_INET; memset(Address.sin_zero, 0, sizeof(Address.sin_zero)); return connect(Sock, (PSOCKADDR)&Address, sizeof(Address)); } void* Alloc(DWORD Size) { return HeapAlloc(GetProcessHeap(), HEAP_NO_SERIALIZE, Size); } void* AllocZ(DWORD Size) { return HeapAlloc(GetProcessHeap(), HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY, Size); } void Free(void* Memory) { HeapFree(GetProcessHeap(), HEAP_NO_SERIALIZE, Memory); } VOID WINAPI SendCompletion(DWORD dwErrorCode, DWORD dwNumberOfBytesTransferred, LPOVERLAPPED lpOverlapped) { Free(lpOverlapped->hEvent); // Data duplicated Free(lpOverlapped); } void SendData(SOCKET Sock, const char* Data) { size_t Bytes = strlen(Data); void* DuplicateData = Alloc(Bytes); LPOVERLAPPED Overlapped = (LPOVERLAPPED)AllocZ(sizeof(OVERLAPPED)); memcpy(DuplicateData, Data, Bytes); Overlapped->hEvent = DuplicateData; WriteFileEx((HANDLE)Sock, DuplicateData, Bytes, Overlapped, &SendCompletion); } struct ReceiveOverlapped : public OVERLAPPED { SOCKET Sock; bool* Disconnect; HWND OutWindow; }; VOID WINAPI ReceiveCompletion(DWORD dwErrorCode, DWORD dwNumberOfBytesTransferred, ReceiveOverlapped* lpOverlapped) { if(!dwErrorCode && dwNumberOfBytesTransferred) { LPBYTE Data = (LPBYTE)lpOverlapped->hEvent; CHARRANGE Range = {-1, -1}; Data[dwNumberOfBytesTransferred] = '\0'; SendMessage(lpOverlapped->OutWindow, EM_EXSETSEL, 0, (LPARAM)&Range); SendMessage(lpOverlapped->OutWindow, EM_REPLACESEL, FALSE, (LPARAM)Data); ReadFileEx((HANDLE)lpOverlapped->Sock, Data, DATA_SIZE-1, lpOverlapped, (LPOVERLAPPED_COMPLETION_ROUTINE)&ReceiveCompletion); } else { closesocket(lpOverlapped->Sock); *lpOverlapped->Disconnect = true; } } void RunMainLoop(HWND OutWindow, HWND InWindow) { SOCKET Sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(Sock == INVALID_SOCKET) return; bool Disconnect = false; if(!Connect(Sock)) { ShowWindow(OutWindow, SW_SHOW); ShowWindow(InWindow, SW_SHOW); BYTE Data[DATA_SIZE]; ReceiveOverlapped Overlapped; // This MUST NOT go out of scope while ReadFileEx is in progress Overlapped.hEvent = Data; Overlapped.Sock = Sock; Overlapped.Disconnect = &Disconnect; Overlapped.OutWindow = OutWindow; if(ReadFileEx((HANDLE)Sock, Data, DATA_SIZE-1, &Overlapped, (LPOVERLAPPED_COMPLETION_ROUTINE)&ReceiveCompletion) || GetLastError() == ERROR_IO_PENDING) { SendData(Sock, "\x03\x04""Anonymous\r\n"); // Workaround for VC bug while(!Disconnect) { switch(MsgWaitForMultipleObjectsEx(0, 0, INFINITE, QS_ALLINPUT, MWMO_ALERTABLE)) { case WAIT_OBJECT_0: MSG msg; while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) { UINT m = msg.message; TranslateMessage(&msg); if(msg.hwnd == InWindow && msg.message == WM_CHAR && msg.wParam == '\r') { int Size = GetWindowTextLength(InWindow)+3; char* Text = (char*)Alloc(Size); if(!GetWindowText(InWindow, Text, Size-2)) Text[0] = '\0'; *(LPWORD)&Text[Size-3] = '\n\r'; Text[Size-1] = 0; SendData(Sock, Text); SetWindowText(InWindow, ""); } else DispatchMessage(&msg); } if(!IsWindow(InWindow) || !IsWindow(OutWindow)) { closesocket(Sock); SleepEx(0, TRUE); Disconnect = true; } break; } } } } else closesocket(Sock); } void Main(void) { HINSTANCE RichEdit = LoadLibrary("RichEd20.dll"); if(!RichEdit) { ExitProcess(ERROR_FILE_NOT_FOUND); __assume(0); // Visual C++-specific } if(!InitWinsock()) { HINSTANCE Mod = GetModuleHandle(0); if(HWND OutWindow = CreateWindowEx(WS_EX_APPWINDOW | WS_EX_OVERLAPPEDWINDOW, "RICHEDIT20A", "", WS_CAPTION | ES_AUTOVSCROLL | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SIZEBOX | WS_SYSMENU | ES_READONLY | ES_MULTILINE, CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, 0, 0, Mod, 0)) { if(HWND InWindow = CreateWindowEx(0, "EDIT", "MiniChat", WS_CAPTION | ES_AUTOHSCROLL | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SIZEBOX | WS_SYSMENU, 50, 50, 200, 45, OutWindow, 0, Mod, 0)) { RunMainLoop(OutWindow, InWindow); DestroyWindow(InWindow); } DestroyWindow(OutWindow); } WSACleanup(); } FreeLibrary(RichEdit); ExitProcess(ERROR_SUCCESS); } [/code] i get all the time following errors: [code]--------------------Configuration: sky_Maincpp - Win32 Debug-------------------- Linking... LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main Debug/sky_Maincpp.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. sky_Maincpp.exe - 2 error(s), 0 warning(s)[/code] i think its got something to do how i create the project (win32 app/console app/mfc app and so forth) help please :D | November 16, 2004, 7:00 PM |
Mephisto | main() not Main(). And if it's a Win32 project it'll be WinMain(). | November 16, 2004, 7:58 PM |
vonLandenhausen | thx alot | November 17, 2004, 10:32 AM |
vonLandenhausen | hmm, it eventually compiles but it is only a console. shouldnt it be a window?! | November 17, 2004, 6:35 PM |
Zakath | Yes, it should be a window. What kind of project do you have this set up as? | November 17, 2004, 7:07 PM |
Spuck | New -> Project -> Win32 Application should i use a different kind of type? i tried it with dev-c++ as well (without the pragma) and it creates a console app as well :'( help please | November 17, 2004, 7:38 PM |
Mephisto | Did you use main or WinMain? | November 17, 2004, 10:53 PM |
Skywing | Just to clarify, this program was intended to be compiled with /ENTRY:Main (and without the CRT linked in). Changing it to int main() should work, though. If you don't want it to be linked as a console app, then change the subsystem setting to /subsystem:windows instead of /subsystem:console. (This should be in your linker options.) | November 17, 2004, 10:58 PM |
vonLandenhausen | thx mate. but tell me please where to learn such things?;D are so 133D only cauz of msdn.com?>.< i just admire your knowledge ;D | November 18, 2004, 1:17 PM |
Myndfyr | [quote author=vonLandenhausen link=topic=9576.msg89240#msg89240 date=1100783859] are so 133D only cauz of msdn.com?[/quote] Huh? | November 18, 2004, 1:56 PM |
Spuck | 133d = elite ;D | November 18, 2004, 2:10 PM |
vonLandenhausen | [quote author=Skywing link=topic=9576.msg89169#msg89169 date=1100732307] Just to clarify, this program was intended to be compiled with /ENTRY:Main (and without the CRT linked in).[/quote] what is the CRT and how to link withou it? i already tried to find it out it with google... | November 18, 2004, 3:52 PM |
vonLandenhausen | CRT = C Run-Time ?? | November 18, 2004, 3:56 PM |
Zakath | If this thread continues to go off-topic in such a flame-intensive way, it will be locked. This is your warning. | November 20, 2004, 6:51 PM |