A Practical Example

Win32 applications can use the CreateFile, ReadFile, and WriteFile API functions to create, access, and modify files over a network using the MSNP redirector. Windows NT and Windows 2000 are the only platforms that support Win32 security. Figure 2-4 demonstrates how to write a simple application that will create a file over a UNC connection. You will find a file with this code on the companion CD, in the \Examples\Chapter02 directory.

Figure 2-4. Simple file creation example

 #include <windows.h> #include <stdio.h> void main(void) { HANDLE FileHandle; DWORD BytesWritten; // Open a handle to file \\Myserver\Myshare\Sample.txt if ((FileHandle = CreateFile("\\\\Myserver\\Myshare\\Sample.txt", GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { printf("CreateFile failed with error %d\n", GetLastError()); return; } // Write 14 bytes to our new file if (WriteFile(FileHandle, "This is a test", 14, &BytesWritten, NULL) == 0) { printf("WriteFile failed with error %d\n", GetLastError()); return; } if (CloseHandle(FileHandle) == 0) { printf("CloseHandle failed with error %d\n", GetLastError()); return; } } 



Network Programming for Microsoft Windows
Linux Server Hacks, Volume Two: Tips & Tools for Connecting, Monitoring, and Troubleshooting
ISBN: 735615799
EAN: 2147483647
Year: 1998
Pages: 159

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net