c# - How to commuicate with a Memory Mapped File from a Web API? -


i have web api controller needs communicate local tool via memory mapped file. when try open such file openexisting "file not found" error.

string mmf_in_name = "memorymappedfilename"; memorymappedfile mmf_in = memorymappedfile.openexisting(mmf_in_name); 

i tried add prefix "global/" name without success.

next tried start command line tool controller. tool starts gets same "file not found error". when run tool myself works fine. means file name correct.

how can convince iis worker let me open , use memory mapped file?

i'm using windows server 2012 , iss 8.5.

this works on windows server 2012 , iis 8.5.

it's important understand iis worker runs in different terminal server session normal applications. windows service.

so when application exposing memory mapped file needs create via "global\" prefix added name. needs add security descriptor or identity. in c# this:

string mmf_name = @"global\mymemorymappedfilename";  var security = new memorymappedfilesecurity(); security.addaccessrule(new system.security.accesscontrol.accessrule<memorymappedfilerights>(new securityidentifier(wellknownsidtype.worldsid, null), memorymappedfilerights.fullcontrol, accesscontroltype.allow));   var mmf = memorymappedfile.createoropen(mmf_name     , 1024 * 1024     , memorymappedfileaccess.readwrite     , memorymappedfileoptions.none     , security     , system.io.handleinheritability.inheritable); 

in c++ this:

tchar szname[] = text("global\mymemorymappedfilename");  handle hmapfile; lpctstr pbuf;  security_descriptor sd;  if (!initializesecuritydescriptor(&sd, security_descriptor_revision))   printf("initializesecuritydescriptor failed %d\n", getlasterror());  if (!setsecuritydescriptordacl(&sd, true, 0, false))   printf("setsecuritydescriptordacl failed %d\n", getlasterror());  security_attributes sa; sa.nlength = sizeof(sa); sa.lpsecuritydescriptor = &sd; sa.binherithandle = false;    hmapfile = createfilemapping(   invalid_handle_value,    // use paging file   &sa,                    // default security   page_readwrite,          // read/write access   0,                       // maximum object size (high-order dword)   buf_size,                // maximum object size (low-order dword)   szname);                 // name of mapping object  if (hmapfile == null) {   _tprintf(text("could not create file mapping object (%d).\n"),        getlasterror());   return 1; } 

an application creating such objects needs start admin rights.

now when client iis worker tries access file needs make sure use correct name, aka use "global\" prefix. in c# like:

string mmf_name = @"global\mymemorymappedfilename";  var mmf = memorymappedfile.openexisting(mmf_name     , memorymappedfilerights.readwrite     , handleinheritability.inheritable); 

in c++:

tchar szname[] = text("global\\mymemorymappedfilename");  handle hmapfile; lpctstr pbuf;  hmapfile = openfilemapping(   file_map_all_access,   // read/write access   true,                 // !!!!! inherit name   szname);               // name of mapping object  if (hmapfile == null) {   _tprintf(text("could not open file mapping object (%d).\n"),        getlasterror());   return 1; } 

when done. iis worker should able access application via memory mapped file. no need change identity of worker. in fact, run in default settings.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -