c++winapic++builder-xe

Is there a way to create a shared directory/folder using WINAPI or Embarcadero C++ Builder XE?


Possible Duplicate:
Windows Folder Share API

I need my C++ application to occasionally create shared directories. I am using Embarcadero C++ Builder XE which has only the CreateDir() function which has no way to specify shared access. Is there anything within the Embarcadero C++ Builder environment that will allow me to do this simply, or failing that is there a simple way to do this directly using the Windows API ? I have taken a look at the msdn pages for something that will allow me to do this but I have yet to find anything documented in a clear way.

EDIT Specifically the folder and its contents need to be accessible to all other machines on the network.


Solution

  • To get a directory shared across the network, you need to first create the directory, then tell your computer to share it over the network. Creating the directory itself is the same as creating any other directory (i.e., CreateDir should work fine).

    From there, you need to share the directory. You do this with NetShareAdd. Here's a (really simplistic but tested) bit of sample code:

    #include <windows.h>
    #include <lm.h>
    
    int main() { 
    
        SHARE_INFO_2 info = {0};
    
        info.shi2_netname = L"test_share";
        info.shi2_type = STYPE_DISKTREE;
        info.shi2_permissions = ACCESS_ALL;
        info.shi2_max_uses = -1;
        info.shi2_path = L"C:\\local\\path";
    
        NetShareAdd(NULL, 2, (BYTE *)&info, NULL);
    
        return 0;
    }