Use Win32 API to set the read and write permissions shared by the FAT32 folder in Windows XP (1)

Source: Internet
Author: User

 

I just joined the blog Park. Although I used to wander in the garden, I only remember to appreciate the masterpiece of the ox, and never filled the water. This is my virgin post, and the right is to increase the traffic for the garden.

After graduation, I went out of school and found a programming task.

As soon as I got started, I received a task assigned by the project leader: Using Win32 API Programming to Implement Windows XPRead-Only folder sharing in FAT32 formatWhy should we emphasize the FAT32 format? Before I came here, the old version of this project has already achieved file sharing in the NTFS format. Now, we need to add support for the FAT32 format at the customer's requirement.

It is estimated that the project leader thinks this problem is too simple. There is already a ready-made hoist, just draw a picture of it, this low-level task is only worth the effort of my new cainiao.

At the beginning, I thought this task should not be difficult. Although I was definitely a rookie in Win32 API programming, I have never touched on this before. On the one hand, I didn't need it in my previous programming tasks, and I didn't have the guts to touch it. Just a bunch of string conversions left me dizzy.

All right, let's talk about things.

First, let's talk about the differences between NTFs and FAT32 file formats in the Windows operating system in terms of file sharing and read/write permission settings. Simply put, they are:

    1. The NTFS file system can set the read and write permissions for files and folders, while the FAT32 file system does not;
    2. The read/write access control of the NTFS file system is effective for local user access and network access through folder sharing;
    3. Folder sharing also has its own access permission settings, but this access control mechanism only works for network access and does not work for local users;
    4. If the shared folder is in the NTFS partition, network access is controlled by the shared permission setting and NTFS file system permission setting. The final constraint result is the stricter one of the two.

For more information, see: http://technet2.microsoft.com/windowsserver/en/library/ee26f971-c9d4-444f-9622-d5d756229e5e1033.mspx? MFR = true

To express these meanings more intuitively, you can see the following:

First, cancel the default "use simple folder sharing" for Windows XP, and then view the "properties" dialog box for the folders in the FAT32 and NTFS partitions respectively:

The dialog box on the left is in FAT32 format, and the dialog box on the right is in NTFS format,The latter has one more item than the former:Security.

Open "Permissions" in both the dialog box, and then open "security" in the NTFS Properties dialog box ":

This is where we set access permissions through the windows UI. The permission on the left is only used for the network share controller, and the access control function on the right is unique to NTFS, it is valid for both local user access and network access.

The above demonstrates how to set permissions through the windows UI. In addition, for those of us who eat programming meals, it is necessary to know whether there are corresponding doscommands to implement the same function. For the network access permission setting function on the left side, I have not found the corresponding command (if it is easy to find such a command, I will not fill the bucket here :)); for the NTFS access permission setting function on the right, there isDedicated doscommandYou can use:CaclsFor usage, see the DOS help or reference Ms information: http://support.microsoft.com/kb/318754/

After talking so much, it seems that I still haven't gotten into the question. What I want to talk about is how to program it ~~

Let's talk about how to use API ProgrammingNTFS folder sharing:

    1. Call the API function netdeskadd () to set the folder to share. If no other settings are made after this function is called, network users cannot access the shared folder because the folder is stored inNTFS partitionIs also subject to access control of the NTFS file system. Therefore, step 2 is required;
    2. Call the doscommand cacls to grant the read and write permissions to the guest user group;
    3. To cancel file sharing, call the API function netdomaindel () to complete the process.

This is the method used by the old version of the project.CodeI think it is quite simple and clear, and there is no big bend in the way of thinking (a typical post-event Zhuge Liang, others don't know how much energy they have spent and how many detours they have taken to find this solution, then show you the written code. If you see that the number of lines in the Code is not too large, you can say: This is so simple ~~).

For folders in the FAT32 partition, The cacls command does not work. This command is dedicated to files and folders in the NTFS format. That is to say, step 1 of the above solution is unavailable, so now you can only use the netmask add () function to add file sharing. If no other settings are made after this function is called, network users can access shared folders, but have full control permissions (this is the default shared permission setting ), in actual projects, we hope to be able to program and control the read and write permissions. In the NTFS partition, we also have the access control function of the NTFS file system. In the FAT32 partition, we can only rely on the operating system folder to share its own permission setting function.

Now, how do I program and set the read/write permissions for folder sharing?

I don't expect the doscommand. At least I haven't found a command that can achieve this yet; I can only rely on the API, but at the beginning, netmask add () this API has been confusing me for a long time ~~

The netbench add function has a parameter: lpbyteBufIs a pointer to a struct.LevelIf the parameter is set to 2BufThe struct is defined as follows:

1 Typedef Struct _ Share_info_2
2 {
3 Lpwstr shi2_netname;
4 DWORD shi2_type;
5 Lpwstr shi2_remark;
6 DWORD shi2_permissions;
7 DWORD shi2_max_uses;
8 DWORD shi2_current_uses;
9 Lpwstr shi2_path;
10 Lpwstr shi2_passwd;
11 } Performance_info_2,
12 * P1__info_2,
13 * Lp1__info_2;

Note that one of the members is DWORD.Shi2_permissions,Ele. Me, is that my savior? It's too easy !?

However, after repeated and painful experiments, actually, it doesn' t work !!! No matter how much this guy is set, it has no effect on the function execution. Search on csdn, msdn, and cnblog (well, it seems that the garden has no time to discuss such trivial issues), and there are always questions about the usage of this parameter, the post asking for instructions on how to implement read-only sharing of the FAT32 folder. Later, I saw in a corner of msdn that this parameter is invalid for operating systems that do not support user level ......

Hope to fail again. At this time, I, a rookie who has just been in touch with Win32 API for a week and a half, has spent a week on this "small problem. At this time, I felt a little sincere and fearful. I had no results for a week due to such a simple problem. I should have given it to you, fortunately, my boss didn't turn my fear into his actions ~~

No way, you can only find other ways. After repeated searches for information, repeated tests, repeated failures, and repeated searches for information, orz finally found a solution to setting the sharing permission, the specific process should be left to the next post. (My virgin post is just like this. Why has it been written for so long ?)
PS: In normal mode, How can two textures be displayed side by side? I spent half a day doing it in HTML, Dizzy ~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.