⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendit.cpp

📁 《Windows CE 权威指南》(作者:(美)CHRIS MUENCH
💻 CPP
📖 第 1 页 / 共 2 页
字号:

    cbWritten = sendto( sock,
				  (CHAR FAR *)&filePacket,
				  filePacket.PakSize,  0,
				  (SOCKADDR *) &saUdpServ,
				  sizeof ( SOCKADDR_IN ));
    return ( cbWritten == SOCKET_ERROR )
            ? WSAGetLastError() : 0;
}   // SendWSockPacket

// <BOOK_ADDON Chapter 9.6.1> **************************************
BOOL SndIrDA(int len, BYTE *Buffer, DWORD code)
{
#define NumRetries	5	// We try five time to send than fail
SOCKET			sock;
SOCKADDR_IRDA		address = {AF_IRDA, 0,0,0,0, "SendItReceiver"};
DEVICELIST			devList;    
SOCKERR			serr   = 0;
int				devListLen = sizeof(devList),
				cnt = 0,idx=0;
PACKET_FILE 		filePacket;
INT				cbWritten;
BOOL				ret=TRUE;

    sock = socket(AF_IRDA, SOCK_STREAM, 0);
    devList.numDevice = 0; // initialize number of devices to zero
    // Make NumRetries (5) attempts to find server device
    while ((devList.numDevice == 0) && (cnt <= NumRetries))    
    {
        // see if device exists & retrieve its device identifier
        getsockopt(sock, SOL_IRLMP, IRLMP_ENUMDEVICES,
                   (char *)&devList, &devListLen);        
  		if (devList.numDevice!=0) break;
		cnt++;
        Sleep(500); // Wait half a second before retrying    
   }
   if  (cnt > NumRetries)    return FALSE;
   else // We found a server
   {        // Get socket address of server
		for (idx = 0; idx <= 3; idx++)             
			address.irdaDeviceID[idx] = devList.Device[0].irdaDeviceID[idx];
		connect(sock, (struct sockaddr *)&address,sizeof(SOCKADDR_IRDA));
		
		memset(filePacket.szFile,0,sizeof(filePacket.szFile));
		filePacket.type   = PACKET_TYPE_FILE;
		filePacket.cbFile = len;
		filePacket.PakSize= len+(4*sizeof(DWORD));
		filePacket.Code	  = code;
		memcpy( filePacket.szFile, Buffer, len);
		cbWritten = send( sock,(CHAR FAR *)&filePacket,len+(4*sizeof(DWORD)), 0 );
		serr = ( cbWritten == SOCKET_ERROR ) ? WSAGetLastError() : 0;	
	    if( serr != 0 ) ret=FALSE;
		closesocket(sock);    
   }
   return ret;
}
// <BOOK_ADDON Chapter 9.6.1> **************************************


BOOL SendPacket(WCHAR *Buffer, DWORD code,TCHAR *tIP)
{
BOOL ret;
long len;

	len=(wcslen(Buffer)+1)*sizeof(WCHAR); 
// Buffer = String incl. Terminating zero
// <BOOK_ADDON Chapter 9.6.1> **************************************
	if (0==0)
	   ret=SndIrDA(len,(BYTE *)Buffer,code);
	else
// </BOOK_ADDON Chapter 9.6.1> **************************************
		ret=SendWSockPacket( sCommand, len, (BYTE *)Buffer,code, tIP); 
    if( ret != 0 ) return FALSE;	// more error handling goes here
    return TRUE;
}
// </BOOK_ADDON Chapter 9.5.1> **************************************



#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE			hInst;			// The current instance
HWND				hwndCB;			// The command bar handle

// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass	(HINSTANCE hInstance, LPTSTR szWindowClass);
BOOL				InitInstance	(HINSTANCE, int);
LRESULT CALLBACK	WndProc			(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About			(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(	HINSTANCE hInstance,
					HINSTANCE hPrevInstance,
					LPTSTR    lpCmdLine,
					int       nCmdShow)
{
	MSG msg;
	HACCEL hAccelTable;

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_SENDIT);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
// <BOOK_ADDON Chapter 9.5.1> **************************************
	CloseServer();
// </BOOK_ADDON Chapter 9.5.1> **************************************
	return msg.wParam;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
	WNDCLASS	wc;

    wc.style			= CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc		= (WNDPROC) WndProc;
    wc.cbClsExtra		= 0;
    wc.cbWndExtra		= 0;
    wc.hInstance		= hInstance;
    wc.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SENDIT));
    wc.hCursor			= 0;
    wc.hbrBackground	= (HBRUSH) GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName		= 0;
    wc.lpszClassName	= szWindowClass;

	return RegisterClass(&wc);
}

//
//  FUNCTION: InitInstance(HANDLE, int)
//
//  PURPOSE: Saves instance handle and creates main window
//
//  COMMENTS:
//
//    In this function, we save the instance handle in a global variable and
//    create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND	hWnd;
	TCHAR	szTitle[MAX_LOADSTRING];			// The title bar text
	TCHAR	szWindowClass[MAX_LOADSTRING];		// The window class name

	hInst = hInstance;		// Store instance handle in our global variable
	// Initialize global strings
	LoadString(hInstance, IDC_SENDIT, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance, szWindowClass);

	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
#ifdef UNDER_CE // <BOOK_ADDON Chapter 9.5.1/> ****
	hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, 
		NULL, 
		hInstance, NULL);
// <BOOK_ADDON Chapter 9.5.1> *****************************
	#else
	hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
		0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL, 
		LoadMenu(hInstance,MAKEINTRESOURCE(IDM_MENU)),
		hInstance, NULL);
#endif
// <BOOK_ADDON Chapter 9.5.1> *****************************
	if (!hWnd)
	{	
		return FALSE;
	}

	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);
#ifdef UDNER_CE // <BOOK_ADDON Chapter 9.5.1/> ****
	if (hwndCB)
		CommandBar_Show(hwndCB, TRUE);
#endif // <BOOK_ADDON Chapter 9.5.1/> ****

// <BOOK_ADDON Chapter 9.5.1> **************************************
	InitServer(hWnd);
// </BOOK_ADDON Chapter 9.5.1> **************************************
	return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	TCHAR szHello[MAX_LOADSTRING];

	switch (message) 
	{
		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{
				case IDM_HELP_ABOUT:
				   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
				   break;
				case IDM_FILE_EXIT:
				   DestroyWindow(hWnd);
				   break;
// <BOOK_ADDON Chapter 9.5.1> **************************************
				case ID_SENDPACKET:
#ifdef UNDER_CE
					SendPacket(L"This was sent from Windows CE",
						     WM_SOCKET_SELECT,TEXT("192.168.55.1"));
#else
					SendPacket(L"This was sent from Windows NT/98",
				  		     WM_SOCKET_SELECT,TEXT("192.168.55.5"));
#endif
				   break;
// </BOOK_ADDON Chapter 9.5.1> **************************************
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_CREATE:
#ifdef UNDER_CE // <BOOK_ADDON Chapter 9.5.1/> ****
			hwndCB = CommandBar_Create(hInst, hWnd, 1);			
			CommandBar_InsertMenubar(hwndCB, hInst, IDM_MENU, 0);
			CommandBar_AddAdornments(hwndCB, 0, 0);
#endif // <BOOK_ADDON Chapter 9.5.1/> ****
			break;
// <BOOK_ADDON Chapter 9.5.1> **************************************
		case WM_SOCKET_SELECT:
				TCHAR wsu[100];
				if (sizeof(TCHAR)==sizeof(WCHAR))
					wsprintf(wsu,TEXT("%s"),(TCHAR *)wParam);
				else
				{
					WCHAR *tTxt=(WCHAR *)wParam;
					for (int i=0;i<(int)wcslen(tTxt);i++)
						wsu[i]=(TCHAR)tTxt[i];
					wsu[i]=0;
				}	// Some Unicode to ANSI conversion

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -